반응형
pom.xml
implementation("io.jsonwebtoken:jjwt:0.12.3")
SecretKey
SecretKey key = new SecretKeySpec(
"key는-256-bits-이상이어야함".getBytes(StandardCharsets.UTF_8),
Jwts.SIG.HS256.key().build().getAlgorithm()
);
Encoding
String jwt = Jwts.builder()
.signWith(key)
.expiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))
.subject("student")
.claim("name", "john")
.claim("age", 25)
.compact();
System.out.println(jwt);
eyJhbGciOiJIUzM4NCJ9.eyJleHAiOjE1OTkwNTg1ODIsInN1YiI6InN0dWRlbnQiLCJuYW1lIjoiam9obiIsImFnZSI6MjV9.zqkbBnFdS8LSjlYfUWypDVnRTiPNmk7WSTbAc9II1Twq6UQVQgwn9QTqvjLXnLP-
Decoding
String jwt = "eyJhbGciOiJIUzM4NCJ9.eyJleHAiOjE1OTkwNTg1ODIsInN1YiI6InN0dWRlbnQiLCJuYW1lIjoiam9obiIsImFnZSI6MjV9.zqkbBnFdS8LSjlYfUWypDVnRTiPNmk7WSTbAc9II1Twq6UQVQgwn9QTqvjLXnLP-";
Claims claims = Jwts.parser()
.verifyWith(key)
.build()
.parseSignedClaims(jwt)
.getPayload();
System.out.println(claims.getSubject());
System.out.println(claims.get("name"));
System.out.println(claims.get("age"));
System.out.println(claims.get("email"));
student
john
25
null
반응형
'Development > Java' 카테고리의 다른 글
[Java] Selenium (0) | 2020.12.28 |
---|---|
[Java] Apache HttpClient (0) | 2020.12.28 |
[Java] Markdown 파싱 (0) | 2020.12.28 |
[Java] VisualVM 사용하기 (0) | 2019.09.30 |
[Java] JDK 버전별 신규 기능 (0) | 2019.04.21 |