44import clap .server .adapter .outbound .jwt .access .AccessTokenClaimKeys ;
55import clap .server .application .port .outbound .auth .ForbiddenTokenPort ;
66import clap .server .application .port .outbound .auth .JwtProvider ;
7- import clap .server .exception .AuthException ;
87import clap .server .exception .JwtException ;
98import clap .server .exception .code .AuthErrorCode ;
109import io .jsonwebtoken .Claims ;
2423import org .springframework .security .web .access .AccessDeniedHandler ;
2524import org .springframework .security .web .authentication .WebAuthenticationDetailsSource ;
2625import org .springframework .stereotype .Component ;
26+ import org .springframework .util .AntPathMatcher ;
2727import org .springframework .util .StringUtils ;
2828import org .springframework .web .filter .OncePerRequestFilter ;
2929
3030import java .io .IOException ;
3131import java .util .Arrays ;
32+ import java .util .stream .Stream ;
3233
3334import static clap .server .adapter .inbound .security .WebSecurityUrl .*;
3435
@@ -42,13 +43,22 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
4243 private final AccessDeniedHandler accessDeniedHandler ;
4344 private final ForbiddenTokenPort forbiddenTokenPort ;
4445
46+ public static final String [] PUBLIC_ENDPOINTS = Stream .of (
47+ HEALTH_CHECK_ENDPOINT ,
48+ READ_ONLY_PUBLIC_ENDPOINTS ,
49+ SWAGGER_ENDPOINTS
50+ ).flatMap (Arrays ::stream ).toArray (String []::new );
51+
52+ public static final String [] ANONYMOUS_ENDPOINTS = {LOGIN_ENDPOINT , REISSUANCE_ENDPOINT };
53+
4554 @ Override
4655 protected void doFilterInternal (
4756 @ NotNull HttpServletRequest request ,
4857 @ NotNull HttpServletResponse response ,
4958 @ NotNull FilterChain filterChain
5059 ) throws ServletException , IOException {
5160 try {
61+
5262 if (isAnonymousRequest (request )) {
5363 filterChain .doFilter (request , response );
5464 return ;
@@ -66,10 +76,19 @@ protected void doFilterInternal(
6676 }
6777
6878 private boolean isAnonymousRequest (HttpServletRequest request ) {
69- String accessToken = request .getHeader (HttpHeaders .AUTHORIZATION );
70- return accessToken == null ;
79+ boolean isAnonymousURI = Arrays .stream (ANONYMOUS_ENDPOINTS )
80+ .anyMatch (endpoint -> new AntPathMatcher ().match (endpoint , request .getRequestURI ()));
81+ boolean isAnonymous = request .getHeader (HttpHeaders .AUTHORIZATION ) == null ;
82+ return isAnonymousURI && isAnonymous ;
7183 }
7284
85+ @ Override
86+ protected boolean shouldNotFilter (HttpServletRequest request ) throws ServletException {
87+ return Arrays .stream (PUBLIC_ENDPOINTS )
88+ .anyMatch (endpoint -> new AntPathMatcher ().match (endpoint , request .getRequestURI ()));
89+ }
90+
91+
7392 private String resolveAccessToken (
7493 HttpServletRequest request
7594 ) throws ServletException {
@@ -106,6 +125,8 @@ private String resolveAccessToken(
106125 }
107126
108127
128+
129+
109130 private boolean isTemporaryTokenAllowed (String requestUrl ) {
110131 return requestUrl .equals (TEMPORARY_TOKEN_ALLOWED_ENDPOINT );
111132 }
0 commit comments