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> 

希望这是有帮助的。

相关问题