
問題
在學習Shiro的時候,遇到Shiro丟擲org.apache.shiro.authc.AuthenticationException
異常,完整異常如下:
org.apache.shiro.authc.AuthenticationException: Authentication failed for token submission [org.apache.shiro.authc.UsernamePasswordToken - xue8, rememberMe=false]. Possible unexpected error? (Typical or expected login exceptions should extend from AuthenticationException).
複製程式碼
出現這個異常的原因是因為身份驗證出錯了,但是我覺得我寫的Realm應該沒什麼錯誤,最後折騰了一會,翻看了一下Shiro的一些原始碼,終於知道出現這個問題的原因了,於是想將過程記錄下來。
解決
我在Realm
的身份證驗證方法doGetAuthenticationInfo
新增斷點進行除錯

authenticationToken
獲取使用者密碼getCredentials()
的時候出問題了,這就奇怪了,獲取使用者名稱getPrincipal()
的時候沒問題,而且通過檢視authenticationToken
的原始碼,發現他們兩個是一樣的東西


doGetAuthenticationInfo
的時候都是成功的,那為什麼會出錯呢?在除錯doGetAuthenticationInfo
的時候,發現了username和password的儲存方式不一樣

subject.login(token)
方法中,使用者名稱和密碼都是以字串String儲存在token中的,

doGetAuthenticationInfo
就變了呢,於是我繼續檢視了UsernamePasswordToken
的原始碼,會不會是因為這個物件將密碼的字串型別轉成char[]型別呢

UsernamePasswordToken
將傳入的密碼password從String字串型別轉成了char[]字元陣列型別,到這裡我也就明白了,在Realm
的doGetAuthenticationInfo
方法中,傳入的AuthenticationToken authenticationToken
使用者名稱是以String方式儲存的而密碼是以Char[]儲存的,用接收String的方式去接收Char[]型別資料肯定就不行了呢,所以我將密碼改成用Char[]接收,如下

即可解決問題。
原文地址:ddnd.cn/2019/02/01/…