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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.panzhihua.common.redis;
 
import com.panzhihua.common.utlis.JWTTokenUtil;
import com.panzhihua.common.redis.TokenManager;
import com.panzhihua.common.utlis.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 javax.annotation.Resource;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
 
/**
 * 通过Redis存储和验证token的实现类
 */
@Component
public class RedisTokenManager implements TokenManager
{
//    @Resource
    private RedisTemplate<String,String> redis;
 
//    @Resource
//    private RedisTemplate<String,Object> redis1;
 
    @Resource
    RedisTemplate<String, Object> redisTemplate;
    
    @Resource
    private RedisUtils redisUtils;
 
    @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) {
//        redis.multi(); // 开启事务
//
//        if (StringUtils.isEmpty(token)) {
//            return false;
//        }
//        // 获取Redis服务器的地址
//
//        System.out.println("Redis服务器地址: " + redisTemplate.getConnectionFactory().getConnection());
//
//        // 获取Redis服务器的端口
//        System.out.println("Redis服务器端口: " + redisTemplate.getConnectionFactory().getConnection());
//        Set<String> keys = redisTemplate.keys("*");
//
//        // 打印所有的键(key)
//        for (String key : keys) {
//            System.out.println(key);
//        }
//        Object login_user_info = redisUtils.get("login_user_info");
//        Object userId2 = redisUtils.get("08839375c14aebe729fb0fb25defdea123782abc-2");
////        String userId = redisTemplate.boundValueOps("2").get().toString();
//        Object o = redisTemplate.opsForValue().get("08839375c14aebe729fb0fb25defdea123782abc-2");
//        String s = redis.boundValueOps("2").get();
////        String s = o;
////        Object auxiliaryPoints =redisTemplate.opsForValue().get("auxiliaryPoints");
//
//        if (userId2 == null) {
//            return false;
//        }
//        redis.exec();
//        //如果验证成功,说明此用户进行了一次有效操作,延长token的过期时间
//        redis.boundValueOps(token).expire(Constant.TOKEN_EXPIRES_HOUR, TimeUnit.HOURS);
//        return true;
        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();
    }
 
}