大家好。就像标题所说的那样,我在使用Payara Server上部署的应用程序上使用最新的带有JavaEE的Spring Security和JavaEE来对用户进行身份验证时遇到问题。
我已经在ApacheDS中设置了DIT,其中有几个用户的密码使用SHA256进行哈希处理。我知道我使用的不是那么安全的哈希算法,但是在尝试将ApacheDS提供的哈希算法与Spring Security上可用的哈希算法进行匹配时遇到问题。
关于主要问题,在验证java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "SHA256"
用户身份时出现异常。
我将列出使该应用程序的身份验证过程正常工作的相关代码段:
pom.xml
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-bom</artifactId>
<version>5.1.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
</dependency>
</dependencies>
WebSecurityConfig类
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest()
.fullyAuthenticated()
.and()
.formLogin();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.
ldapAuthentication()
.userDnPatterns("uid={0},ou=users,ou=applications")
.contextSource()
.url("ldap://localhost:10389/dc=mycompany,dc=com")
.and()
.passwordCompare()
.passwordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder())
.passwordAttribute("userPassword");
}
}
SecurityWebApplicationInitializer类
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
public SecurityWebApplicationInitializer() {
super(WebSecurityConfig.class);
}
}
根据我从Spring Security文档中可以了解的内容,在JavaEE Web应用程序上使用Spring Security启用和使用身份验证只需要前面的两个类。我的应用程序是JSF Web应用程序,并且我正在使用Payara Server版本4.1.2.172进行部署。我还使用Spring Security生成的默认登录表单登录应用程序,一旦能够解决此问题,我计划将其替换。顺便说一句,我没有使用嵌入式ApacheDS服务器---我正在使用非嵌入式ApacheDS安装。
请注意,此异常不同于许多人似乎获得的异常,其中id为“ null”。另外,我可以断言该应用程序可以正确访问ApacheDS,但是由于某种原因,它在为用户定义的sha256哈希密码找到密码编码器时遇到了问题。我已经检查了ApacheDS存储密码的方式,它看起来像这样:{SHA256} 14Kjt6P / yQAg3t7Vr7JRldj9hJ032OWJSp2MAaU843w =。我可能是错的,但是我认为Spring Security希望根据我在Internet上看到的代码(包括this页面上的Spring Security开发人员和其他人的代码)来判断哈希算法ID为小写在互联网上。
请帮助我解决这个问题,并且如果可能的话,有人可以告诉我Apache Security提供的最安全的哈希算法是什么,Spring Security也支持该算法?
此外,我想知道当涉及LDAP服务器时,Spring Security使用什么算法来认证用户。据我所知,就是这样:
非常感谢您的任何答复。如您所见,这也是我第一次使用Spring Security和LDAP服务器。