@@ -12,33 +12,25 @@ public class CORSHelper {
1212
1313 private static String CORS_ALLOWED_ORIGIN = "CORS_ALLOW_ORIGIN" ;
1414 private static String CORS_MAX_AGE_SECONDS = "86400" ;
15-
15+ private static String PREFLIGHT_REQUEST = "OPTIONS" ;
1616
1717 public static boolean corsSupportEnabled () {
1818 return getAllowedOrigin () != null ;
1919 }
2020
21- public static HashMap <String , String > getCORSHeaders (Map <String , List <String >> headers ) {
22- String corsAllowedOrigin = getAllowedOrigin ();
23- if (corsAllowedOrigin == null ) return null ;
21+ public static HashMap <String , String > getCORSHeaders (String httpMethod , Map <String , List <String >> headers ) {
22+ if (getAllowedOrigin () == null ) {
23+ return null ;
24+ }
2425
2526 String requestedMethod = getHeaderValue (REQUEST_METHOD_HEADER_NAME , headers );
2627 String requestedHeaders = getHeaderValue (REQUEST_HEADERS_HEADER_NAME , headers );
27- if (requestedMethod == null ) {
28- return null ;
29- }
3028
31- return corsHeaders (corsAllowedOrigin , requestedMethod , requestedHeaders );
29+ return corsHeaders (httpMethod , requestedMethod , requestedHeaders );
3230 }
3331
34- public static HashMap <String , String > getCORSHeaders (String requestedMethod , String requestedHeaders ) {
35- String corsAllowedOrigin = getAllowedOrigin ();
36-
37- if (corsAllowedOrigin == null || requestedMethod == null ) {
38- return null ;
39- }
40-
41- return corsHeaders (corsAllowedOrigin , requestedMethod , requestedHeaders );
32+ public static HashMap <String , String > getCORSHeaders (String httpMethod , String requestedMethod , String requestedHeaders ) {
33+ return corsHeaders (httpMethod , requestedMethod , requestedHeaders );
4234 }
4335
4436 private static String getAllowedOrigin () {
@@ -49,15 +41,26 @@ private static String getAllowedOrigin() {
4941 return corsAllowedOrigin ;
5042 }
5143
52- private static HashMap <String , String > corsHeaders (String corsAllowedOrigin , String requestedMethod , String requestedHeaders ) {
44+ private static HashMap <String , String > corsHeaders (String httpMethodName , String requestedMethod , String requestedHeaders ) {
45+ String corsAllowedOrigin = getAllowedOrigin ();
46+ if (corsAllowedOrigin == null ) {
47+ return null ;
48+ }
49+
50+ boolean isPreflightRequest = httpMethodName .equalsIgnoreCase (PREFLIGHT_REQUEST );
51+
5352 HashMap <String , String > corsHeaders = new HashMap <>();
5453 corsHeaders .put ("Access-Control-Allow-Origin" , corsAllowedOrigin );
5554 corsHeaders .put ("Access-Control-Allow-Credentials" , "true" );
56- if (requestedHeaders != null && !requestedHeaders .isEmpty ()) {
55+ corsHeaders .put ("Access-Control-Max-Age" , CORS_MAX_AGE_SECONDS );
56+
57+ if (isPreflightRequest && requestedHeaders != null && !requestedHeaders .isEmpty ()) {
5758 corsHeaders .put ("Access-Control-Allow-Headers" , requestedHeaders );
5859 }
59- corsHeaders .put ("Access-Control-Allow-Methods" , requestedMethod );
60- corsHeaders .put ("Access-Control-Max-Age" , CORS_MAX_AGE_SECONDS );
60+ if (isPreflightRequest && requestedMethod != null && !requestedMethod .isEmpty ()) {
61+ corsHeaders .put ("Access-Control-Allow-Methods" , requestedMethod );
62+ }
63+
6164 return corsHeaders ;
6265 }
6366
0 commit comments