Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails CORS not enabled because no origin

I have a grails 2.2.4 application. I wanted to enable CORS So I installed cors plugin by having the following line in build config.

plugins {
    runtime ':cors:1.1.8'
}

Then in the config.groovy

cors.headers = ['Access-Control-Allow-Origin': '*']

But after this when I run the application, CORS in not enabled. So I debugged the CORS plugin. The issue seems to be in CorsFilter class in the following method

private boolean checkOrigin(HttpServletRequest req, HttpServletResponse resp) {
    String origin = req.getHeader("Origin");
    if (origin == null) {
        //no origin; per W3C spec, terminate further processing for both preflight and actual requests
        return false;
    }

The origin parameter in the above line is always null as the request does not have the parameter 'Origin'. Is there something i'm doing wrong? I'm not looking for the answer which says add a manual header with the name "Origin" since that is not exactly a proper fix

I'm quite new to CORS so appriciate the help.

like image 513
Visahan Avatar asked Nov 01 '16 09:11

Visahan


1 Answers

In addition to Access-Control-Allow-Origin, and in addition to setting the Origin header on request, you probably need to specify these response headers as well:

  • Access-Control-Allow-Headers: accept
  • Access-Control-Allow-Headers: origin
  • Access-Control-Allow-Headers: content-type
  • Access-Control-Allow-Method: GET
  • Access-Control-Allow-Method: POST

Also make sure you respond to HTTP OPTIONS requests with these headers and a blank 200 OK response.

like image 190
Seán Hayes Avatar answered Sep 26 '22 03:09

Seán Hayes