首页 > 解决方案 > 通过 Spring Security 的 Active Directory 身份验证返回由 LDAP 引起的有效用户的错误凭据:错误代码 49

问题描述

我正在开发一个 spring boot maven 项目来验证具有用户名和密码的用户。如果用户通过身份验证,微服务需要返回 true/false。

安全配置.java

package com.app.config;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{

    private static final Logger logger = LoggerFactory.getLogger(SecurityConfiguration.class);

    private String url = "ldaps://org.abc.in:3387";
    private String domain = "org.abc.in";
    private String userDNPattern = "CN=pan,OU=Users,OU=UCV,DC=org,DC=abc,DC=in";

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                        .authorizeRequests().antMatchers("/", "logout").permitAll();

    }


     @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {              
         auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());

        }

     @Bean
        public AuthenticationManager authenticationManager() {
         return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
        }

     @Bean
        public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {

            ActiveDirectoryLdapAuthenticationProvider adProvider = new ActiveDirectoryLdapAuthenticationProvider(domain, url);
            adProvider.setConvertSubErrorCodesToExceptions(true);
            adProvider.setUseAuthenticationRequestCredentials(true);
            //adProvider.setAuthoritiesMapper(new NullAuthoritiesMapper());
            return adProvider;
        }

}

我通过传递用户名和密码来调用以下方法

public Authentication signin(String username, String password) throws Exception{
        Authentication auth = new UsernamePasswordAuthenticationToken("pan@abc.in", "password");
        return authenticationManager.authenticate(auth); // this line gives Bad Credential error
    }

调用方法 authenticationManager.authenticate(auth) 时出现以下错误

ctiveDirectoryLdapAuthenticationProvider : Active Directory authentication failed: Supplied password was invalid
org.springframework.security.authentication.BadCredentialsException: Bad credentials
    at org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider.badCredentials(ActiveDirectoryLdapAuthenticationProvider.java:308)

引起:org.springframework.security.ldap.authentication.ad.ActiveDirectoryAuthenticationException:[LDAP:错误代码 49 - 80090308:LdapErr:DSID-0C09042F,注释:AcceptSecurityContext 错误,数据 52e,v2580

我浏览了以下链接: javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]

使用 Spring Security 3.2、Spring Ldap 2.0 和 JavaConfig 的 Active Directory 身份验证

LDAP:错误代码 49 - 80090308:LdapErr:DSID-0C0903A9,注释:AcceptSecurityContext 错误,数据 52e,v1db1

基于上述链接,我尝试了 setAuthoritiesMapper 并通过了 NullAuthoritiesMapper,因为我没有任何权限进行映射。

adProvider.setAuthoritiesMapper(new NullAuthoritiesMapper());

我试图做出改变,userDNPattern = "CN=pan,OU=Users,OU=UCV,DC=org,DC=abc,DC=in";但没有奏效。CN= pan 是我的用户名

我可以使用上面的 url、域和 userDnPattern 和密码访问 Apache Active Directory。我需要对密码进行编码吗?

标签: spring-bootspring-securityactive-directoryldap

解决方案


这可能不是唯一的问题,但这肯定是其中的一部分:

adProvider.setSearchFilter(userDNPattern);

该方法采用 LDAP 查询来查找用户,而不是 DN。的文档setSearchFilter()说默认是这样的:

(&(objectClass=user)(userPrincipalName={0}))

这将采用您提供的用户名,并找到具有该用户名的用户userPrincipalName(“username@domain.com”格式)。

如果这就是你想要的,那么你根本不需要打电话setSearchFilter()

如果您希望用户能够仅使用用户名 ( sAMAccountName) 登录,那么您可以使用以下命令:

adProvider.setSearchFilter("(&(objectClass=user)(sAMAccountName={0}))");

更新:我认为您还有其他一些问题:

private String url = "ldaps://org.abc.in:3387";
private String domain = "org.abc.in";

首先,ldaps://实际上是无效的。只需使用ldap://. 如果指定的端口使用 SSL,那么它将进行 SSL 握手。

此外,您将域设置为org.abc.in,但是当您登录时,您使用的是@abc.in. domain您指定的 附加到您为登录指定的用户名,因此它正在尝试使用 登录,pan@abc.in@org.abc.in这当然行不通。您最好将其设置为domainnull以便以后不会尝试附加任何内容。但这意味着您的用户需要使用该username@abc.in格式登录。


推荐阅读