Confused about how to handle CORS OPTIONS preflight requests

后端 未结 2 529
星月不相逢
星月不相逢 2020-12-01 04:58

I\'m new to working with Cross Origin Resource Sharing and trying to get my webapp to respond to CORS requests. My webapp is a Spring 3.2 app running on Tomcat 7.0.42.

2条回答
  •  误落风尘
    2020-12-01 05:12

    I sat down and debugged through the org.apache.catalina.filters.CorsFilter to figure out why the request was being forbidden. Hopefully this can help someone out in the future.

    According to the W3 CORS Spec Section 6.2 Preflight Requests, the preflight must reject the request if any header submitted does not match the allowed headers.

    The default configuration for the CorsFilter cors.allowed.headers (as is yours) does not include the Authorization header that is submitted with the request.

    I updated the cors.allowed.headers filter setting to accept the authorization header and the preflight request is now successful.

    
      CorsFilter
      org.apache.catalina.filters.CorsFilter
        
            cors.allowed.headers
            Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization
             
    
    

    Of course, I'm not sure why the authorization header is not by default allowed by the CORS filter.

提交回复
热议问题