2017-02-17 97 views
0

對於Spring Boot中的Spring安全性設置。 LDAP身份驗證提供程序默認配置爲使用BindAuthenticator類。如何在Spring Boot中對BindAuthenticator的handleBindException進行Spring LDAP身份驗證設置

該類包含方法

/** 
* Allows subclasses to inspect the exception thrown by an attempt to bind   with a 
* particular DN. The default implementation just reports the failure to  the debug 
* logger. 
*/ 
protected void handleBindException(String userDn, String username,  Throwable cause) { 
if (logger.isDebugEnabled()) { 
logger.debug("Failed to bind as " + userDn + ": " + cause); 
} 
} 

這種方法來處理身份驗證有關的異常像無效憑證。

我想重載這個方法,所以我可以處理這個問題並根據LDAP返回的錯誤代碼返回適當的錯誤信息。如無效的密碼或帳戶被鎖定。

當前的LDAP實現總是返回「Bad Credentials」,但沒有給出正確的圖片說明爲什麼我的憑證無效。我想覆蓋的情況下

  1. 在帳戶被鎖定
  2. 密碼已過期,所以我可以重定向到更改密碼
  3. 帳戶鎖定因無效密碼重試次數

請幫助

+0

是否應該......你真的想把它還給你的最終用戶嗎?從安全的角度來看,禹不想公開這些信息。如果一個賬戶被鎖定,黑客現在知道它有一個有效的用戶名。同樣的,你告訴他密​​碼錯誤或者用戶名不存在。 –

+0

@Denium我建立這個內部應用程序只有工作人員可以訪問到內部網。所以這是我的產品所有者的期望:) – Ahsan

+1

從安全的角度來看並不重要。誰說所有的用戶都很樂意,那些不滿的員工呢。我可以想象,您可能希望在日誌中記錄這些信息,但您希望能夠像外部世界那樣通用 –

回答

1

我通過定義LDAP上下文而不是使用Spring啓動LDAPAuthenticationProviderConfigurer來解決問題。

然後創建FilterBasedLdapUserSearch並用我的ConnectBindAuthenticator覆蓋BindAuthentication。

我爲spring引導配置創建了一個單獨的LDAPConfiguration類,並將所有這些自定義對象註冊爲Beans。

從我創建LdapAuthenticationProvider可疑通過傳遞我的自定義對象構造器

的配置是如下

@Bean 
public DefaultSpringSecurityContextSource contextSource() { 
    DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(env.getProperty("ldap.url")); 
    contextSource.setBase(env.getProperty("ldap.base")); 
    contextSource.setUserDn(env.getProperty("ldap.managerDn")); 
    contextSource.setPassword(env.getProperty("ldap.managerPassword")); 
    return contextSource; 
} 

@Bean 
public ConnectBindAuthenticator bindAuthenticator() { 
    ConnectBindAuthenticator connectBindAuthenticator = new ConnectBindAuthenticator(contextSource()); 
    connectBindAuthenticator.setUserSearch(ldapUserSearch()); 
    connectBindAuthenticator.setUserDnPatterns(new String[]{env.getProperty("ldap.managerDn")}); 
    return connectBindAuthenticator; 
} 

@Bean 
public LdapUserSearch ldapUserSearch() { 
    return new FilterBasedLdapUserSearch("", env.getProperty("ldap.userSearchFilter"), contextSource()); 
} 
0

你必須改變你的春季安全配置,以增加您的認證者的延長上述目的:

CustomBindAuthenticator.java

public class CustomBindAuthenticator extends BindAuthenticator { 

    public CustomBindAuthenticator(BaseLdapPathContextSource contextSource) { 
     super(contextSource); 
    } 

    @Override 
    protected void handleBindException(String userDn, String username, Throwable cause) { 
     // TODO: Include here the logic of your custom BindAuthenticator 
     if (somethingHappens()) { 
      throw new MyCustomException("Custom error message"); 
     } 

     super.handleBindException(userDn, username, cause); 
    } 
} 

彈簧security.xml文件

<beans:bean id="contextSource" 
    class="org.springframework.security.ldap.DefaultSpringSecurityContextSource"> 
    <beans:constructor-arg value="LDAP_URL" /> 
    <beans:property name="userDn" value="USER_DN" /> 
    <beans:property name="password" value="PASSWORD" /> 
</beans:bean> 

<beans:bean id="userSearch" 
    class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch"> 
    <beans:constructor-arg index="0" value="USER_SEARCH_BASE" /> 
    <beans:constructor-arg index="1" value="USER_SEARCH_FILTER" /> 
    <beans:constructor-arg index="2" ref="contextSource" /> 
</beans:bean> 

<beans:bean id="ldapAuthProvider" 
    class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider"> 
    <beans:constructor-arg> 
     <beans:bean class="com.your.project.CustomBindAuthenticator"> 
      <beans:constructor-arg ref="contextSource" /> 
      <beans:property name="userSearch" ref="userSearch" /> 
     </beans:bean> 
    </beans:constructor-arg> 
</beans:bean> 

<security:authentication-manager alias="authenticationManager"> 
    <security:authentication-provider ref="ldapAuthProvider" /> 
</security:authentication-manager> 

希望這是有幫助的。

相關問題