Skip to content

Commit 0ab521e

Browse files
committed
Update DSW011-JWT-token
1 parent 6b142e5 commit 0ab521e

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

handsonDoc/DSW011-JWT-token.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,128 @@ public class JWTUtil {
150150
}
151151
```
152152

153+
### 更新JWT工具类-JWTUtil:
154+
代码:com/jinwei/{项目根目录}/JWTUtil.java
155+
156+
更新:添加了JSON配置参数文件的读取
157+
158+
```java
159+
package com.jinwei.S8_mongotemplate;
160+
161+
import com.google.gson.Gson;
162+
import io.jsonwebtoken.*;
163+
import lombok.Data;
164+
import javax.crypto.SecretKey;
165+
import java.io.IOException;
166+
import java.util.Date;
167+
import java.util.UUID;
168+
import io.jsonwebtoken.Jwts;
169+
import io.jsonwebtoken.security.Keys;
170+
import io.jsonwebtoken.security.SecureDigestAlgorithm;
171+
172+
import java.time.Instant;
173+
174+
@Data
175+
public class JWTUtil {
176+
// 获取JWT配置-对象-GSON
177+
private static String jsonFile = "jwt-authorization.json";
178+
static JsonUtil jsonUtil = new JsonUtil();
179+
static String jsonStr;
180+
181+
static {
182+
try {
183+
jsonStr = jsonUtil.readJSON(jsonFile);
184+
} catch (IOException e) {
185+
throw new RuntimeException(e);
186+
}
187+
}
188+
189+
static Gson gson = new Gson();
190+
static JWTConfig jwtConfig = gson.fromJson(jsonStr, JWTConfig.class);
191+
192+
// 设置token访问的过期时间-单位/分种
193+
private static final int MINUTE = 60;
194+
public static final int ACCESS_EXPIRE = 1 * MINUTE;
195+
196+
// 设置秘钥的加密算法
197+
private final static SecureDigestAlgorithm<SecretKey, SecretKey> ALGORITHM = Jwts.SIG.HS512;
198+
// 生成私钥,只能在服务器端保存
199+
// 使用Jwts.SIG.HS256 算法需要SECRET至少32位
200+
// 使用Jwts.SIG.HS512 算法需要SECRET至少64位
201+
202+
// 设置密钥字符串
203+
private final static String SECRET = jwtConfig.secret;
204+
// 使用加密算法加密密钥字符串
205+
public final static SecretKey KEY = Keys.hmacShaKeyFor(SECRET.getBytes());
206+
207+
// 设置jwt签发者
208+
private final static String JWT_ISS = jwtConfig.iss;
209+
210+
// 设置jwt主题
211+
private final static String SUBJECT = jwtConfig.subject;
212+
213+
public JWTUtil() throws IOException {
214+
}
215+
216+
/* 常用声明:
217+
iss: jwt签发者-签发方
218+
sub: jwt主题-面向用户
219+
aud: jwt接受者-接受方
220+
exp: jwt过期时间-过期时间必须要大于签发时间
221+
nbf: jwt开始启用时间-定义在什么时间之前-jwt不可用的
222+
iat: jwt签发时间-过期时间必须要大于签发时间
223+
jti: jwt唯一身份标识-主要用来作为一次性token-回避重放攻击
224+
*/
225+
public String genJWTToken(String inputStr) {
226+
227+
// 生成令牌id-UUID.randomUUID()-随机
228+
String uuid = UUID.randomUUID().toString();
229+
Date exprireDate = Date.from(Instant.now().plusSeconds(ACCESS_EXPIRE));
230+
231+
return Jwts.builder()
232+
// 设置头部信息-header
233+
.header()
234+
.add("type", "JWT")
235+
.add("algo", "HS512")
236+
.and()
237+
// 设置负载信息-payload
238+
.claim("username", inputStr)
239+
// 设置令牌ID
240+
.id(uuid)
241+
// 设置过期日期
242+
.expiration(exprireDate)
243+
// 设置签发时间
244+
.issuedAt(new Date())
245+
// 设置主题
246+
.subject(SUBJECT)
247+
// 设置签发者
248+
.issuer(JWT_ISS)
249+
// 设置签名
250+
.signWith(KEY, ALGORITHM)
251+
.compact();
252+
}
253+
254+
// 解析token-claim
255+
public Jws<Claims> parseClaim(String token) {
256+
return Jwts.parser()
257+
.verifyWith(KEY) // 必须持有相同的KEY才能解析
258+
.build()
259+
.parseSignedClaims(token);
260+
}
261+
262+
// 解析头部-Header
263+
public JwsHeader parseHeader(String token) {
264+
return parseClaim(token).getHeader();
265+
}
266+
267+
// 解析负载-Payload
268+
public Claims parsePayload(String token) {
269+
return parseClaim(token).getPayload();
270+
}
271+
272+
}
273+
```
274+
153275
## 测试主文件
154276

155277
```java

0 commit comments

Comments
 (0)