-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathJwtUtil.java
More file actions
33 lines (27 loc) · 851 Bytes
/
JwtUtil.java
File metadata and controls
33 lines (27 loc) · 851 Bytes
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
package com.codesoom.assignment.utils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.security.Key;
@Component
public class JwtUtil {
private final Key key;
public JwtUtil(@Value("${jwt.secret}") String secret) {
this.key = Keys.hmacShaKeyFor(secret.getBytes());
}
public String encode(Long userId) {
return Jwts.builder()
.claim("userId", userId)
.signWith(key)
.compact();
}
public Claims decode(String token) {
return Jwts.parserBuilder()
.setSigningKey(key)
.build()
.parseClaimsJws(token)
.getBody();
}
}