2016-08-01 26 views
0

我想要一個用戶能夠通過他的用戶名和郵件登錄嗎?如何使用我的spring安全配置來實現這一點?如何根據郵件和uid使用spring Security從LDAP驗證用戶?

@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進行身份驗證是標準的?

回答

1

您需要使用自定義用戶搜索過濾器。以下代碼使用OR過濾器嘗試將uid或郵件與用戶在登錄屏幕中輸入的值進行匹配:

@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); 
     } 
    } 
} 
相關問題