淺析security遇到java.lang.IllegalArgumentException:Cannot pass null or empty values to constructor問題處理


一、問題描述

  使用 Spring Security 開發登錄鑒權校驗時,發現部分用戶報錯:Reason: Cannot pass null or empty values to constructor in spring security

ERROR ***.security.JWTLoginFilter 221 - An internal error occurred while trying to authenticate the user. org.springframework.security.authentication.InternalAuthenticationServiceException: Cannot pass null or empty values to constructor at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:123) at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:144) at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:175) at com.enmox.emcs.security.JWTLoginFilter.attemptAuthentication(JWTLoginFilter.java:92)

  下面報錯的顯示就都是 security 源碼里的方法。

二、原因分析及方案解決

1、原因分析:

  因為無法在 spring security 中將 null 或空值傳遞給構造函數。遇到這個報錯大概率都是因為在實現 UserDetailService 接口,執行查庫操作時發現有 null 值(如當前用戶的信息用戶名或者用戶密碼之類為 null)。那么我們就需要確定 null 值的字段,在 UserDetailsService 實現類進行斷點查看即可。

2、為什么 null 會報錯

  查看 security 源碼發現,其在創建user的時候,密碼為null會報錯

  可以看到拋出的錯誤正好是遇到的錯誤,由此可以確定問題點。去數據庫查看登錄異常用戶,其密碼為 null,將其設為空字符串即可。

3、心得:

  當自己代碼沒問題,報錯不知道問題出在哪時,需靜下心來根據報錯定位,去源碼里找,搜索報錯信息,或許可以快速定位到問題節點。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM