package com.nms.swspkmas_standalone.shiro; import cn.hutool.crypto.digest.MD5; import com.nms.swspkmas_standalone.filter.JwtFilter; import com.nms.swspkmas_standalone.utils.JwtTokenUtil; import lombok.extern.slf4j.Slf4j; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.credential.CredentialsMatcher; import org.springframework.stereotype.Component; import java.util.Objects; /** * @Author ling.quan * @Date 2022/2/17 16:52 * @Desciption */ @Slf4j @Component public class JwtCredentialsMatcher implements CredentialsMatcher { /** * JwtCredentialsMatcher只需验证JwtToken内容是否合法 */ @Override public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) { String token = authenticationToken.getCredentials().toString(); String username = authenticationToken.getPrincipal().toString(); try { String newToken = JwtFilter.tokenMap.get(MD5.create().digestHex(token)); String usernameFromToken = JwtTokenUtil.getUserNameFromToken(newToken); if (Objects.equals(username, usernameFromToken)) { return true; } else { return false; } /*Algorithm algorithm = Algorithm.HMAC256(JWTUtils.secret); JWTVerifier verifier = JWT.require(algorithm).withClaim("username", username).build(); verifier.verify(token); return true;*/ } catch (Exception e){ log.error(e.getMessage()); } return false; } }