gpt4 book ai didi

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

转载 作者:知者 更新时间:2024-03-13 08:50:13 32 4
gpt4 key购买 nike

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);
    }
}

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com