@@ -37,23 +37,57 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
3737 HttpServletResponse response = (HttpServletResponse ) servletResponse ;
3838
3939 String origin = request .getHeader ("Origin" );
40- if (origin != null && isOriginAllowed (origin )) {
41- response .setHeader ("Access-Control-Allow-Origin" , origin );
42- response .setHeader ("Access-Control-Allow-Methods" , "GET, POST, PUT, DELETE, OPTIONS" );
43- response .setHeader ("Access-Control-Allow-Headers" , "Authorization, Content-Type, Accept, Jwttoken" );
44- response .setHeader ("Access-Control-Allow-Credentials" , "true" );
40+ String method = request .getMethod ();
41+ String uri = request .getRequestURI ();
42+
43+ logger .debug ("Incoming Origin: {}" , origin );
44+ logger .debug ("Request Method: {}" , method );
45+ logger .debug ("Request URI: {}" , uri );
46+ logger .debug ("Allowed Origins Configured: {}" , allowedOrigins );
47+
48+ if ("OPTIONS" .equalsIgnoreCase (method )) {
49+ if (origin == null ) {
50+ logger .warn ("BLOCKED - OPTIONS request without Origin header | Method: {} | URI: {}" , method , uri );
51+ response .sendError (HttpServletResponse .SC_FORBIDDEN , "OPTIONS request requires Origin header" );
52+ return ;
53+ }
54+ if (!isOriginAllowed (origin )) {
55+ logger .warn ("BLOCKED - Unauthorized Origin | Origin: {} | Method: {} | URI: {}" , origin , method , uri );
56+ response .sendError (HttpServletResponse .SC_FORBIDDEN , "Origin not allowed" );
57+ return ;
58+ }
4559 } else {
46- logger .warn ("Origin [{}] is NOT allowed. CORS headers NOT added." , origin );
47- }
48-
49- if ("OPTIONS" .equalsIgnoreCase (request .getMethod ())) {
50- logger .info ("OPTIONS request - skipping JWT validation" );
51- response .setStatus (HttpServletResponse .SC_OK );
52- return ;
60+ // For non-OPTIONS requests, validate origin if present
61+ if (origin != null && !isOriginAllowed (origin )) {
62+ logger .warn ("BLOCKED - Unauthorized Origin | Origin: {} | Method: {} | URI: {}" , origin , method , uri );
63+ response .sendError (HttpServletResponse .SC_FORBIDDEN , "Origin not allowed" );
64+ return ;
65+ }
5366 }
5467
68+ // Determine request path/context for later checks
5569 String path = request .getRequestURI ();
5670 String contextPath = request .getContextPath ();
71+
72+ // Set CORS headers and handle OPTIONS request only if origin is valid and allowed
73+ if (origin != null && isOriginAllowed (origin )) {
74+ addCorsHeaders (response , origin );
75+ logger .info ("Origin Validated | Origin: {} | Method: {} | URI: {}" , origin , method , uri );
76+
77+ if ("OPTIONS" .equalsIgnoreCase (method )) {
78+ // OPTIONS (preflight) - respond with full allowed methods
79+ response .setStatus (HttpServletResponse .SC_OK );
80+ return ;
81+ }
82+ } else {
83+ logger .warn ("Origin [{}] is NOT allowed. CORS headers NOT added." , origin );
84+
85+ if ("OPTIONS" .equalsIgnoreCase (method )) {
86+ response .sendError (HttpServletResponse .SC_FORBIDDEN , "Origin not allowed for OPTIONS request" );
87+ return ;
88+ }
89+ }
90+
5791 logger .info ("JwtUserIdValidationFilter invoked for path: " + path );
5892
5993 // Log cookies for debugging
@@ -70,7 +104,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
70104 }
71105
72106 // Log headers for debugging
73- logger .info ("JWT token from header: " );
107+ logger .debug ("JWT token from header: {}" , request . getHeader ( "Jwttoken" ) != null ? "present" : "not present " );
74108
75109 // Skip login and public endpoints
76110 if (path .equals (contextPath + "/user/userAuthenticate" )
@@ -132,6 +166,15 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
132166 }
133167 }
134168
169+ private void addCorsHeaders (HttpServletResponse response , String origin ) {
170+ response .setHeader ("Access-Control-Allow-Origin" , origin ); // Never use wildcard
171+ response .setHeader ("Access-Control-Allow-Methods" , "GET, POST, PUT, PATCH, DELETE, OPTIONS" );
172+ response .setHeader ("Access-Control-Allow-Headers" ,
173+ "Authorization, Content-Type, Accept, Jwttoken, serverAuthorization, ServerAuthorization, serverauthorization, Serverauthorization" );
174+ response .setHeader ("Access-Control-Allow-Credentials" , "true" );
175+ response .setHeader ("Access-Control-Max-Age" , "3600" );
176+ }
177+
135178 private boolean isOriginAllowed (String origin ) {
136179 if (origin == null || allowedOrigins == null || allowedOrigins .trim ().isEmpty ()) {
137180 logger .warn ("No allowed origins configured or origin is null" );
@@ -144,14 +187,12 @@ private boolean isOriginAllowed(String origin) {
144187 String regex = pattern
145188 .replace ("." , "\\ ." )
146189 .replace ("*" , ".*" )
147- .replace ("http://localhost:.*" , "http://localhost:\\ d+" ); // special case for wildcard port
148-
190+ .replace ("http://localhost:.*" , "http://localhost:\\ d+" );
191+
149192 boolean matched = origin .matches (regex );
150193 return matched ;
151194 });
152- }
153-
154- private boolean isMobileClient (String userAgent ) {
195+ } private boolean isMobileClient (String userAgent ) {
155196 if (userAgent == null )
156197 return false ;
157198 userAgent = userAgent .toLowerCase ();
0 commit comments