|
6 | 6 | import jakarta.servlet.ServletException; |
7 | 7 | import jakarta.servlet.http.HttpServletRequest; |
8 | 8 | import jakarta.servlet.http.HttpServletResponse; |
9 | | -import lombok.RequiredArgsConstructor; |
10 | 9 | import lombok.extern.slf4j.Slf4j; |
11 | 10 | import org.springframework.http.HttpStatus; |
12 | 11 | import org.springframework.security.core.context.SecurityContextHolder; |
13 | | -import org.springframework.stereotype.Component; |
| 12 | +import org.springframework.util.AntPathMatcher; |
14 | 13 | import org.springframework.web.filter.OncePerRequestFilter; |
15 | 14 | import org.terning.terningserver.common.security.jwt.application.JwtUserIdExtractor; |
16 | 15 | import org.terning.terningserver.common.security.jwt.auth.UserAuthentication; |
|
19 | 18 | import org.terning.terningserver.common.util.IpAddressUtil; |
20 | 19 |
|
21 | 20 | import java.io.IOException; |
| 21 | +import java.util.List; |
22 | 22 | import java.util.Optional; |
23 | 23 |
|
24 | 24 | import static org.springframework.http.HttpHeaders.AUTHORIZATION; |
25 | 25 |
|
26 | | -@Component |
27 | | -@RequiredArgsConstructor |
28 | 26 | @Slf4j |
29 | 27 | public class JwtAuthenticationFilter extends OncePerRequestFilter { |
30 | 28 |
|
| 29 | + private static final AntPathMatcher antPathMatcher = new AntPathMatcher(); |
| 30 | + |
31 | 31 | private final JwtUserIdExtractor jwtUserIdExtractor; |
32 | 32 | private final RateLimitingService rateLimitingService; |
| 33 | + private final List<String> authWhitelist; |
| 34 | + |
| 35 | + public JwtAuthenticationFilter( |
| 36 | + JwtUserIdExtractor jwtUserIdExtractor, |
| 37 | + RateLimitingService rateLimitingService, |
| 38 | + List<String> authWhitelist |
| 39 | + ) { |
| 40 | + this.jwtUserIdExtractor = jwtUserIdExtractor; |
| 41 | + this.rateLimitingService = rateLimitingService; |
| 42 | + this.authWhitelist = authWhitelist; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected boolean shouldNotFilter(HttpServletRequest request) { |
| 47 | + String requestURI = request.getRequestURI(); |
| 48 | + for (String pattern : this.authWhitelist) { |
| 49 | + if (antPathMatcher.match(pattern, requestURI)) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + } |
| 53 | + return false; |
| 54 | + } |
33 | 55 |
|
34 | 56 | @Override |
35 | 57 | protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) |
|
0 commit comments