无关风月
2025-02-28 2f8e70ad2884d2b6b7443dfae0af11ae9cfc8b99
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package com.jilongda.common.security;
 
import com.jilongda.common.basic.Constant;
import com.jilongda.common.exception.ServiceException;
import com.jilongda.common.exception.TokenException;
import com.jilongda.common.swagger.GlobalResultEnum;
import io.jsonwebtoken.*;
import io.jsonwebtoken.impl.DefaultClaims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
 
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.*;
 
/**
 * JWT工具类
 *
 * @author Louis
 * @date Jun 29, 2019
 */
@Slf4j
public class JwtTokenUtils implements Serializable {
 
 
    /**
     * 用户名称
     */
    private static final String USERNAME = Claims.SUBJECT;
 
    /**
     * 环境信息
     */
    private static final String ENV = "env";
 
    /**
     * 用户主体信息
     */
    private static final String USERID = "userId";
    /**
     * 创建时间
     */
    private static final String CREATED = "created";
    /**
     * 权限列表
     */
    private static final String AUTHORITIES = "authorities";
 
    /**
     * JWT Token 签发机构
     */
    private static final String ISSUING_AGENCY = "Lingser Technology ISSUER";
    /**
     * JWT Token 默认的密钥
     */
    private static final String SIGNING_KEY = "Lingser Technology SIGNING_KEY";
    private static final long serialVersionUID = -576504484244573039L;
 
    /**
     * 从数据声明生成令牌
     *
     * @param payload         载荷
     * @param tokenExpireTime
     * @return
     */
    private static String generateToken(Map<String, Object> payload, long tokenExpireTime) {
        // token存在时间
        Date expirationDate = new Date(System.currentTimeMillis() + tokenExpireTime * 1000L);
        Claims claims = new DefaultClaims();
        // 签发机构
        claims.setIssuer(ISSUING_AGENCY);
        // 载荷
        claims.putAll(payload);
        //
        String token = Jwts.builder()
                .setClaims(claims)
                .setExpiration(expirationDate)
                .signWith(SignatureAlgorithm.HS512, SIGNING_KEY)
                .compact();
        return token;
    }
 
    /**
     * 生成token
     *
     * @param userName
     * @param tokenExpireTime
     * @return
     */
    public static String generateToken(String userName, long tokenExpireTime) {
        Map<String, Object> payload = new HashMap<>(2);
        payload.put(USERNAME, userName);
        payload.put(CREATED, new Date());
        return generateToken(payload, tokenExpireTime);
    }
 
    /**
     * 主要用于token不需要托管,但有多个场景的情况
     * 生成token
     *
     * @param userName
     * @param evn
     * @param tokenExpireTime
     * @return
     */
    public static String generateToken(String userName, String evn, long tokenExpireTime) {
        Map<String, Object> payload = new HashMap<>(3);
        payload.put(USERNAME, userName);
        payload.put(ENV, evn);
        payload.put(CREATED, new Date());
        return generateToken(payload, tokenExpireTime);
    }
 
    /**
     * 从令牌中获取用户名
     *
     * @param token 令牌
     * @return 用户名
     */
    public static String getUsername(String token) {
        Claims claims = getClaimsFromToken(token);
        return claims.getSubject();
    }
 
 
    /**
     * 根据请求令牌获取登录认证信息
     *
     * @param token
     * @return
     */
    public static Authentication getAuthenticatione(String token) {
        Authentication authentication = getAuthentication();
        if (StringUtils.hasLength(token)) {
            // 上下文认证为空
            if (Objects.isNull(authentication)) {
                // 上下文中Authentication为空
                Claims claims = getClaimsFromToken(token);
                if (Objects.isNull(claims)) {
                    throw new ServiceException("从令牌中获取数据声明失败");
                }
                String username = claims.getSubject();
                Object userDetails = claims.get(USERID);
                if (!StringUtils.hasLength(username)) {
                    throw new ServiceException("获取用户名失败");
                }
                // 如果需要手动续期,此项判断需要注释
                if (isTokenExpired(token)) {
                    throw new TokenException(GlobalResultEnum.TOKEN_EXPIRE.getCode(),GlobalResultEnum.TOKEN_EXPIRE.getMessage());
                }
                List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
                authentication = new UsernamePasswordAuthenticationToken(username, userDetails, authorities);
            } else {
                if (validateToken(token, getUsername())) {
                    // 如果上下文中Authentication非空,且请求令牌合法,直接返回当前登录认证信息
                    authentication = getAuthentication();
                }
            }
        }
        return authentication;
    }
 
    /**
     * 从令牌中获取数据声明
     * 解析JWT Token
     *
     * @param token 令牌
     * @return 数据声明
     */
    public static Claims getClaimsFromToken(String token) {
        try {
            // 传入令牌,判断accessToken是否存在
            Claims claims = Jwts.parser().setSigningKey(SIGNING_KEY).parseClaimsJws(token).getBody();
            return claims;
        } catch (ExpiredJwtException e) {
            log.error("Token 已过期");
            throw new TokenException(GlobalResultEnum.TOKEN_EXPIRE.getCode(),GlobalResultEnum.TOKEN_EXPIRE.getMessage());
        } catch (UnsupportedJwtException e) {
            log.error("不支持的 Token");
            throw new TokenException(GlobalResultEnum.TOKEN_PARSE_ERROR);
        } catch (MalformedJwtException e) {
            log.error("Token 无效");
            throw new TokenException(GlobalResultEnum.TOKEN_PARSE_ERROR);
        } catch (SignatureException e) {
            log.error("无效的 Token 签名");
            throw new TokenException(GlobalResultEnum.TOKEN_PARSE_ERROR);
        } catch (IllegalArgumentException e) {
            log.error("Token 参数不存在");
            throw new TokenException(GlobalResultEnum.TOKEN_PARSE_ERROR);
        } catch (TokenException e) {
            log.error("Token  已过期");
            throw new TokenException(GlobalResultEnum.TOKEN_EXPIRE.getCode(),GlobalResultEnum.TOKEN_EXPIRE.getMessage());
        } catch (Exception e) {
            log.error("token 解析失败,请尝试重新登录");
            throw new TokenException(GlobalResultEnum.TOKEN_PARSE_ERROR);
        }
 
    }
 
 
    /**
     * 验证令牌
     *
     * @param token
     * @param username
     * @return
     */
    public static Boolean validateToken(String token, String username) {
        String userName = getUsername(token);
        if (!StringUtils.hasLength(userName)) {
            throw new ServiceException(401,"请求令牌无效");
        }
        return (userName.equals(username) && !isTokenExpired(token));
    }
 
 
    /**
     * 刷新令牌,重新生成
     *
     * @param token
     * @return
     */
    public static String refreshToken(String token, long tokenExpireTime) {
        String refreshedToken;
        try {
            Claims claims = getClaimsFromToken(token);
            String userName = claims.getSubject();
            refreshedToken = generateToken(userName, tokenExpireTime);
        } catch (Exception e) {
            throw new SecurityException("刷新令牌出错!");
        }
        return refreshedToken;
    }
 
    /**
     * 刷新令牌,重新生成
     *
     * @param token
     * @param env
     * @param tokenExpireTime
     * @return
     */
    public static String refreshToken(String token, String env, long tokenExpireTime) {
        String refreshedToken;
        try {
            Claims claims = getClaimsFromToken(token);
            String userName = claims.getSubject();
            refreshedToken = generateToken(userName, env, tokenExpireTime);
        } catch (Exception e) {
            throw new SecurityException("刷新令牌出错!");
        }
        return refreshedToken;
    }
 
    /**
     * 判断令牌是否过期
     *
     * @param token 令牌
     * @return 是否过期
     */
    public static Boolean isTokenExpired(String token) {
        try {
            Claims claims = getClaimsFromToken(token);
            Date expiration = claims.getExpiration();
            return expiration.before(new Date());
        } catch (Exception e) {
            return false;
        }
    }
 
    /**
     * 获取请求token
     *
     * @param request
     * @return
     */
    public static String getToken(HttpServletRequest request) {
        String token = request.getHeader(Constant.DEF_TOKEN_HEADER);
        String asToken = request.getHeader(Constant.ALIAS_TOKEN_HEADER);
        // 如果这两个头部信息均不存在
        if (!StringUtils.hasLength(token) && !StringUtils.hasLength(asToken)) {
            throw new ServiceException("资源未认证,访问受限");
        }
        // token转换
        if (!StringUtils.hasLength(token)) {
            token = asToken;
        }
        String tokenHead = "Bearer ";
        if (token.contains(tokenHead)) {
            token = token.substring(tokenHead.length());
        }
        if (!StringUtils.hasLength(token)) {
            throw new ServiceException("访问令牌不能为空");
        }
        // base64 解码
        try {
            token = new String(Base64Utils.decode(token.getBytes()));
        } catch (IllegalArgumentException e) {
            throw new ServiceException(401,"无效的令牌");
        }
        return token;
    }
 
 
 
    /**
     * 获取当前用户名
     *
     * @return
     */
    public static String getUsername() {
        String username = null;
        Authentication authentication = getAuthentication();
        if (Objects.nonNull(authentication)) {
            Object principal = authentication.getPrincipal();
            if (Objects.nonNull(principal)) {
                username = (String) principal;
            }
        }
        return username;
    }
 
 
 
 
 
 
 
    /**
     * 获取当前用户信息id
     *
     * @return
     */
    public static Object getUserInfo() {
        Object userInfo = null;
        Authentication authentication = getAuthentication();
        if (Objects.nonNull(authentication)) {
            userInfo = authentication.getDetails();
        }
        return userInfo;
    }
 
    /**
     * 获取用户名
     *
     * @return
     */
    public static String getUsername(Authentication authentication) {
        String username = null;
        if (Objects.nonNull(authentication)) {
            Object principal = authentication.getPrincipal();
            if (Objects.nonNull(principal) && principal instanceof UserDetails) {
                username = (String) principal;
            }
        }
        return username;
    }
 
    /**
     * 获取当前登录信息
     *
     * @return
     */
    public static Authentication getAuthentication() {
        if (Objects.isNull(SecurityContextHolder.getContext())) {
            return null;
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return authentication;
    }
 
}