java - 如何使用 spring Security 根据邮件和 uid 从 LDAP 验证用户?

标签 java spring security spring-boot ldap

我希望用户能够通过其 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 进行身份验证是标准的?

最佳答案

您需要使用自定义用户搜索过滤器。以下代码使用 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);
        }
    }
}

关于java - 如何使用 spring Security 根据邮件和 uid 从 LDAP 验证用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38692200/

相关文章:

java - jar 在浏览器中启动时未签名,但在 javaws 中则未签名

java - 可反序列化异常: local class is not compatible cause of serialVersionID

java - Spring + Hibernate 未插入数据库(@Transactional 不起作用)

java - Veracode - 网页中与脚本相关的 HTML 标签的不正确中和(基本 XSS)

java - TestNG 中的动态 @Test 生成

java - 可能发生错误的情况 - "Is there an unresolvable circular reference?"

java - HtmlUnit 安全警告 : Please treat the URL above as you would your password and do not share it with anyone

security - 如何在数据库上安全地存储用户名/密码(哈希不会)

java - 在 tuProlog 中执行查询时出错

java - 延迟在 Java 图形中不起作用