Security內存驗證無法登陸,There is no PasswordEncoder mapped for the id "null"

Security進行內存驗證用戶的時候,代碼是這麼寫的

auth.inMemoryAuthentication().withUser("demo").password("demo").roles("USER");//用戶角色
auth.inMemoryAuthentication().withUser("ssss").password("111111").roles("SSSS");
auth.inMemoryAuthentication().withUser("dddd").password("111111").roles("DDDD");
auth.inMemoryAuthentication().withUser("ffff").password("111111").roles("FFFF");

報錯如下
在這裏插入圖片描述
看着視頻教學,老師也是這麼寫的,但是我的就不行,已經被視頻坑過不止一次兩次了,都是有剪輯的,後來發現這裏有坑

**首先Spring Security5.0之後,原始密碼是“password”,在Spring Security5.0中密碼的存儲格式是“{id}。。。”這種格式的。前面的id是加密方式,也就是說,程序拿到傳過來的密碼的時候,首先會以“{id}。。。”這種格式來確定後面的密碼是被怎麼樣加密的,如果找不到就認爲id是null。所以我們的程序機會一直出現這個錯誤:There is no PasswordEncoder mapped for the id “null”。

所以我們只需要這麼些就可以解決了

auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("ceshi").password(new BCryptPasswordEncoder().encode("123456")).roles("USER");

完美

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