Created
March 5, 2012 19:30
-
-
Save anonymous/1980513 to your computer and use it in GitHub Desktop.
scpring_habr_question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class MongoUserDetailsService implements UserDetailsService { | |
@Override | |
public UserDetails loadUserByUsername(String username) throws AuthenticationServiceException { | |
MongoOperations mongoOperations = null; | |
try { | |
mongoOperations = new MongoTemplate(new Mongo(), "mydb"); | |
} catch (UnknownHostException e) { | |
e.printStackTrace(); | |
} catch (MongoException e) { | |
e.printStackTrace(); | |
} | |
try { | |
Query query = new Query(Criteria.where("email").is(username)); | |
System.out.println("query ready to go"); | |
Customer customer = mongoOperations.findOne(query, Customer.class); | |
System.out.println("query done"); | |
if (customer == null){ | |
throw new AuthenticationServiceException("Authentication failed for user " + username); | |
} | |
boolean enabled = true; | |
boolean accountNonExpired = true; | |
boolean credentialsNonExpired = true; | |
boolean accountNonLocked = true; | |
return new User( | |
customer.getUsername(), | |
customer.getPassword().toLowerCase(), | |
enabled, | |
accountNonExpired, | |
credentialsNonExpired, | |
accountNonLocked, | |
getAuthorities(2)); | |
} catch (Exception e) { | |
System.out.println("query failed"); | |
throw new RuntimeException(e); | |
} | |
} | |
public Collection<? extends GrantedAuthority> getAuthorities(Integer role) { | |
List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role)); | |
return authList; | |
} | |
public List<String> getRoles(Integer role) { | |
List<String> roles = new ArrayList<String>(); | |
if (role.intValue() == 1) { | |
roles.add("ROLE_USER"); | |
roles.add("ROLE_ADMIN"); | |
} else if (role.intValue() == 2) { | |
roles.add("ROLE_USER"); | |
} | |
return roles; | |
} | |
public static List<GrantedAuthority> getGrantedAuthorities(List<String> roles) { | |
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); | |
for (String role : roles) { | |
authorities.add(new GrantedAuthorityImpl("ROLE_USER")); | |
} | |
return authorities; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> | |
<http access-denied-page="/error403.jsp"> | |
<intercept-url pattern="/index*" access="ROLE_USER,ROLE_ANONYMOUS"/> | |
<intercept-url pattern="/add*" access="ROLE_USER"/> | |
<intercept-url pattern="/delete/*" access="ROLE_ADMIN"/> | |
<form-login login-page="/login.jsp" default-target-url="/index" authentication-failure-url="/login.jsp?error=true"/> | |
<logout logout-url="/logout" logout-success-url="/index"/> | |
<anonymous username="guest" granted-authority="ROLE_ANONYMOUS"/> | |
<remember-me user-service-ref="userDetailsService"/> | |
</http> | |
<authentication-manager> | |
<authentication-provider user-service-ref="mongoUserDetailsService"/> | |
</authentication-manager> | |
<beans:bean id="mongoUserDetailsService" class="com.company.testproj.utils.MongoUserDetailsService"/> | |
</beans:beans> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment