Skip to content

Commit dcba09e

Browse files
authored
Merge pull request #398 from JECT-Study/feat/343-feat-jwtexceptionhandlerfilter
feat: JWT 인증 필터에서 액세스 토큰 및 검증 토큰 처리 로직 개선
2 parents 9516df2 + 25e3d2d commit dcba09e

1 file changed

Lines changed: 30 additions & 14 deletions

File tree

src/main/java/org/ject/support/common/security/jwt/JwtAuthenticationFilter.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.ject.support.common.security.jwt;
22

33
import jakarta.servlet.FilterChain;
4+
import jakarta.servlet.http.Cookie;
45
import jakarta.servlet.http.HttpServletRequest;
56
import jakarta.servlet.http.HttpServletResponse;
67
import lombok.RequiredArgsConstructor;
@@ -40,33 +41,40 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
4041
@Override
4142
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) {
4243

43-
String accessToken = jwtTokenProvider.resolveAccessToken(request);
44-
4544
try {
45+
// =========================
46+
// 1. Access Token 처리 (선택 인증)
47+
// =========================
48+
String accessToken = jwtTokenProvider.resolveAccessToken(request);
49+
4650
if (accessToken != null) {
47-
if (!jwtTokenProvider.validateToken(accessToken)) {
48-
throw new AuthException(AuthErrorCode.INVALID_TOKEN);
51+
if (jwtTokenProvider.validateToken(accessToken)) {
52+
Authentication auth = jwtTokenProvider.getAuthenticationByToken(accessToken);
53+
SecurityContextHolder.getContext().setAuthentication(auth);
54+
} else {
55+
clearAuthCookie(response, "accessToken");
56+
SecurityContextHolder.clearContext();
4957
}
50-
Authentication auth = jwtTokenProvider.getAuthenticationByToken(accessToken);
51-
SecurityContextHolder.getContext().setAuthentication(auth);
52-
chain.doFilter(request, response);
53-
return;
5458
}
5559

56-
String verificationToken = jwtTokenProvider.resolveVerificationToken(request);
60+
// =========================
61+
// 2. Verification Token 처리 (의도적 인증)
62+
// =========================
63+
String verificationToken =
64+
jwtTokenProvider.resolveVerificationToken(request);
65+
5766
if (verificationToken != null) {
5867
if (!jwtTokenProvider.validateToken(verificationToken)) {
68+
// verification token은 실패 시 에러가 맞음
5969
throw new AuthException(AuthErrorCode.INVALID_TOKEN);
6070
}
61-
// verification 토큰에서 이메일 추출
71+
6272
String email = jwtTokenProvider.extractEmailFromVerificationToken(verificationToken);
73+
6374
Authentication auth = createVerificationAuthentication(email);
6475
SecurityContextHolder.getContext().setAuthentication(auth);
65-
chain.doFilter(request, response);
66-
return;
6776
}
6877

69-
// 두 토큰 모두 없으면 인증 없이 진행 (익명 요청)
7078
chain.doFilter(request, response);
7179

7280
} catch (Exception e) {
@@ -87,5 +95,13 @@ private Authentication createVerificationAuthentication(String email) {
8795
return new UsernamePasswordAuthenticationToken(
8896
userDetails, "", authorities);
8997
}
90-
}
9198

99+
private void clearAuthCookie(HttpServletResponse response, String cookieName) {
100+
Cookie cookie = new Cookie(cookieName, null);
101+
cookie.setMaxAge(0);
102+
cookie.setPath("/");
103+
cookie.setHttpOnly(true);
104+
cookie.setSecure(true);
105+
response.addCookie(cookie);
106+
}
107+
}

0 commit comments

Comments
 (0)