|
6 | 6 | import jakarta.servlet.http.HttpServletRequest; |
7 | 7 | import jakarta.servlet.http.HttpServletResponse; |
8 | 8 | import lombok.RequiredArgsConstructor; |
| 9 | +import org.springframework.http.HttpMethod; |
9 | 10 | import org.springframework.lang.NonNull; |
10 | 11 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
11 | 12 | import org.springframework.security.core.context.SecurityContextHolder; |
12 | 13 | import org.springframework.security.core.userdetails.UserDetails; |
13 | 14 | import org.springframework.security.core.userdetails.UserDetailsService; |
14 | 15 | import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; |
15 | 16 | import org.springframework.stereotype.Component; |
| 17 | +import org.springframework.util.AntPathMatcher; |
16 | 18 | import org.springframework.web.filter.OncePerRequestFilter; |
17 | 19 |
|
18 | 20 | import java.io.IOException; |
| 21 | +import java.util.List; |
19 | 22 |
|
20 | 23 | @Component |
21 | 24 | @RequiredArgsConstructor |
22 | 25 | public class JwtAuthenticationFilter extends OncePerRequestFilter { |
23 | 26 |
|
24 | 27 | private final JwtProvider jwtProvider; |
25 | 28 | private final UserDetailsService userDetailsService; |
| 29 | + private final AntPathMatcher antPathMatcher = new AntPathMatcher(); |
| 30 | + |
| 31 | + private static final List<String> WHITE_LIST = List.of( |
| 32 | + "/swagger-ui/**", |
| 33 | + "/v3/api-docs/**", |
| 34 | + "/swagger-resources/**", |
| 35 | + "/webjars/**", |
| 36 | + "/auth/**", |
| 37 | + "/api/members/auth/kko", |
| 38 | + "/auth/refresh" |
| 39 | + ); |
| 40 | + |
| 41 | + @Override |
| 42 | + protected boolean shouldNotFilter(HttpServletRequest request) { |
| 43 | + if (request.getMethod().equalsIgnoreCase(HttpMethod.OPTIONS.name())) { |
| 44 | + return true; |
| 45 | + } |
| 46 | + return WHITE_LIST.stream().anyMatch(white -> antPathMatcher.match(white, request.getRequestURI())); |
| 47 | + } |
26 | 48 |
|
27 | 49 | @Override |
28 | 50 | protected void doFilterInternal( |
@@ -53,7 +75,6 @@ protected void doFilterInternal( |
53 | 75 | SecurityContextHolder.getContext().setAuthentication(authToken); |
54 | 76 | } |
55 | 77 | } catch (Exception e) { |
56 | | - // Bearer 토큰이 있지만 유효하지 않은 경우, 컨텍스트를 비워서 인증되지 않은 상태로 만듭니다. |
57 | 78 | SecurityContextHolder.clearContext(); |
58 | 79 | } |
59 | 80 |
|
|
0 commit comments