Created
May 31, 2017 11:26
-
-
Save sauthieg/3736375cc8de86965fe5dcb6702739c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (request.method == 'OPTIONS') { | |
/** | |
* Supplies a response to a CORS preflight request. | |
* | |
* Example response: | |
* | |
* HTTP/1.1 200 OK | |
* Access-Control-Allow-Origin: http://app.example.com:8081 | |
* Access-Control-Allow-Methods: POST | |
* Access-Control-Allow-Headers: Authorization | |
* Access-Control-Allow-Credentials: true | |
* Access-Control-Max-Age: 3600 | |
*/ | |
def origin = request.headers['Origin']?.firstValue | |
def response = new Response(Status.OK) | |
// Browsers sending a cross-origin request from a file might have Origin: null. | |
response.headers.put("Access-Control-Allow-Origin", origin) | |
request.headers['Access-Control-Request-Method']?.values.each() { | |
response.headers.add("Access-Control-Allow-Methods", it) | |
} | |
request.headers['Access-Control-Request-Headers']?.values.each() { | |
response.headers.add("Access-Control-Allow-Headers", it) | |
} | |
response.headers.put("Access-Control-Allow-Credentials", "true") | |
response.headers.put("Access-Control-Max-Age", "3600") | |
return response | |
} | |
return next.handle(context, request) | |
.thenOnResult { response -> | |
if (!response.status.isServerError()) { | |
def headers = [ | |
"Access-Control-Allow-Origin": request.headers['Origin']?.firstValue, | |
"Access-Control-Allow-Credentials": "true", | |
"Access-Control-Expose-Headers": "WWW-Authenticate" | |
] | |
response.headers.addAll(headers) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment