JwtUtils.java
3.85 KB
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
package com.trash.garbage.utils;
import io.jsonwebtoken.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/**
* jwt 工具类
*
* @author 20412
*/
@Slf4j
public class JwtUtils {
/**
* 过期时间设置 最近 365天
*/
private final static long tokenExpiration = 365 * 24 * 60 * 60 * 1000;
/**
* jwt密钥
*/
private final static String tokenSecret = "guzijian";
/**
* 创建token
*/
public static String createToken(String id, String tel) {
String token = Jwts.builder()
// 说明
.setSubject("AUTH-USER")
// 过期时间
.setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
// 加入认证信息
.claim("tel", tel)
.claim("userId", id)
// 加密方式 jwt 密钥方便解密
.signWith(SignatureAlgorithm.HS256, tokenSecret)
// 压缩方式
.compressWith(CompressionCodecs.GZIP)
// 构建契约
.compact();
return token;
}
/**
* 通过token获取user id
*/
public static String getUserId(String token) {
Claims claims = null;
try {
Jws<Claims> claimsJws = Jwts.parser()
.setSigningKey(tokenSecret)
.parseClaimsJws(token);
claims = claimsJws.getBody();
} catch (Exception e) {
return null;
}
return (String) claims.get("userId");
}
/**
* 获取用户id
*/
public static String getUserId(HttpServletRequest request) {
String token = request.getHeader("Authority");
Claims claims = null;
try {
Jws<Claims> claimsJws = Jwts.parser()
.setSigningKey(tokenSecret)
.parseClaimsJws(token);
claims = claimsJws.getBody();
} catch (Exception e) {
throw new RuntimeException("解析失败" + e.getMessage());
}
return (String) claims.get("userId");
}
public static String getUserName(String token) {
Claims claims = null;
try {
Jws<Claims> claimsJws = Jwts.parser()
.setSigningKey(tokenSecret)
.parseClaimsJws(token);
claims = claimsJws.getBody();
} catch (Exception e) {
throw new RuntimeException("token解析失败" + e.getMessage());
}
return (String) claims.get("username");
}
public static String getUserName(HttpServletRequest request) {
String token = request.getHeader("Authority");
Claims claims = null;
try {
Jws<Claims> claimsJws = Jwts.parser()
.setSigningKey(tokenSecret)
.parseClaimsJws(token);
claims = claimsJws.getBody();
} catch (Exception e) {
throw new RuntimeException("token解析失败" + e.getMessage());
}
return (String) claims.get("username");
}
public String getUserIdByRequest(HttpServletRequest request) {
String authorization = request.getHeader("Authorization");
Claims claims = null;
try {
Jws<Claims> claimsJws = Jwts.parser()
.setSigningKey(tokenSecret)
.parseClaimsJws(authorization);
claims = claimsJws.getBody();
} catch (Exception e) {
throw new RuntimeException("token解析失败" + e.getMessage());
}
return (String) claims.get("userId");
}
// public static void main(String[] args) {
// String guzijian = createToken(new UserCustom(new User("1", "204126329@qq.com", "guzijian"), Collections.emptyList()));
// System.out.println(guzijian);
// }
}