今天使用springsecurity报错了,出现了下面这种错误。
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder.matches(DelegatingPasswordEncoder.java:250) ~[spring-security-core-5.3.6.RELEASE.jar:5.3.6.RELEASE]
at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:198) ~[spring-security-core-5.3.6.RELEASE.jar:5.3.6.RELEASE]
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$LazyPasswordEncoder.matches(WebSecurityConfigurerAdapter.java:615) ~[spring-security-config-5.3.6.RELEASE.jar:5.3.6.RELEASE]
at org.springframework.security.authentication.dao.DaoAuthenticationProvider.additionalAuthenticationChecks(DaoAuthenticationProvider.java:90) ~[spring-security-core-5.3.6
经过百度一查,发现是springsecurity5.x后面的版本都已经不能这样写了,需要写md5加密的密码才行。所以只要把这个改成如下几种代码形式即可。
第一种
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("ADMIN");
第二种
auth.inMemoryAuthentication().passwordEncoder(new Pbkdf2PasswordEncoder()).withUser("admin").password(new Pbkdf2PasswordEncoder().encode("123456")).roles("ADMIN");