問題描述-
在學習spring security時使用明文密碼進行登錄校驗時報錯"org.springframework.security.authentication.BadCredentialsException: Bad credentials(壞 憑 證)",
1`校驗時數據庫密碼同樣是明文密碼
2`校驗時已經將{noop}拼接但還是報錯
問題解決-
使用spring security5.0后,配置文件中直接寫普通的密碼如:123456,會報錯:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
這是因為spring security5.0以后默認需要選擇密碼加密方式,如果還想用簡單密碼的話,spring security還是給了兩個方案,一種是在配置文件中配置:
1`在xml中配置NoOpPasswordEncoder
<bean id="passwordEncoder" class="org.springframework.security.crypto.password.NoOpPasswordEncoder" factory-method="getInstance"/>
2`在你校驗時密碼拼接{noop}
"{noop}"+userInfo.getPassword()
問題后續-
在使用方法2時,既xml配置沒有設置使用加密密碼編碼(PasswordEncoder)時,校驗時拼接{noop}依舊檢驗不成功,原因未知.
后選用在xml中配置不適用加密編碼時,去掉拼接的{noop}校驗成功了.
再使用bCryptPasswordEncoder.encode(password)方法生成加密密碼后手動存入數據庫,再開啟xml的配置一切正常.
參考地址https://blog.csdn.net/qq_42197032/article/details/101379132