CORS enabled but response for preflight has invalid HTTP status code 404 when POSTing JSON

Thanks but getting 405 error,after the above config changes. Finally it works after adding below code in web api Global.asax file protected void Application_BeginRequest(Object sender, EventArgs e) { //HttpContext.Current.Response.AddHeader(“Access-Control-Allow-Origin”, “*”); if (HttpContext.Current.Request.HttpMethod == “OPTIONS”) { HttpContext.Current.Response.AddHeader(“Cache-Control”, “no-cache”); HttpContext.Current.Response.AddHeader(“Access-Control-Allow-Methods”, “GET, POST”); HttpContext.Current.Response.AddHeader(“Access-Control-Allow-Headers”, “Content-Type, Accept”); HttpContext.Current.Response.AddHeader(“Access-Control-Max-Age”, “1728000”); HttpContext.Current.Response.End(); } }

Enabling CORS globally in Spring Boot

You could indeed define your own Filter as you mentioned in your answer. Spring already has such a CorsFilter already though, so you don’t have to create one yourself. Just register it as a bean and it should work: @Bean public CorsFilter corsFilter() { final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); final CorsConfiguration config = new … Read more

Confused about how to handle CORS OPTIONS preflight requests

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 … Read more

Cross-Origin Resource Sharing with Spring Security

I was able to do this by extending UsernamePasswordAuthenticationFilter… my code is in Groovy, hope that’s OK: public class CorsAwareAuthenticationFilter extends UsernamePasswordAuthenticationFilter { static final String ORIGIN = ‘Origin’ @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response){ if (request.getHeader(ORIGIN)) { String origin = request.getHeader(ORIGIN) response.addHeader(‘Access-Control-Allow-Origin’, origin) response.addHeader(‘Access-Control-Allow-Methods’, ‘GET, POST, PUT, DELETE’) response.addHeader(‘Access-Control-Allow-Credentials’, ‘true’) response.addHeader(‘Access-Control-Allow-Headers’, request.getHeader(‘Access-Control-Request-Headers’)) } … Read more

CORS-enabled server not denying requests

CORS configuration on its own isn’t going to cause a server to deny requests. You can’t cause server-side blocking of requests just through CORS configuration. The only thing servers do differently when you configure CORS support is just to send the Access-Control-Allow-Origin response header and other CORS response headers. That’s it. Actual enforcement of cross-origin … Read more

Access to fetch at https://accounts.google.com/o/oauth2/v2/auth has been blocked by CORS

The authentication flow must happen in a visible browsing context, not with a fetch request. In other words: You must navigate the current tab to (or open a new tab at) http://localhost:8000/api/mail/login, the tab will then be redirected to https://accounts.google.com/o/oauth2/v2/auth?… and this page becomes visible. Now the user must interact with that page to choose/confirm … Read more