其他分享
首页 > 其他分享> > Spring Security – There is no PasswordEncoder mapped for the id “null”解决方案

Spring Security – There is no PasswordEncoder mapped for the id “null”解决方案

作者:互联网

异常说明

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]

后面还有一些先省略不表,我们在这个异常中,可以看到主要原因是: There is no PasswordEncoder mapped for the id “null”

即我们在对密码进行编码的时候没有指定对应的密码编码器.这个原因是由于在spring security5中,密码的存储需要进行编码,所以需要添加编码格式,spring Security框架已经为我们定制好了一些默认编码器,我们只需要在代码中声明即可

出错部分代码

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("123456").roles("ADMIN");//基于内存管理的角色授权登录系统
    }
 }

更改方法

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("{noop}123456").roles("ADMIN");//基于内存管理的角色授权登录系统
    }
 }

指定了NoOpPasswordEncoder进行编码

More

根据官网的介绍,我们可以使用以下的声明方式进行不同的编码

{bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG (1)
{noop}password (2)
{pbkdf2}5d923b44a6d129f3ddf3e3c8d29412723dcbde72445e8ef6bf3b508fbf17fa4ed4d6b99ca763d8dc (3)
{scrypt}$e0801$8bWJaSu2IKSn9Z9kM+TPXfOc/9bdYSrN1oD9qfVThWEwdRTnO7re7Ei+fUZRJ68k9lTyuTeUp4of4g24hHnazw==$OAOec05+bXxvuu/1qZ6NUR+xQYvYv7BeL1QxwRpY5Pc=  (4)
{sha256}97cde38028ad898ebc02e690819fa220e88c62e0699403e94fff291cfffaf8410849f27605abcbc0 (5)

参考

https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-format
https://www.mkyong.com/spring-boot/spring-security-there-is-no-passwordencoder-mapped-for-the-id-null/

标签:mapped,no,DelegatingPasswordEncoder,spring,There,Spring,RELEASE,security,passwor
来源: https://blog.csdn.net/boyan_HFUT/article/details/91541114