1. 程式人生 > >Spring Security 報There is no PasswordEncoder mapped for the id "null"

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

encode row The autowired auto ppa params isp span

查了下發現是spring security 版本在5.0後就要加個PasswordEncoder了

解決辦法
  1. 在securityConfig類下加入NoOpPasswordEncoder,不過官方已經不推薦了
    @Bean
    public static NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }
  1. 在securityConfig類下加入密碼加密,在數據庫中存的密碼也是要經過這個加密的才能匹配上
    @Autowired
    private UserDetailsService customUserService;

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(customUserService).passwordEncoder(new BCryptPasswordEncoder());
    }

補充:加密操作

    public static void main(String[] args) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        //加密"0"
        String encode = bCryptPasswordEncoder.encode("0");
        System.out.println(encode);
        //結果:$2a$10$/eEV4X7hXPzYGzOLXfCizu6h7iRisp7I116wPA3P9uRcHAKJyY4TK
    }


作者:yyq唯心不易
鏈接:https://www.jianshu.com/p/9e7792d767b2
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並註明出處。

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