package com.app.admin.framework.system.permission.shiro.matcher;

import cn.hutool.crypto.SecureUtil;
import com.app.framework.core.utils.pwd.PasswordUtil;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

/**
 * 告诉shiro如何验证加密密码，通过SimpleCredentialsMatcher或HashedCredentialsMatcher
 */
public class PwdCredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
        Object accountCredentials = getCredentials(info);
        String pwd = String.valueOf(token.getPassword());
        if(pwd.length() < 32){
            pwd = SecureUtil.md5(pwd);
        }
        String pwd_encrypt = PasswordUtil.encrypt(pwd);
        //将密码加密与系统加密后的密码校验，内容一致就返回true,不一致就返回false
        return equals(pwd_encrypt, accountCredentials);
    }
}
