Add a proper LDAP user details
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 28 Apr 2010 09:55:47 +0000 (09:55 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 28 Apr 2010 09:55:47 +0000 (09:55 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@3510 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

security/modules/org.argeo.security.manager.ldap/META-INF/spring/ldap-osgi.xml
security/modules/org.argeo.security.manager.ldap/META-INF/spring/ldap.xml
security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/ldap/ArgeoSecurityDaoLdap.java

index d3623c3c46b34bd0ef840aa5dead5fa863294ef6..12f8909eb28da77ac58ebcde2e1e0ce5eac984fb 100644 (file)
@@ -12,7 +12,7 @@
 \r
        <service ref="securityDao" interface="org.argeo.security.ArgeoSecurityDao"\r
                context-class-loader="service-provider" />\r
-       <service ref="userDetailsManager"\r
+       <service ref="userDetailsService"\r
                interface="org.springframework.security.userdetails.UserDetailsService"\r
                context-class-loader="service-provider" />\r
 \r
index 3f6f3db5881efbd66c6e79a759f598865acfafaa..bd117530d814198547a7cabce1219ca861e708a8 100644 (file)
@@ -53,8 +53,8 @@
                <property name="userNatureMappers" ref="userNatureMappers" />
        </bean>
 
-       <bean id="userDetailsManager" factory-bean="securityDao"
-               factory-method="getUserDetailsManager">
+       <bean id="userDetailsService" factory-bean="securityDao"
+               factory-method="getUserDetailsService">
        </bean>
 
        <bean id="ldapAuthenticator"
index 305fe903f9f9cf55f0a81c58ddf8a15f70f664e0..171252848f08dabbb9ab38298a5ddf8c29d0e35e 100644 (file)
@@ -16,10 +16,10 @@ import org.argeo.security.core.ArgeoUserDetails;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.ldap.core.ContextExecutor;
 import org.springframework.ldap.core.ContextMapper;
-import org.springframework.ldap.core.ContextSource;
 import org.springframework.ldap.core.DirContextAdapter;
 import org.springframework.ldap.core.DistinguishedName;
 import org.springframework.ldap.core.LdapTemplate;
+import org.springframework.ldap.core.support.BaseLdapPathContextSource;
 import org.springframework.security.Authentication;
 import org.springframework.security.context.SecurityContextHolder;
 import org.springframework.security.ldap.DefaultLdapUsernameToDnMapper;
@@ -27,9 +27,12 @@ import org.springframework.security.ldap.LdapAuthoritiesPopulator;
 import org.springframework.security.ldap.LdapUsernameToDnMapper;
 import org.springframework.security.ldap.LdapUtils;
 import org.springframework.security.ldap.populator.DefaultLdapAuthoritiesPopulator;
+import org.springframework.security.ldap.search.FilterBasedLdapUserSearch;
 import org.springframework.security.userdetails.UserDetails;
 import org.springframework.security.userdetails.UserDetailsManager;
+import org.springframework.security.userdetails.UserDetailsService;
 import org.springframework.security.userdetails.ldap.LdapUserDetailsManager;
+import org.springframework.security.userdetails.ldap.LdapUserDetailsService;
 import org.springframework.security.userdetails.ldap.UserDetailsContextMapper;
 
 public class ArgeoSecurityDaoLdap implements ArgeoSecurityDao, InitializingBean {
@@ -46,12 +49,13 @@ public class ArgeoSecurityDaoLdap implements ArgeoSecurityDao, InitializingBean
        private String defaultRole = "ROLE_USER";
        private String rolePrefix = "ROLE_";
 
+       private final BaseLdapPathContextSource contextSource;
        private final LdapTemplate ldapTemplate;
 
        private LdapUsernameToDnMapper usernameMapper = null;
 
        private UserDetailsContextMapper userDetailsMapper;
-//     private LdapUserDetailsService ldapUserDetailsService;
+       private LdapUserDetailsService ldapUserDetailsService;
        private List<UserNatureMapper> userNatureMappers;
 
        public void afterPropertiesSet() throws Exception {
@@ -83,15 +87,19 @@ public class ArgeoSecurityDaoLdap implements ArgeoSecurityDao, InitializingBean
                        userDetailsManager = ludm;
                }
 
-//             if (ldapUserDetailsService == null) {
-//                     ldapUserDetailsService = new LdapUserDetailsService(null,
-//                                     authoritiesPopulator);
-//                     ldapUserDetailsService.setUserDetailsMapper(userDetailsMapper);
-//             }
+               if (ldapUserDetailsService == null) {
+                       FilterBasedLdapUserSearch ldapUserSearch = new FilterBasedLdapUserSearch(
+                                       userBase, "(" + usernameAttributeName + "={0})",
+                                       contextSource);
+                       ldapUserDetailsService = new LdapUserDetailsService(ldapUserSearch,
+                                       authoritiesPopulator);
+                       ldapUserDetailsService.setUserDetailsMapper(userDetailsMapper);
+               }
        }
 
-       public ArgeoSecurityDaoLdap(ContextSource contextSource) {
-               ldapTemplate = new LdapTemplate(contextSource);
+       public ArgeoSecurityDaoLdap(BaseLdapPathContextSource contextSource) {
+               this.contextSource = contextSource;
+               ldapTemplate = new LdapTemplate(this.contextSource);
        }
 
        public void create(ArgeoUser user) {
@@ -279,8 +287,8 @@ public class ArgeoSecurityDaoLdap implements ArgeoSecurityDao, InitializingBean
                this.groupClasses = groupClasses;
        }
 
-       public UserDetailsManager getUserDetailsManager() {
-               return userDetailsManager;
+       public UserDetailsService getUserDetailsService() {
+               return ldapUserDetailsService;
        }
 
 }