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

 

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