1. 程式人生 > >springboot整合shiro時認證出現報錯(Submitted credentials for token...)

springboot整合shiro時認證出現報錯(Submitted credentials for token...)

springboot整合shiro時認證出現報錯無非就是密碼不匹配

可能發生的原因:

  1. 前端傳的密碼是明文,而後臺儲存的是hash值,導致先後臺不匹配報錯

如果資料庫儲存的密碼是加密的 那麼要 從前端獲取密碼後,在Java裡將其轉換成hash值

  1. 如果java已經將其加密,但仍然報錯那就去ShiroConfig裡面看憑證匹配器是不是set了hashIterations(2)
@Bean
public HashedCredentialsMatcher hashedCredentialsMatcher() {
    HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
    hashedCredentialsMatcher.setHashAlgorithmName(ShiroUtils.algorithmName);
    hashedCredentialsMatcher.setHashIterations(ShiroUtils.hashIterations);
    return hashedCredentialsMatcher;
}

如果設定了2那麼就是加密了兩次 和你的密碼肯定不匹配了,這個時候就把這行程式碼註釋掉吧 因為預設就是1,雜湊一次

  1. 可能是因為在UsernamePasswordToken內部將密碼部分轉為字元陣列了,所以要這樣取String password = new String((char[]) token.getCredentials()); ,此密碼為明文,然後給密碼加密String md5Pwd = new Md5Hash(password, username).toHex();,裡面的username為salt,然後再將md5Pwd放到SimpleAuthenticationInfo中,下面貼一張圖
    在這裡插入圖片描述