我希望用户能够通过其 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/