xiejun
2024-11-01 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.core.jwt;
 
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springblade.core.jwt.props.JwtProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
 
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.concurrent.TimeUnit;
 
/**
 * Jwt工具类
 *
 * @author Chill
 */
public class JwtUtil {
 
    /**
     * token基础配置
     */
    public static String BEARER = "bearer";
    public static Integer AUTH_LENGTH = 7;
 
    /**
     * token保存至redis的key
     */
    private static final String REFRESH_TOKEN_CACHE = "blade:refreshToken";
    private static final String TOKEN_CACHE = "blade:token";
    private static final String TOKEN_KEY = "token:state:";
 
    /**
     * jwt配置
     */
    private static JwtProperties jwtProperties;
 
    /**
     * redis工具
     */
    private static RedisTemplate<String, Object> redisTemplate;
 
    public static JwtProperties getJwtProperties() {
        return jwtProperties;
    }
 
    public static void setJwtProperties(JwtProperties properties) {
        if (JwtUtil.jwtProperties == null) {
            JwtUtil.jwtProperties = properties;
        }
    }
 
    public static RedisTemplate<String, Object> getRedisTemplate() {
        return redisTemplate;
    }
 
    public static void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        if (JwtUtil.redisTemplate == null) {
            JwtUtil.redisTemplate = redisTemplate;
        }
    }
 
    /**
     * 签名加密
     */
    public static String getBase64Security() {
        return Base64.getEncoder().encodeToString(getJwtProperties().getSignKey().getBytes(StandardCharsets.UTF_8));
    }
 
    /**
     * 获取请求传递的token串
     *
     * @param auth token
     * @return String
     */
    public static String getToken(String auth) {
        if ((auth != null) && (auth.length() > AUTH_LENGTH)) {
            String headStr = auth.substring(0, 6).toLowerCase();
            if (headStr.compareTo(BEARER) == 0) {
                auth = auth.substring(7);
            }
            return auth;
        }
        return null;
    }
 
    /**
     * 解析jsonWebToken
     *
     * @param jsonWebToken token串
     * @return Claims
     */
    public static Claims parseJWT(String jsonWebToken) {
        try {
            return Jwts.parserBuilder()
                .setSigningKey(Base64.getDecoder().decode(getBase64Security())).build()
                .parseClaimsJws(jsonWebToken).getBody();
        } catch (Exception ex) {
            return null;
        }
    }
 
    /**
     * 获取保存在redis的accessToken
     *
     * @param tenantId    租户id
     * @param userId      用户id
     * @param accessToken accessToken
     * @return accessToken
     */
    public static String getAccessToken(String tenantId, String userId, String accessToken) {
        return String.valueOf(getRedisTemplate().opsForValue().get(getAccessTokenKey(tenantId, userId, accessToken)));
    }
 
 
    /**
     * 添加accessToken至redis
     *
     * @param tenantId    租户id
     * @param userId      用户id
     * @param accessToken accessToken
     * @param expire      过期时间
     */
    public static void addAccessToken(String tenantId, String userId, String accessToken, int expire) {
        getRedisTemplate().delete(getAccessTokenKey(tenantId, userId, accessToken));
        getRedisTemplate().opsForValue().set(getAccessTokenKey(tenantId, userId, accessToken), accessToken, expire, TimeUnit.SECONDS);
    }
 
    /**
     * 删除保存在redis的accessToken
     *
     * @param tenantId 租户id
     * @param userId   用户id
     */
    public static void removeAccessToken(String tenantId, String userId) {
        removeAccessToken(tenantId, userId, null);
    }
 
    /**
     * 删除保存在redis的accessToken
     *
     * @param tenantId    租户id
     * @param userId      用户id
     * @param accessToken accessToken
     */
    public static void removeAccessToken(String tenantId, String userId, String accessToken) {
        getRedisTemplate().delete(getAccessTokenKey(tenantId, userId, accessToken));
    }
 
    /**
     * 获取accessToken索引
     *
     * @param tenantId    租户id
     * @param userId      用户id
     * @param accessToken accessToken
     * @return token索引
     */
    public static String getAccessTokenKey(String tenantId, String userId, String accessToken) {
        String key = tenantId.concat(":").concat(TOKEN_CACHE).concat("::").concat(TOKEN_KEY);
        if (getJwtProperties().getSingle() || StringUtils.isEmpty(accessToken)) {
            return key.concat(userId);
        } else {
            return key.concat(accessToken);
        }
    }
 
    /**
     * 获取保存在redis的refreshToken
     *
     * @param tenantId     租户id
     * @param userId       用户id
     * @param refreshToken refreshToken
     * @return accessToken
     */
    public static String getRefreshToken(String tenantId, String userId, String refreshToken) {
        return String.valueOf(getRedisTemplate().opsForValue().get(getRefreshTokenKey(tenantId, userId)));
    }
 
    /**
     * 添加refreshToken至redis
     *
     * @param tenantId     租户id
     * @param userId       用户id
     * @param refreshToken refreshToken
     * @param expire       过期时间
     */
    public static void addRefreshToken(String tenantId, String userId, String refreshToken, int expire) {
        getRedisTemplate().delete(getRefreshTokenKey(tenantId, userId));
        getRedisTemplate().opsForValue().set(getRefreshTokenKey(tenantId, userId), refreshToken, expire, TimeUnit.SECONDS);
    }
 
    /**
     * 删除保存在refreshToken的token
     *
     * @param tenantId 租户id
     * @param userId   用户id
     */
    public static void removeRefreshToken(String tenantId, String userId) {
        getRedisTemplate().delete(getRefreshTokenKey(tenantId, userId));
    }
 
    /**
     * 获取refreshToken索引
     *
     * @param tenantId 租户id
     * @param userId   用户id
     * @return token索引
     */
    public static String getRefreshTokenKey(String tenantId, String userId) {
        return tenantId.concat(":").concat(REFRESH_TOKEN_CACHE).concat("::").concat(TOKEN_KEY).concat(userId);
    }
 
}