1313import org .springframework .stereotype .Component ;
1414import org .springframework .web .filter .OncePerRequestFilter ;
1515import sopt .comfit .global .constants .Constants ;
16+ import sopt .comfit .global .logging .MdcUtils ;
1617import sopt .comfit .global .security .info .JwtAuthenticationToken ;
1718import sopt .comfit .global .security .info .JwtUserInfo ;
1819import sopt .comfit .global .security .manager .JwtAuthenticationManager ;
@@ -34,32 +35,40 @@ protected void doFilterInternal(HttpServletRequest request,
3435 HttpServletResponse response ,
3536 FilterChain filterChain ) throws ServletException , IOException {
3637
38+ try {
39+ MdcUtils .generateTraceId ();
3740
38- String header = request .getHeader (Constants .PREFIX_AUTH );
39- log .info ("header:{}" ,header );
41+ String header = request .getHeader (Constants .PREFIX_AUTH );
42+ log .info ("header:{}" , header );
4043
41- if (header == null || !header .startsWith ("Bearer " )) {
42- filterChain .doFilter (request , response );
43- return ;
44- }
45- String token = HeaderUtil .refineHeader (request , Constants .PREFIX_AUTH , Constants .BEARER );
46- Claims claim = jwtUtil .validateToken (token );
47- log .info ("claim: getUserId() = {}" , claim .get (Constants .CLAIM_USER_ID , Long .class ));
44+ if (header == null || !header .startsWith ("Bearer " )) {
45+ filterChain .doFilter (request , response );
46+ return ;
47+ }
48+ String token = HeaderUtil .refineHeader (request , Constants .PREFIX_AUTH , Constants .BEARER );
49+ Claims claim = jwtUtil .validateToken (token );
50+ log .info ("claim: getUserId() = {}" , claim .get (Constants .CLAIM_USER_ID , Long .class ));
51+
52+ JwtUserInfo jwtUserInfo = JwtUserInfo .from (claim );
4853
49- JwtUserInfo jwtUserInfo = JwtUserInfo . from ( claim );
54+ MdcUtils . setUserId ( jwtUserInfo . userId () );
5055
51- JwtAuthenticationToken unAuthenticatedToken = new JwtAuthenticationToken (jwtUserInfo );
56+ JwtAuthenticationToken unAuthenticatedToken = new JwtAuthenticationToken (jwtUserInfo );
5257
53- JwtAuthenticationToken authenticatedToken = (JwtAuthenticationToken ) jwtAuthenticationManager .authenticate (unAuthenticatedToken );
58+ JwtAuthenticationToken authenticatedToken = (JwtAuthenticationToken ) jwtAuthenticationManager .authenticate (unAuthenticatedToken );
5459
55- log .info ("Authentication Successful: {}" , authenticatedToken );
60+ log .info ("Authentication Successful: {}" , authenticatedToken );
5661
57- authenticatedToken .setDetails (new WebAuthenticationDetailsSource ().buildDetails (request ));
62+ authenticatedToken .setDetails (new WebAuthenticationDetailsSource ().buildDetails (request ));
5863
59- SecurityContext securityContext = SecurityContextHolder .createEmptyContext ();
60- securityContext .setAuthentication (authenticatedToken );
61- SecurityContextHolder .setContext (securityContext );
62- filterChain .doFilter (request , response );
64+ SecurityContext securityContext = SecurityContextHolder .createEmptyContext ();
65+ securityContext .setAuthentication (authenticatedToken );
66+ SecurityContextHolder .setContext (securityContext );
67+
68+ filterChain .doFilter (request , response );
69+ } finally {
70+ MdcUtils .clear ();
71+ }
6372 }
6473
6574}
0 commit comments