spring boot报错:There is no PasswordEncoder mapped for the id “null“

x33g5p2x  于2022-02-07 转载在 Spring  
字(1.0k)|赞(0)|评价(0)|浏览(257)

spring boot报错:There is no PasswordEncoder mapped for the id "null"

原因:新版spring boot需要在自定义的WebSecurityConfigurerAdapter里面的

configure(AuthenticationManagerBuilder auth)

函数里面为明文密码实现一个密码加密器。

解决方案:在自己继承的WebSecurityConfigurerAdapter的类中的

configure(AuthenticationManagerBuilder auth)

中,自定义一个密码加密器类,为明文加密。例如:

@Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("admin")
                .password("admin").roles("USER", "ADMIN")
                .and()
                .withUser("fly")
                .password("123456").roles("USER")
                .and()
                .passwordEncoder(new MyPasswordEncoder());
    }

其中的

MyPasswordEncoder

即为自定义的密码加密器。其实现为:

import org.springframework.security.crypto.password.PasswordEncoder;

public class MyPasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence rawPassword) {
        return rawPassword.toString();
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return rawPassword.equals(encodedPassword);
    }
}

相关文章