2
2
3
3
import jakarta .servlet .FilterChain ;
4
4
import jakarta .servlet .ServletException ;
5
+ import jakarta .servlet .http .Cookie ;
5
6
import jakarta .servlet .http .HttpServletRequest ;
6
7
import jakarta .servlet .http .HttpServletResponse ;
7
8
import lombok .extern .slf4j .Slf4j ;
9
+ import org .benchmarker .security .util .MethodUrlPair ;
8
10
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
9
11
import org .springframework .security .core .context .SecurityContextHolder ;
10
12
import org .springframework .security .core .userdetails .UserDetails ;
13
+ import org .springframework .security .core .userdetails .UsernameNotFoundException ;
11
14
import org .springframework .web .filter .OncePerRequestFilter ;
12
15
13
16
import java .io .IOException ;
14
17
15
18
import static org .benchmarker .security .constant .TokenConsts .ACCESS_TOKEN_COOKIE_NAME ;
19
+ import static org .benchmarker .security .constant .URLConsts .WHITE_LIST_URLS ;
16
20
17
21
@ Slf4j
18
22
public class JwtAuthFilter extends OncePerRequestFilter {
@@ -30,18 +34,34 @@ public JwtAuthFilter(BMUserDetailsService userDetailsService,
30
34
@ Override
31
35
protected void doFilterInternal (HttpServletRequest request , HttpServletResponse response ,
32
36
FilterChain filterChain ) throws ServletException , IOException {
33
- String userId = jwtTokenProvider .validateTokenAndGetUserId (request ,
34
- ACCESS_TOKEN_COOKIE_NAME );
35
- if (userId != null ) {
36
- log .info ("userId : {}" , userId );
37
-
38
- UserDetails userDetails = userDetailsService .loadUserByUsername (userId );
39
- UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken (
40
- userDetails , null , userDetails .getAuthorities ());
41
- SecurityContextHolder .getContext ().setAuthentication (auth );
42
-
43
- log .info ("SecurityContextHolder.getContext().getAuthentication() : {}" ,
44
- SecurityContextHolder .getContext ().getAuthentication ());
37
+
38
+ for (MethodUrlPair methodUrlPair : WHITE_LIST_URLS ) {
39
+ if (methodUrlPair .getMethod ().contains (request .getMethod ()) &&
40
+ methodUrlPair .getUrl ().equals (request .getRequestURI ())) {
41
+ filterChain .doFilter (request , response );
42
+ return ;
43
+ }
44
+ }
45
+
46
+ try {
47
+ String userId = jwtTokenProvider .validateTokenAndGetUserId (request ,
48
+ ACCESS_TOKEN_COOKIE_NAME );
49
+ if (userId != null ) {
50
+ UserDetails userDetails = userDetailsService .loadUserByUsername (userId );
51
+ UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken (
52
+ userDetails , null , userDetails .getAuthorities ());
53
+ SecurityContextHolder .getContext ().setAuthentication (auth );
54
+
55
+ log .info ("SecurityContextHolder.getContext().getAuthentication() : {}" ,
56
+ SecurityContextHolder .getContext ().getAuthentication ());
57
+ }
58
+ } catch (UsernameNotFoundException ex ) {
59
+ Cookie cookie = new Cookie (ACCESS_TOKEN_COOKIE_NAME , null );
60
+ cookie .setPath ("/" );
61
+ cookie .setMaxAge (0 );
62
+ response .addCookie (cookie );
63
+ response .sendRedirect ("/" );
64
+ return ;
45
65
}
46
66
47
67
filterChain .doFilter (request , response );
0 commit comments