mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.dg.core.manager;
 
import com.dg.core.Constant;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DigestUtils;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
 
import java.util.UUID;
import java.util.concurrent.TimeUnit;
 
/**
 * 通过Redis存储和验证token的实现类
 */
@Component
public class RedisTokenManager implements TokenManager
{
    private RedisTemplate<String, String> redis;
 
    @Qualifier("redisTemplate")
    @Autowired
    public void setRedis(RedisTemplate redis) {
        this.redis = redis;
        //泛型设置成Long后必须更改对应的序列化方案
        redis.setKeySerializer(new JdkSerializationRedisSerializer());
    }
 
    @Override
    public String createToken(String userId, String userRole) {
        //使用uuid作为源token
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String token = DigestUtils.sha1DigestAsHex(uuid + userId + "e23ktjsdf") + "-" + userRole;
        //存储到redis并设置过期时间
        redis.boundValueOps(token).set(String.valueOf(userId), Constant.TOKEN_EXPIRES_HOUR, TimeUnit.HOURS);
        redis.boundValueOps(String.valueOf(userId)).set(token, Constant.TOKEN_EXPIRES_HOUR, TimeUnit.HOURS);
        return token;
    }
 
    public boolean checkToken(String token) {
        if (StringUtils.isEmpty(token)) {
            return false;
        }
        String userId = redis.boundValueOps(token).get();
        if (userId == null) {
            return false;
        }
        //如果验证成功,说明此用户进行了一次有效操作,延长token的过期时间
        redis.boundValueOps(token).expire(Constant.TOKEN_EXPIRES_HOUR, TimeUnit.HOURS);
        return true;
    }
 
 
    /**
     * 验证花城token
     * @param token
     * @return
     */
    @Override
    public boolean checkHCToken(String token) {
        if (StringUtils.isEmpty(token)) {
            return false;
        }
        // token解析
        Claims claims = JWTTokenUtil.getClaimsFromToken(token);
        if (ObjectUtils.isEmpty(claims)) {
            return false;
        }
        String userId = claims.getSubject();
        if (ObjectUtils.isEmpty(userId))
        {
            return false;
        }
        return true;
    }
 
 
    @Override
    public long getUserId(String token) {
        if (token == null) {
            return -1;
        }
        String userId = redis.boundValueOps(token).get();
        return Long.parseLong(userId);
    }
 
    /**
     * 获取花城e+的 UserId
     * @param token
     * @return
     */
    @Override
    public long getHCUserId(String token) {
        if (token == null) {
            return -1;
        }
        // token解析
        Claims claims = JWTTokenUtil.getClaimsFromToken(token);
        if (ObjectUtils.isEmpty(claims)) {
            return -1;
        }
        String userId = claims.getSubject();
        if (ObjectUtils.isEmpty(userId))
        {
            return -1;
        }
        return Long.parseLong(userId);
    }
 
    public void deleteToken(String token) {
        redis.delete(token);
    }
 
    @Override
    public String getTokenByUserId(String userId) {
        return redis.boundValueOps(userId).get();
    }
 
}