2121import com .example .ssccwebbe .global .security .jwt .util .JwtUtil ;
2222import com .fasterxml .jackson .databind .ObjectMapper ;
2323
24+ import lombok .extern .slf4j .Slf4j ;
25+
2426// JWT 검증 필터
27+ @ Slf4j
2528public class JwtFilter extends OncePerRequestFilter {
2629
2730 // Request 헤더에서 accessToken 을 추출하여 인증, 인가를 처리함
@@ -30,13 +33,26 @@ protected void doFilterInternal(
3033 HttpServletRequest request , HttpServletResponse response , FilterChain filterChain )
3134 throws ServletException , IOException {
3235
36+ log .info (
37+ "[JwtFilter] Processing request - Method: {}, URI: {}" ,
38+ request .getMethod (),
39+ request .getRequestURI ());
40+
3341 String authorization = request .getHeader ("Authorization" );
3442 if (authorization == null ) {
43+ log .warn (
44+ "[JwtFilter] No Authorization header found for URI: {}" ,
45+ request .getRequestURI ());
3546 filterChain .doFilter (request , response );
3647 return ;
3748 }
3849
50+ log .info ("[JwtFilter] Authorization header found" );
51+
3952 if (!authorization .startsWith ("Bearer " )) {
53+ log .error (
54+ "[JwtFilter] Invalid token format - Authorization header does not start with"
55+ + " 'Bearer '" );
4056 // ApiResponse 형식으로 응답 작성
4157 response .setStatus (JwtErrorCode .INVALID_TOKEN_FORMAT .getHttpStatus ().value ());
4258 response .setContentType ("application/json;charset=UTF-8" );
@@ -47,25 +63,39 @@ protected void doFilterInternal(
4763 return ;
4864 }
4965
66+ log .info ("[JwtFilter] Token format valid (starts with 'Bearer ')" );
67+
5068 // 토큰 파싱
5169 String accessToken = authorization .split (" " )[1 ];
70+ String tokenPreview =
71+ accessToken .length () > 20 ? accessToken .substring (0 , 20 ) + "..." : accessToken ;
72+ log .info ("[JwtFilter] Extracted token: {}" , tokenPreview );
5273
5374 // 토큰 검증
75+ log .info ("[JwtFilter] Validating token..." );
5476 if (JwtUtil .isValid (accessToken , true )) {
5577
5678 String username = JwtUtil .getUsername (accessToken );
5779 String role = JwtUtil .getRole (accessToken );
5880
81+ log .info ("[JwtFilter] Token valid - Username: {}, Role: {}" , username , role );
82+
5983 List <GrantedAuthority > authorities =
6084 Collections .singletonList (new SimpleGrantedAuthority (role ));
6185
6286 Authentication auth =
6387 new UsernamePasswordAuthenticationToken (username , null , authorities );
6488 SecurityContextHolder .getContext ().setAuthentication (auth );
6589
90+ log .info (
91+ "[JwtFilter] Authentication successful for user: {} with role: {}" ,
92+ username ,
93+ role );
94+
6695 filterChain .doFilter (request , response );
6796
6897 } else {
98+ log .error ("[JwtFilter] Token validation failed - Token is invalid or expired" );
6999 // ApiResponse 형식으로 응답 작성
70100 response .setStatus (JwtErrorCode .INVALID_TOKEN .getHttpStatus ().value ());
71101 response .setContentType ("application/json;charset=UTF-8" );
0 commit comments