提交 | 用户 | age
|
826b66
|
1 |
package com.hx.util; |
C |
2 |
|
|
3 |
import java.util.Base64; |
|
4 |
import java.util.Date; |
|
5 |
|
|
6 |
import javax.crypto.SecretKey; |
|
7 |
import javax.crypto.spec.SecretKeySpec; |
|
8 |
|
|
9 |
import io.jsonwebtoken.Claims; |
|
10 |
import io.jsonwebtoken.ExpiredJwtException; |
|
11 |
import io.jsonwebtoken.JwtBuilder; |
|
12 |
import io.jsonwebtoken.Jwts; |
|
13 |
import io.jsonwebtoken.SignatureAlgorithm; |
|
14 |
import io.jsonwebtoken.SignatureException; |
|
15 |
|
|
16 |
/** |
|
17 |
* 生成token (登录,或者其他) |
|
18 |
* @author chenjiahe |
|
19 |
* @Data 20200609 |
|
20 |
*/ |
|
21 |
public class JwtTool { |
|
22 |
|
|
23 |
/** |
|
24 |
* 签发JWT |
|
25 |
* @param id 标识 |
|
26 |
* @param subject 可以是JSON数据 尽可能少 |
|
27 |
* @param ttlMillis 有效时间长度(秒),为空你认证时间 |
|
28 |
* @return String |
|
29 |
* |
|
30 |
*/ |
|
31 |
public static String createJWT(String id, String subject, long ttlMillis) { |
|
32 |
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; |
|
33 |
long nowMillis = System.currentTimeMillis(); |
|
34 |
Date now = new Date(nowMillis); |
|
35 |
SecretKey secretKey = generalKey(); |
|
36 |
JwtBuilder builder = Jwts.builder() |
|
37 |
.setId(id) |
|
38 |
.setSubject(subject) // 主题 |
|
39 |
.setIssuer("user") // 签发者 |
|
40 |
.setIssuedAt(now) // 签发时间 |
|
41 |
.signWith(signatureAlgorithm, secretKey); // 签名算法以及密匙 |
|
42 |
if (SimpleTool.checkNotNull(ttlMillis)&&ttlMillis >= 0) { |
|
43 |
long expMillis = nowMillis + ttlMillis*1000; |
|
44 |
Date expDate = new Date(expMillis); |
|
45 |
builder.setExpiration(expDate); // 过期时间 |
|
46 |
} |
|
47 |
return builder.compact(); |
|
48 |
} |
|
49 |
/** |
|
50 |
* 验证JWT |
|
51 |
* @param jwtStr |
|
52 |
* @return JwtConstant |
|
53 |
*/ |
|
54 |
public static JwtConstant validateJWT(String jwtStr) { |
|
55 |
JwtConstant checkResult = new JwtConstant(); |
|
56 |
Claims claims = null; |
|
57 |
try { |
|
58 |
claims = parseJWT(jwtStr); |
|
59 |
checkResult.setSuccess(true); |
|
60 |
checkResult.setClaims(claims); |
|
61 |
} catch (ExpiredJwtException e) { |
|
62 |
checkResult.setErrCode(JwtConstant.JWT_ERRCODE_EXPIRE); |
|
63 |
checkResult.setSuccess(false); |
|
64 |
} catch (SignatureException e) { |
|
65 |
checkResult.setErrCode(JwtConstant.JWT_ERRCODE_FAIL); |
|
66 |
checkResult.setSuccess(false); |
|
67 |
} catch (Exception e) { |
|
68 |
checkResult.setErrCode(JwtConstant.JWT_ERRCODE_FAIL); |
|
69 |
checkResult.setSuccess(false); |
|
70 |
} |
|
71 |
return checkResult; |
|
72 |
} |
|
73 |
|
|
74 |
public static SecretKey generalKey() { |
|
75 |
byte[] encodedKey = Base64.getDecoder().decode(JwtConstant.JWT_SECERT); |
|
76 |
SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES"); |
|
77 |
return key; |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* |
|
82 |
* 解析JWT字符串 |
|
83 |
* @param jwt |
|
84 |
* @return |
|
85 |
* @throws Exception |
|
86 |
*/ |
|
87 |
public static Claims parseJWT(String jwt) throws Exception { |
|
88 |
SecretKey secretKey = generalKey(); |
|
89 |
return Jwts.parser() |
|
90 |
.setSigningKey(secretKey) |
|
91 |
.parseClaimsJws(jwt) |
|
92 |
.getBody(); |
|
93 |
} |
|
94 |
} |