IncorrectCredentialsException: Submitted credentials for token did not match the expected credential


shiroパスワードコンパレータの問題
問題の背景
これはshiroプロジェクトのログインパスワード処理を変更する際に発生した問題であり、sso単点ログインの論理がshiroによってログイン検証されることである.しかし問題は、私が単点登録入口から受け取った情報にはパスワードがなく、データベースにはMD 5の要約から出た暗号文が格納されており、通常の手段で明文パスワードを得ることができないことです.
異常原因
シングルポイントでビジネスロジックにログインしてshiro検証を行う場合、明文パスワードを取得できず、shiroのデフォルト証明書比較器の検証に合格できません.
org.apache.shiro.authc.IncorrectCredentialsException: did not match the expected credentials.

解決方法
SimpleCredentialsMatcherを継承して暗号比較器をカスタマイズし、論理を書き換える
/**
 * @Date: 2020/3/30 20:46
 * @Description:
 * shiro
 *         
 */
public class CredentialMatcher extends SimpleCredentialsMatcher {
     

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
     
        UsernamePasswordToken uToken = (UsernamePasswordToken)token;
        //       
        String password  =  new String(uToken.getPassword());
        //           
        String dbPassword = (String) info.getCredentials();
        if(dbPassword.equals(password.trim())){
     
            //      ,          md5 
            return true;
        }else {
     
            //       ,       ,md5
            String pwd = MD5.encodeByMD5(password.trim());
            if(dbPassword.equalsIgnoreCase(pwd)){
     
                return true;
            }else {
     
                return this.equals(pwd, dbPassword);
            }
        }
    }

}

ShiroConfig構成の変更
@Configuration
public class ShiroConfig {
     

	@Bean(name = "loginRealm")
    public UserRealm getLoginRealm(){
     
        UserRealm realm= new UserRealm();
        //     CredentialMatcher,    doCredentialsMatch    sso       。
        realm.setCredentialsMatcher(credentialMatcher());
//        realm.setCredentialsMatcher(getHashedCredentialsMatcher());
        return realm;
    }
    
	@Bean
    public CredentialMatcher credentialMatcher() {
     
    	//         
        return new CredentialMatcher();
    }
    
    @Bean
    public HashedCredentialsMatcher getHashedCredentialsMatcher(){
     
    	//         
        HashedCredentialsMatcher rm = new HashedCredentialsMatcher();
        rm.setHashAlgorithmName("md5");
        rm.setHashIterations(1);
        return rm;

    }

}

天下の英雄は私の世代を出て、江湖に入ると歳月は私に生活を爱する「无间行者」を催促して、努力して実践した解决策をみんなに分かち合いますもしこの文章があなたに役に立つならば、1つの賛、1つの評論、1つの関心、私はすべてとても楽しくて、少し励まして、私にあなたが見ていることを知らせます.