@@ -37,28 +37,46 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
3737 HttpServletResponse response = (HttpServletResponse ) servletResponse ;
3838
3939 String origin = request .getHeader ("Origin" );
40+ String method = request .getMethod ();
41+ String uri = request .getRequestURI ();
4042
4143 logger .debug ("Incoming Origin: {}" , origin );
4244 logger .debug ("Allowed Origins Configured: {}" , allowedOrigins );
45+ if ("OPTIONS" .equalsIgnoreCase (method )) {
46+ if (origin == null ) {
47+ logger .warn ("BLOCKED - OPTIONS request without Origin header | Method: {} | URI: {}" , method , uri );
48+ response .sendError (HttpServletResponse .SC_FORBIDDEN , "OPTIONS request requires Origin header" );
49+ return ;
50+ }
51+ if (!isOriginAllowed (origin )) {
52+ logger .warn ("BLOCKED - Unauthorized Origin | Origin: {} | Method: {} | URI: {}" , origin , method , uri );
53+ response .sendError (HttpServletResponse .SC_FORBIDDEN , "Origin not allowed" );
54+ return ;
55+ }
56+ } else {
57+ // For non-OPTIONS requests, validate origin if present
58+ if (origin != null && !isOriginAllowed (origin )) {
59+ logger .warn ("BLOCKED - Unauthorized Origin | Origin: {} | Method: {} | URI: {}" , origin , method , uri );
60+ response .sendError (HttpServletResponse .SC_FORBIDDEN , "Origin not allowed" );
61+ return ;
62+ }
63+ }
64+
65+ String path = request .getRequestURI ();
66+ String contextPath = request .getContextPath ();
4367
4468 if (origin != null && isOriginAllowed (origin )) {
45- response .setHeader ("Access-Control-Allow-Origin" , origin );
46- response .setHeader ("Access-Control-Allow-Methods" , "GET, POST, PUT, DELETE, OPTIONS" );
47- response .setHeader ("Access-Control-Allow-Headers" , "Authorization, Content-Type, Accept, Jwttoken" );
48- response . setHeader ( "Vary" , "Origin " );
69+ response .setHeader ("Access-Control-Allow-Origin" , origin ); // Never use wildcard
70+ response .setHeader ("Access-Control-Allow-Methods" , "GET, POST, PUT, PATCH, DELETE, OPTIONS" );
71+ response .setHeader ("Access-Control-Allow-Headers" ,
72+ "Authorization, Content-Type, Accept, Jwttoken, serverAuthorization, ServerAuthorization, serverauthorization, Serverauthorization " );
4973 response .setHeader ("Access-Control-Allow-Credentials" , "true" );
74+ response .setHeader ("Access-Control-Max-Age" , "3600" );
75+ logger .info ("Origin Validated | Origin: {} | Method: {} | URI: {}" , origin , method , uri );
5076 } else {
5177 logger .warn ("Origin [{}] is NOT allowed. CORS headers NOT added." , origin );
5278 }
5379
54- if ("OPTIONS" .equalsIgnoreCase (request .getMethod ())) {
55- logger .info ("OPTIONS request - skipping JWT validation" );
56- response .setStatus (HttpServletResponse .SC_OK );
57- return ;
58- }
59-
60- String path = request .getRequestURI ();
61- String contextPath = request .getContextPath ();
6280 logger .info ("JwtUserIdValidationFilter invoked for path: " + path );
6381
6482 // Log cookies for debugging
@@ -142,7 +160,7 @@ private boolean isOriginAllowed(String origin) {
142160 String regex = pattern
143161 .replace ("." , "\\ ." )
144162 .replace ("*" , ".*" )
145- .replace ("http://localhost:.*" , "http://localhost:\\ d+" ); // special case for wildcard port
163+ .replace ("http://localhost:.*" , "http://localhost:\\ d+" );
146164
147165 boolean matched = origin .matches (regex );
148166 return matched ;
0 commit comments