Skip to content

Commit 3f1fed6

Browse files
committed
hotfix: jwt 검증과정에서 인증정보가 있으면 채우고, 없어도 예외를 발생시키지 않고 진행하게 변경
1 parent 39b890d commit 3f1fed6

1 file changed

Lines changed: 33 additions & 21 deletions

File tree

src/main/java/WishPool/Be/security/filter/OAuth2JwtAuthFilter.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import jakarta.servlet.http.HttpServletRequest;
88
import jakarta.servlet.http.HttpServletResponse;
99
import lombok.RequiredArgsConstructor;
10+
import lombok.extern.slf4j.Slf4j;
1011
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
1112
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
1213
import org.springframework.security.core.Authentication;
@@ -20,6 +21,7 @@
2021
import java.util.List;
2122

2223
@Component
24+
@Slf4j
2325
@RequiredArgsConstructor
2426
public class OAuth2JwtAuthFilter extends OncePerRequestFilter {
2527
private final JwtUtil jwtUtil;
@@ -38,33 +40,43 @@ protected boolean shouldNotFilter(HttpServletRequest request) throws ServletExce
3840
|| path.startsWith("/swagger-ui")
3941
|| path.startsWith("/v3/api-docs")
4042
|| path.startsWith("/api/test")
41-
|| path.startsWith("/contact")
42-
|| path.startsWith("/wishpools")
43-
|| path.startsWith("/wishpools/");
43+
|| path.startsWith("/contact");
44+
4445
}
4546

4647
@Override
4748
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
4849
String atc = request.getHeader("Authorization");
49-
if (!StringUtils.hasText(atc) || !atc.startsWith("Bearer ")) {
50-
throw new AuthenticationCredentialsNotFoundException("유효하지 않은 jwt 토큰이거나, 토큰이 존재하지 않습니다.");
51-
}
52-
String accessToken = atc.substring(7);
50+
// [수정] 1. 토큰이 존재하고 "Bearer "로 시작하는 경우에만 인증 절차를 시도합니다.
51+
if (StringUtils.hasText(atc) && atc.startsWith("Bearer ")) {
52+
String accessToken = atc.substring(7);
53+
try {
54+
// [수정] 2. 토큰이 유효한 경우(verifyAccessToken 통과)에만 SecurityContext에 인증 정보를 등록합니다.
55+
if (jwtUtil.verifyAccessToken(accessToken)) {
56+
log.info("여기 걸림");
57+
String userId = jwtUtil.getId(accessToken);
58+
String role = jwtUtil.getRole(accessToken);
59+
String name = jwtUtil.getName(accessToken);
60+
61+
// SecurityContext에 등록할 User 객체를 만들어줍니다.
62+
SecurityUserDto userDto = SecurityUserDto.builder()
63+
.userId(Long.valueOf(userId))
64+
.role(role)
65+
.name(name)
66+
.build();
67+
68+
// SecurityContext에 인증 객체를 등록해줍니다.
69+
Authentication auth = getAuthentication(userDto);
70+
SecurityContextHolder.getContext().setAuthentication(auth);
71+
}
72+
// 'verifyAccessToken'이 false를 반환하면 (유효하지 않음)
73+
// 이 블록을 그냥 건너뛰고 익명 사용자로 진행됩니다.
5374

54-
// AccessToken의 값이 있고, 유효한 경우에 진행한다.
55-
if (jwtUtil.verifyAccessToken(accessToken)) {
56-
String userId = jwtUtil.getId(accessToken);
57-
String role = jwtUtil.getRole(accessToken);
58-
String name = jwtUtil.getName(accessToken);
59-
// SecurityContext에 등록할 User 객체를 만들어준다.
60-
SecurityUserDto userDto = SecurityUserDto.builder()
61-
.userId(Long.valueOf(userId))
62-
.role(role)
63-
.name(name)
64-
.build();
65-
// SecurityContext에 인증 객체를 등록해준다.
66-
Authentication auth = getAuthentication(userDto);
67-
SecurityContextHolder.getContext().setAuthentication(auth);
75+
} catch (Exception e) {
76+
// [수정] 3. 토큰 검증 중 예외(예: 만료, 서명 오류)가 발생해도 요청을 막지 않고 로그만 남깁니다.
77+
// 요청은 익명 사용자로 계속 진행됩니다.
78+
log.warn("JWT 토큰 검증 오류 (요청은 익명으로 계속): {}", e.getMessage());
79+
}
6880
}
6981
filterChain.doFilter(request, response);
7082
}

0 commit comments

Comments
 (0)