55import com .permitseoul .permitserver .domain .auth .core .exception .AuthWrongJwtException ;
66import com .permitseoul .permitserver .domain .auth .core .jwt .CookieExtractor ;
77import com .permitseoul .permitserver .domain .auth .core .jwt .JwtProvider ;
8- import com .permitseoul .permitserver .global .Constants ;
98import com .permitseoul .permitserver .global .domain .CookieType ;
109import com .permitseoul .permitserver .global .exception .FilterException ;
1110import com .permitseoul .permitserver .global .response .code .ErrorCode ;
1211import jakarta .servlet .FilterChain ;
1312import jakarta .servlet .ServletException ;
14- import jakarta .servlet .http .Cookie ;
1513import jakarta .servlet .http .HttpServletRequest ;
1614import jakarta .servlet .http .HttpServletResponse ;
1715import lombok .NonNull ;
2624import org .springframework .web .filter .OncePerRequestFilter ;
2725
2826import java .io .IOException ;
29- import java .util .Enumeration ;
3027import java .util .List ;
3128
32-
3329@ RequiredArgsConstructor
3430@ Slf4j
3531public class JwtAuthenticationFilter extends OncePerRequestFilter {
3632 private final JwtProvider jwtProvider ;
37- private final List <String > whiteURIList ;
33+ private final List <String > whiteURIListNotUsingToken ;
34+ private final List <String > whiteURIListUsingToken ;
3835 private final AntPathMatcher pathMatcher = new AntPathMatcher ();
39- private static final String REISSUE_URI = "/api/users/reissue" ;
40- private static final String LOGIN_URI = "/api/users/login" ;
4136 private static final String USER_ID_MDC_KEY = "user_id" ;
4237 private static final String ANONYMOUS_USER_ID = "anonymous" ;
4338
39+ @ Override
40+ protected boolean shouldNotFilter (@ NonNull final HttpServletRequest request ) {
41+ return whiteURIListNotUsingToken .stream ()
42+ .anyMatch (pattern -> pathMatcher .match (pattern , request .getRequestURI ()));
43+ }
44+
4445 @ Override
4546 protected void doFilterInternal (@ NonNull final HttpServletRequest request ,
46- @ NonNull final HttpServletResponse response ,
47- @ NonNull final FilterChain filterChain ) throws ServletException , IOException {
47+ @ NonNull final HttpServletResponse response ,
48+ @ NonNull final FilterChain filterChain ) throws ServletException , IOException {
4849 final String uri = request .getRequestURI ();
4950 try {
5051 MDC .put (USER_ID_MDC_KEY , ANONYMOUS_USER_ID );
5152
52- if (isHealthCheckUri (uri ) || isLoginOrReissue (uri )) {
53- filterChain .doFilter (request , response );
54- return ;
55- }
5653 setAuthentication (request );
5754 filterChain .doFilter (request , response );
5855 } catch (AuthCookieException e ) {
59- if (isWhiteListUrl (uri )) {
60- SecurityContextHolder .getContext ().setAuthentication (new UsernamePasswordAuthenticationToken (null , null , null ));
56+ if (isUsingTokenUrl (uri )) {
57+ SecurityContextHolder .getContext ().setAuthentication (
58+ new UsernamePasswordAuthenticationToken (null , null , null ));
6159 filterChain .doFilter (request , response );
6260 } else {
6361 throw new FilterException (ErrorCode .NOT_FOUND_AT_COOKIE );
@@ -69,14 +67,12 @@ protected void doFilterInternal(@NonNull final HttpServletRequest request,
6967 } catch (ServletException | IOException e ) {
7068 log .error ("[JWT Filter] unexpected error. ua={}" ,
7169 request .getHeader ("User-Agent" ),
72- e
73- );
70+ e );
7471 throw new FilterException (ErrorCode .INTERNAL_FILTER_ERROR );
7572 } catch (Exception e ) {
7673 log .error ("[JWT Filter] unexpected error. ua={}" ,
7774 request .getHeader ("User-Agent" ),
78- e
79- );
75+ e );
8076 throw new FilterException (ErrorCode .INTERNAL_SERVER_ERROR );
8177 } finally {
8278 MDC .remove (USER_ID_MDC_KEY );
@@ -93,16 +89,7 @@ private void setAuthentication(final HttpServletRequest request) {
9389 new UsernamePasswordAuthenticationToken (userId , null , authorities ));
9490 }
9591
96- private boolean isWhiteListUrl (final String requestURI ) {
97- return whiteURIList .stream ().anyMatch (pattern -> pathMatcher .match (pattern , requestURI ));
98- }
99-
100- private boolean isHealthCheckUri (final String uri ) {
101- return pathMatcher .match (Constants .HEALTH_CHECK_URL , uri );
102- }
103-
104- private boolean isLoginOrReissue (final String uri ) {
105- return pathMatcher .match (LOGIN_URI , uri )
106- || pathMatcher .match (REISSUE_URI , uri );
92+ private boolean isUsingTokenUrl (final String requestURI ) {
93+ return whiteURIListUsingToken .stream ().anyMatch (pattern -> pathMatcher .match (pattern , requestURI ));
10794 }
10895}
0 commit comments