[英]How to authenticate a user from LDAP based on mail and by uid with spring Security?
I want a user to be able to login both by his uid and by mail ? 我希望用户能够同时通过其uid和邮件登录? How to achieve this using my spring Security configuration ?
如何使用我的spring Security配置实现此目的?
@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);
}
}
}
At userDnPatterns can I specify another attribute along with uid? 我可以在userDnPatterns中指定另一个属性以及uid吗? Or authentication with uid is standard ?
还是用uid认证是标准的?
You need to use a custom user search filter. 您需要使用自定义用户搜索过滤器。 The following code uses an OR filter that tries to match the uid or mail to the value entered by the user in the login screen:
以下代码使用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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.