Skip to content

Commit 86b285b

Browse files
committed
fix: refine JwtAuthenticationFilter to properly handle auth endpoints and CORS preflight requests
1 parent dd0a089 commit 86b285b

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

server/src/main/java/com/incial/crm/security/JwtAuthenticationFilter.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,18 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
2828

2929
@Override
3030
protected boolean shouldNotFilter(HttpServletRequest request) {
31-
return request.getServletPath().startsWith("/api/v1/auth/");
31+
32+
// Allow auth endpoints
33+
if (request.getServletPath().startsWith("/api/v1/auth/")) {
34+
return true;
35+
}
36+
37+
// Allow CORS preflight
38+
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
39+
return true;
40+
}
41+
42+
return false;
3243
}
3344

3445
@Override
@@ -38,12 +49,6 @@ protected void doFilterInternal(
3849
FilterChain filterChain
3950
) throws ServletException, IOException {
4051

41-
// Skip CORS preflight
42-
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
43-
filterChain.doFilter(request, response);
44-
return;
45-
}
46-
4752
String authHeader = request.getHeader("Authorization");
4853

4954
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
@@ -90,9 +95,8 @@ protected void doFilterInternal(
9095
);
9196

9297
SecurityContextHolder.getContext().setAuthentication(authentication);
93-
9498
}
9599

96100
filterChain.doFilter(request, response);
97101
}
98-
}
102+
}

0 commit comments

Comments
 (0)