在spring-security登陆时报错:There is no PasswordEncoder mapped for the id “null”
版本问题
用spring-security5.0以上报错
使用内存获取认证信息
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
出现报错
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches(DelegatingPasswordEncoder.java:244) ~[spring-security-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:198) ~[spring-security-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration$LazyPasswordEncoder.matches(AuthenticationConfiguration.java:289) ~[spring-security-config-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks(DaoAuthenticationProvider.java:90) ~[spring-security-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:166) ~[spring-security-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:175) ~[spring-security-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:200) ~[spring-security-core-5.1.5.RELEASE.jar:5.1.5.RELEASE]
原因
Spring security 5.0中新增了多种加密方式,也改变了密码的格式。
解决方法
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("user").password(new BCryptPasswordEncoder().encode("password")).roles("USER");
另外
是从数据库中获取认证信息的话,这样写
auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());
本文详细解析了在Spring Security 5.x版本中遇到的There is no PasswordEncoder mapped for the id 'null'错误。此错误通常发生在使用内存或数据库进行用户认证时,未正确配置密码编码器。文章提供了具体的解决方案,包括如何使用BCryptPasswordEncoder进行密码加密,以确保认证过程顺利进行。
518

被折叠的 条评论
为什么被折叠?



