kongzy
2023-09-22 3124f3a5b7f45d043b228829b6b3a2e541b31574
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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;
    }
 
}