Skip to content

Commit 1aadb68

Browse files
committed
feat(accessToken): cookie의 accessToken 추출 가능
- 쿠키와 헤더 모두에서 토큰을 추출할 수 있음. - 쿠키에서 토큰을 초기화하지 못하면 헤더에서 토큰을 찾음. - 헤더에서 토큰을 찾는 이유는 스웨거, 디버깅. - accessToken 리팩토링 - 쿠키 NPE 예방.
1 parent 4837aa7 commit 1aadb68

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
package com.somemore.auth.jwt.domain;
22

33
public record EncodedToken(String value) {
4+
5+
public boolean isUninitialized() {
6+
return value == null
7+
|| value.isEmpty()
8+
|| value.equals("UNINITIALIZED");
9+
}
10+
11+
public EncodedToken removePrefix(String prefix) {
12+
if (this.value.startsWith(prefix)) {
13+
return new EncodedToken(this.value.substring(prefix.length()));
14+
}
15+
return this;
16+
}
417
}

src/main/java/com/somemore/auth/jwt/filter/JwtAuthFilter.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import com.somemore.auth.authentication.JwtAuthenticationToken;
44
import com.somemore.auth.jwt.domain.EncodedToken;
5+
import com.somemore.auth.jwt.domain.TokenType;
56
import com.somemore.auth.jwt.exception.JwtErrorType;
67
import com.somemore.auth.jwt.exception.JwtException;
78
import com.somemore.auth.jwt.usecase.JwtUseCase;
89
import io.jsonwebtoken.Claims;
910
import jakarta.servlet.FilterChain;
1011
import jakarta.servlet.ServletException;
12+
import jakarta.servlet.http.Cookie;
1113
import jakarta.servlet.http.HttpServletRequest;
1214
import jakarta.servlet.http.HttpServletResponse;
1315
import lombok.RequiredArgsConstructor;
@@ -19,7 +21,9 @@
1921
import org.springframework.web.filter.OncePerRequestFilter;
2022

2123
import java.io.IOException;
24+
import java.util.Arrays;
2225
import java.util.List;
26+
import java.util.Objects;
2327

2428
@RequiredArgsConstructor
2529
@Slf4j
@@ -30,11 +34,11 @@ public class JwtAuthFilter extends OncePerRequestFilter {
3034

3135
@Override
3236
protected boolean shouldNotFilter(HttpServletRequest request) {
33-
String token = request.getHeader("Authorization");
37+
EncodedToken accessToken = getAccessToken(request);
3438
String path = request.getRequestURI();
3539

36-
return token == null
37-
|| token.isEmpty()
40+
return accessToken == null
41+
|| accessToken.isUninitialized()
3842
|| path.equals("/api/center/sign-in");
3943
}
4044

@@ -54,17 +58,33 @@ protected void doFilterInternal(HttpServletRequest request,
5458
}
5559

5660
private EncodedToken getAccessToken(HttpServletRequest request) {
57-
String accessToken = request.getHeader("Authorization");
58-
if (accessToken == null || accessToken.isEmpty()) {
61+
EncodedToken accessToken = findAccessTokenFromCookie(request);
62+
63+
if (accessToken.isUninitialized()) {
64+
accessToken = new EncodedToken(request.getHeader("Authorization"));
65+
}
66+
67+
if (accessToken.isUninitialized()) {
5968
throw new JwtException(JwtErrorType.MISSING_TOKEN);
6069
}
6170

62-
String tokenPrefix = "Bearer ";
63-
if (accessToken.startsWith(tokenPrefix)) {
64-
return new EncodedToken(accessToken.substring(tokenPrefix.length()));
71+
String prefix = "Bearer ";
72+
return accessToken.removePrefix(prefix);
73+
}
74+
75+
private EncodedToken findAccessTokenFromCookie(HttpServletRequest request) {
76+
Cookie[] cookies = request.getCookies();
77+
if (cookies == null) {
78+
return new EncodedToken("UNINITIALIZED");
6579
}
6680

67-
return new EncodedToken(accessToken);
81+
return Arrays.stream(cookies)
82+
.filter(Objects::nonNull)
83+
.filter(cookie -> cookie.getName().equals(TokenType.ACCESS.name()))
84+
.map(Cookie::getValue)
85+
.findFirst()
86+
.map(EncodedToken::new)
87+
.orElse(new EncodedToken("UNINITIALIZED"));
6888
}
6989

7090
private JwtAuthenticationToken createAuthenticationToken(Claims claims,

0 commit comments

Comments
 (0)