【问题标题】:How to authenticate a user from LDAP based on mail and by uid with spring Security?如何基于邮件和使用spring Security的uid从LDAP认证用户?
【发布时间】:2016-08-01 06:24:59
【问题描述】:

我希望用户能够通过他的 uid 和邮件登录?如何使用我的 spring 安全配置来实现这一点?

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest().fullyAuthenticated()
                .and()
                .formLogin().passwordParameter("password");
    }

    @Configuration
    protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {

        @Autowired
        LdapContextSource contextSource;

        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
            auth
                    .ldapAuthentication()
                    .userDnPatterns("uid={0}")
                    .contextSource(contextSource);
        }
    }
}

我可以在 userDnPatterns 中指定另一个属性和 uid 吗?或者使用 uid 进行身份验证是标准的?

【问题讨论】:

    标签: java spring security spring-boot ldap


    【解决方案1】:

    您需要使用自定义用户搜索过滤器。以下代码使用 OR 过滤器,尝试将 uid 或 mail 与用户在登录屏幕中输入的值匹配:

    @Configuration
    @EnableWebSecurity
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .anyRequest().fullyAuthenticated()
                    .and()
                    .formLogin().passwordParameter("password");
        }
    
        @Configuration
        protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
    
            @Autowired
            LdapContextSource contextSource;
    
            @Override
            public void init(AuthenticationManagerBuilder auth) throws Exception {
                auth
                        .ldapAuthentication()
                        .userDnPatterns("uid={0}")
                        .userSearchFilter("(|(uid={0})(mail={0}))")
                        .contextSource(contextSource);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-15
      • 2016-08-11
      • 2015-02-22
      • 2012-09-04
      • 2017-09-22
      • 2020-09-27
      • 1970-01-01
      • 2014-04-04
      • 2013-02-11
      相关资源
      最近更新 更多