Skip to content

Instantly share code, notes, and snippets.

@kurron
Last active October 10, 2016 12:57
Show Gist options
  • Save kurron/c0f7a4b20139435966757ae53b1222e5 to your computer and use it in GitHub Desktop.
Save kurron/c0f7a4b20139435966757ae53b1222e5 to your computer and use it in GitHub Desktop.
Naive Spring Boot CORS Support
// global CORS handler that lets anything frm anywhere in
@Bean
WebMvcConfigurer corsConfigurer() {
new WebMvcConfigurerAdapter() {
@Override
void addCorsMappings( CorsRegistry registry ) {
registry.addMapping( '/**' ).allowedOrigins( '*' ).allowedMethods( 'GET', 'POST', 'DELETE', 'PUT', 'OPTIONS' )
}
}
}
You can test it by hitting an endpoint and you should see CORS headers in the traffic.
http --verbose OPTIONS http://192.168.1.64:8080/descriptor/application Origin:nowhere.com Access-Control-Request-Method:DELETE
OPTIONS /descriptor/application HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: DELETE
Connection: keep-alive
Content-Length: 0
Host: 192.168.1.64:8080
Origin: nowhere.com
User-Agent: HTTPie/0.9.6
HTTP/1.1 200
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET,POST,DELETE,PUT,OPTIONS
Access-Control-Allow-Origin: nowhere.com
Access-Control-Max-Age: 1800
Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH
Content-Length: 0
Date: Sun, 09 Oct 2016 16:54:48 GMT
Vary: Origin
X-Application-Context: jvm-guy-spring-boot-project:8080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment