Hi,
I found that i can customize UserDetailsService to have my own login logic, but not sure if it's the right approach. Here's my configuration below:
applicationContext-security.xml
Code:
<authentication-manager>
<authentication-provider user-service-ref = "userDetailsService" />
</authentication-manager>
<beans:bean id = "userDetailsService" class = "com.test.security.authentication.MyUserService" />
MyUserService.java
Code:
public class MyUserService implements UserDetailsService{
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException, DataAccessException {
try{
TransactUserExample exa = new TransactUserExample();
exa.createCriteria().andUsernameEqualTo(username);
/** get DAO from context, this is actually done via Spring-2.0 context.getBean("userDao")**/
List<TransactUser> users = TransactInitServlet.getUserDAO().selectByExample(exa);
if(users.size() == 1) {
TransactUser u = users.get(0);
GrantedAuthorityImpl auth = new GrantedAuthorityImpl(u.getRole());
User user = new User(u.getUsername(),
u.getPassword(), true, true, true, true, getAuthorities(true));
}
} catch(Exception ex) {
ex.printStackTrace();
}
return null;
}
private GrantedAuthority[] getAuthorities(boolean isAdmin) {
List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>(2);
authList.add(new GrantedAuthorityImpl("ROLE_USER"));
if (isAdmin) {
authList.add(new GrantedAuthorityImpl("ROLE_ADMIN"));
}
return authList.toArray(new GrantedAuthority[] {});
}
}
is this the right way of doing it? i can login by implementing this with iBatis + MySQL + Spring-2.0, how can i get all user details, like firstname, lastname, email etc. once successfully logged in?
Thanks!