MCPcopy Create free account
hub / github.com/bansh2eBreak/SpringVulnBoot-backend / JwtTest

Class JwtTest

src/test/java/icu/secnotes/test/JwtTest.java:12–108  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10import java.util.Map;
11
12public class JwtTest {
13
14 private static String weakSignKey = "Aa123123";
15 private static Long expire = 3600000L; // 1小时
16
17 /**
18 * JWT 令牌生成方法
19 * @param claims JWT第二部分载荷,payload中存储的内容
20 * @return
21 */
22 public static String generateJwt(Map<String, Object> claims) {
23 String jwttoken = Jwts.builder()
24 .signWith(SignatureAlgorithm.HS256, weakSignKey)
25 .setClaims(claims)
26 .setExpiration(new Date(System.currentTimeMillis() + expire))
27 .compact();
28
29 return jwttoken;
30 }
31
32 /**
33 * JWT 令牌解析方法 - 使用弱密码
34 * @param jwttoken
35 * @return
36 */
37 public static Claims parseJwt(String jwttoken) {
38 Claims claims = Jwts.parser()
39 .setSigningKey(weakSignKey)
40 .parseClaimsJws(jwttoken)
41 .getBody();
42
43 return claims;
44 }
45
46 /**
47 * 解析JWT并分别展示header和payload
48 * @param jwttoken JWT令牌
49 */
50 public static void parseAndDisplayJwt(String jwttoken) {
51 try {
52 // 分割JWT的三个部分
53 String[] parts = jwttoken.split("\\.");
54 if (parts.length != 3) {
55 System.out.println("无效的JWT格式");
56 return;
57 }
58
59 // 解析header
60 String headerJson = new String(Base64.getUrlDecoder().decode(parts[0]), StandardCharsets.UTF_8);
61 System.out.println("=== JWT Header ===");
62 System.out.println(headerJson);
63 System.out.println();
64
65 // 解析payload
66 String payloadJson = new String(Base64.getUrlDecoder().decode(parts[1]), StandardCharsets.UTF_8);
67 System.out.println("=== JWT Payload ===");
68 System.out.println(payloadJson);
69 System.out.println();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected