Spring boot - 安全性java配置 - LDAP

时间:2015-08-19 12:55:12

标签: spring spring-security spring-ldap

我正在尝试通过java配置设置ldap身份验证。方法init中的代码auth.getObject返回null并且无法设置身份验证管理器,也没有看到异常。配置有什么问题吗?我如何获得身份验证管理器对象?

@Configuration
    public class LdapAuthenticationConfig extends GlobalAuthenticationConfigurerAdapter {

        /** The environment. */
        private Environment environment;

        @Bean(name="ldapAuthManager")
        public AuthenticationManager getAuthManager(){
            return authenticationManager;
        }

        private AuthenticationManager authenticationManager;


        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
            auth.ldapAuthentication()
                    .userSearchFilter(
                            "(&(sAMAccountName={0})(objectclass=organizationalPerson))")
                    .userSearchBase("OU="+environment.getProperty("ldap.user-search-base.name"))
                    .groupSearchFilter("(member={0})")
                    .groupSearchBase("OU=Global-Groups").groupRoleAttribute("cn")
                    .contextSource().url(environment.getProperty("ldap.url"))
                    .managerDn(environment.getProperty("ldap.conn.user"))
                    .managerPassword(environment.getProperty("ldap.conn.pwd"));

            setAuthenticationManager(auth.getObject());

        }

        public AuthenticationManager getAuthenticationManager() {
            return authenticationManager;
        }

        public void setAuthenticationManager(
                AuthenticationManager authenticationManager) {
            this.authenticationManager = authenticationManager;
        }

        @Autowired
        public void setEnvironment(Environment environment) {

            this.environment = environment;
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试使用port和searchBase为LDAP服务器添加完整的URL“ldap.url”,例如:ldap://**server name**:**port**/**o=userSearchBase**

它对我有用。对于某些reasen .port().userSearchBase()无法构建正确的网址。

希望它有所帮助。