Skip to content

Commit 0056a34

Browse files
committed
refactor: 토큰 타입을 위한 변수 category -> token 으로 변경
1 parent 06e986a commit 0056a34

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/main/java/com/example/log4u/common/oauth2/jwt/JwtAuthenticationFilter.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ protected void doFilterInternal(
7575
// 토큰 만료 확인 , 만료 시 다음 필터로 넘기지 않음(재발급 필요)
7676
try {
7777
System.out.println("만료확인");
78-
System.out.println("category : " + jwtUtil.getCategory(accessToken));
78+
System.out.println("token type : " + jwtUtil.getTokenType(accessToken));
7979
System.out.println("userId : " + jwtUtil.getUserId(accessToken));
8080
System.out.println("role : " + jwtUtil.getRole(accessToken));
8181
jwtUtil.isExpired(accessToken);
@@ -88,17 +88,16 @@ protected void doFilterInternal(
8888
}
8989

9090
// 토큰이 access인지 확인 (발급 시 페이로드에 명시)
91-
String category = jwtUtil.getCategory(accessToken);
91+
String tokenType = jwtUtil.getTokenType(accessToken);
9292

9393
// 이상한 값일 경우
94-
if (!category.equals("access")) {
94+
if (!tokenType.equals("access")) {
9595
PrintWriter writer = response.getWriter();
9696
writer.print("토큰이 만료되었습니다.");
9797
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
9898
return;
9999
}
100100

101-
102101
// userId, role
103102
Long userId = jwtUtil.getUserId(accessToken);
104103

src/main/java/com/example/log4u/common/oauth2/jwt/JwtUtil.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
public class JwtUtil {
1717

1818
private final SecretKey secretKey;
19-
19+
private static final String USER_ID_KEY = "userId";
20+
private static final String TOKEN_TYPE_KEY = "token";
21+
2022
public JwtUtil(@Value("${jwt.secret}") String secret) {
2123
secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8),
2224
Jwts.SIG.HS256.key().build().getAlgorithm());
@@ -29,9 +31,9 @@ public Long getUserId(String token) {
2931
.build()
3032
.parseSignedClaims(token)
3133
.getPayload()
32-
.get("userId", Long.class);
34+
.get(USER_ID_KEY, Long.class);
3335
} catch(ExpiredJwtException ex){
34-
return ex.getClaims().get("userId", Long.class);
36+
return ex.getClaims().get(USER_ID_KEY, Long.class);
3537
}
3638
}
3739

@@ -48,16 +50,16 @@ public String getRole(String token) {
4850
}
4951
}
5052

51-
public String getCategory(String token) {
53+
public String getTokenType(String token) {
5254
try {
5355
return Jwts.parser()
5456
.verifyWith(secretKey)
5557
.build()
5658
.parseSignedClaims(token)
5759
.getPayload()
58-
.get("category", String.class);
60+
.get(TOKEN_TYPE_KEY, String.class);
5961
}catch(ExpiredJwtException ex){
60-
return ex.getClaims().get("category", String.class);
62+
return ex.getClaims().get(TOKEN_TYPE_KEY, String.class);
6163
}
6264
}
6365

@@ -71,10 +73,10 @@ public Boolean isExpired(String token) {
7173
.before(new Date());
7274
}
7375

74-
public String createJwt(String category, Long userId, String role, Long expiredMs) {
76+
public String createJwt(String tokenType, Long userId, String role, Long expiredMs) {
7577
return Jwts.builder()
76-
.claim("category", category)
77-
.claim("userId", userId)
78+
.claim(TOKEN_TYPE_KEY, tokenType)
79+
.claim(USER_ID_KEY, userId)
7880
.claim("role", role)
7981
.issuedAt(new Date(System.currentTimeMillis()))
8082
.expiration(new Date(System.currentTimeMillis() + expiredMs*1000))

0 commit comments

Comments
 (0)