【问题标题】:Spring security LDAP check empty passwordSpring Security LDAP检查空密码
【发布时间】:2018-10-08 22:50:59
【问题描述】:

我在我的应用程序中使用 LDAP 身份验证。 我使用这个代码:

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    String domain = customProperties.getAdDomain();
    String url = customProperties.getAdUrl();
    ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(domain,url);
    provider.setConvertSubErrorCodesToExceptions(true);
    provider.setUseAuthenticationRequestCredentials(true);
    provider.setUserDetailsContextMapper(userDetailsContextMapper());
    auth.authenticationProvider(provider);
    auth.userDetailsService(new MyUserDetailsService());
}

使用空密码进行身份验证。我知道我需要插入一个空密码检查,因为在这种情况下并非所有 LDAP 服务器都返回错误。如何以及在哪里插入空白密码支票更好?

【问题讨论】:

    标签: java spring spring-security ldap


    【解决方案1】:

    除了使用 ActiveDirectoryLdapAuthenticationProvider 之外,您还可以使用 Spring 的 LdapTemplate 来自定义实现如何针对 LdapServer 对用户进行身份验证。您可以参考推荐herehere配置LDAP模板。

    然后,您可以创建一个 CustomAuthenticationProvider 类来处理身份验证。

    CustomAuthenticationProvider.class

    public class CustomAuthenticationProvider implement AuthenticationProvider{
    
      @Autowired
      private LdapTemplate ldapTemplate;
    
      @Override
      public Authentication authenticate(Authentication auth) throws AuthenticationException{
        String username = auth.getName;
        String password = auth.getCredentials().toString();
    
        .. Your code to check whether password is blank ..
    
        AndFilter andFilter = new AndFilter();
        andFilter.and(new EqualFilter("<LDAP USER ATTRIBUTE>",username))
              .and(new EqualFilter("<LDAP GROUP ATTRIBUTE>","<USER GROUP>"));
    
        boolean isValidUser = ldapTemplate.authenticate("",andFilter.encode(),password);
    
        ... Your code to complete the authentication ...
    
    {
    

    我更喜欢这种方法,因为它可以让我更好地控制如何验证用户。这是我之前实现的sample的链接。

    【讨论】:

      猜你喜欢
      • 2012-03-28
      • 2017-09-25
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 2014-09-13
      • 2023-03-20
      • 2018-08-01
      • 2012-06-12
      相关资源
      最近更新 更多