Spring Security 报There is no PasswordEncoder mapped for the id "null"

1、Spring Security升级到5.0后,代码如下,报错误:Spring Security 报There is no PasswordEncoder mapped for the id "null"

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("123").roles("USER");
    }
}

2、

需要增加一个添加一个新的类

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


    @Override
    public boolean matches(CharSequence charSequence, String s) {
        return s.equals(charSequence.toString());
    }
}

 

3、把该新增的类注入到容器中

//@Configuration 可以不加,因为@EnableWebSecurity中已经包含了@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("123").roles("USER");
    }
    //新增的类注入到容器
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new PasswordEncoderUtil();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章