【发布时间】:2019-05-21 07:08:06
【问题描述】:
我正在开发一个 spring boot maven 项目来验证具有用户名和密码的用户。如果用户通过了身份验证,微服务需要返回 true/false。
SecurityConfiguration.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]
Active Directory Authentication using Spring Security 3.2, Spring Ldap 2.0 and JavaConfig
基于上述链接,我尝试了 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。我需要对密码进行编码吗?
【问题讨论】:
-
这是您的完整用户名,包括域“pan@abc.in”吗?
-
@slimane 用户名是 pan,rest 是域
标签: spring-boot spring-security active-directory ldap