-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJWTDecoder.java
More file actions
114 lines (89 loc) · 3.14 KB
/
JWTDecoder.java
File metadata and controls
114 lines (89 loc) · 3.14 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
package com.orderservice.ctrl;
import java.util.Base64;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JWTDecoder {
private static final ObjectMapper objectMapper = new ObjectMapper();
public static void main(String[] args) {
String jwtToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
String[] parts = jwtToken.split("\\.");
if (parts.length == 3) {
String header = decodeBase64(parts[0]);
String payload = decodeBase64(parts[1]);
String signature = parts[2];
Header headerObj= decodePayload( header,Header.class);
Payload payloadObj=decodePayload( payload,Payload.class );
System.out.println(headerObj+"\n"+payloadObj);
System.out.println("Signature: " + signature);
} else {
System.out.println("Invalid JWT token format.");
}
}
private static String decodeBase64(String encodedString) {
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
return new String(decodedBytes);
}
public static <T> T decodePayload(String payloadJson, Class<T> valueType) {
try {
return objectMapper.readValue(payloadJson, valueType);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Failed to decode payload JSON.", e);
}
}
static class Header {
private String alg;
private String typ;
// Getters and setters
public String getAlg() {
return alg;
}
public void setAlg(String alg) {
this.alg = alg;
}
public String getTyp() {
return typ;
}
public void setTyp(String typ) {
this.typ = typ;
}
@Override
public String toString() {
return "Header{" +
"alg='" + alg + '\'' +
", typ='" + typ + '\'' +
'}';
}
}
static class Payload {
private String sub;
private String name;
private long iat;
// Getters and setters
public String getSub() {
return sub;
}
public void setSub(String sub) {
this.sub = sub;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getIat() {
return iat;
}
public void setIat(long iat) {
this.iat = iat;
}
@Override
public String toString() {
return "Payload{" +
"sub='" + sub + '\'' +
", name='" + name + '\'' +
", iat=" + iat +
'}';
}
}
}