I found there are plenty of resources on how could an LDAP user is map to a role:
- configure LdapAuthenticationProvider in Spring. (read here)
- configure user-context-mapper-ref and extends one class with DefaultLdapAuthoritiesPopulator (read here)
- confgiure user-context-mapper-ref and extends one class with LdapUserDetailsMapper (read here)
- configure BindAuthenticator constructor and implements one class with LdapAuthoritiesPopulator (read here)
public class MyAuthorityMapper extends LdapUserDetailsMapper { @Override public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection authority) { UserDetails userDetails = super.mapUserFromContext(ctx, username, authority); Collection<grantedauthority> authorities = new ArrayList<grantedauthority>(); if( "huahsin".equalsIgnoreCase(username) ) { authorities.add(new GrantedAuthorityImpl("ROLE_ADMIN")); } else { authorities.add(new GrantedAuthorityImpl("ROLE_USER")); } return new User(userDetails.getUsername(), userDetails.getPassword(), true, true, true, true, authorities); } }ROLE_ADMIN and ROLE_USER shown in the code snippet above is hard coded sample on how the authority is granted. In real life, the role should obtain from other source such as database. On Spring configuration site, I will have this:
<authentication-manager alias="authenticationManager"> <ldap-authentication-provider user-search-filter="cn={0}" group-search-base="ou=Counter Strike,ou=java,dc=homebrew,dc=org" user-context-mapper-ref="myAuthorityMapper"/> </authentication-manager> <beans:bean class="org.huahsin.WebEngineering.MyAuthorityMapper" id="myAuthorityMapper"/> <ldap-server url="ldap://127.0.0.1:10389"/>
No comments:
Post a Comment