-
Notifications
You must be signed in to change notification settings - Fork 563
Add support for Spring Web MVC CORS configuration mechanisms [DATAREST-573] #947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Sébastien Deleuze commented As discussed with Oliver Drotbohm, this will require Spring Data REST to depend on Spring Framework 4.2. I have detailed a possible solution for CORS support in Spring Data REST without requiring this issue to be resolved in the Stackoverflow answer. Some implementation notes : supporting Global CORS configuration should be as simple as adding |
Hendy Irawan commented +1 for this. Got this (related) issue from spring-projects/spring-boot#4029 (comment) |
Gigen Thomas commented +1 Need CORS Support for Data Rest |
Hendy Irawan commented Please fix this, and it should be promoted to "Major". The number one reason for having CORS is access to REST services from web client, so it should be easy to enable this for Spring Data REST. |
Mark Burns commented +1 I'm surprised more people have not upvoted this one |
Nathan Ward commented +1 I am looking to deploy an Angular app separately from my Spring Data REST services. |
Silvio Casamassima commented +1 I'm developing an Angular client app too. Please fix this |
Leonardo commented +1 |
Jia Wern Lim commented +1 , similarly developing an Angular app |
Patrick Hütter commented +1, i'm also developing an angular 2 app |
Kevin Vasko commented Is there a workaround for this issue at the moment? The only thing I have found is by doing this.. @Configuration
public class MyConfiguration {
@Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(0);
return bean;
}
} from https://spring.io/blog/2015/06/08/cors-support-in-spring-framework This seems to work for GET requests but preflighted requests still fail. I get this error message on a DELETE request (even though I have config.addAllowedMehtod("*"). Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access. The response had HTTP status code 403. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. Is there a way to work around this until this is included? Ended up finding a solution: import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Note this is a very simple CORS filter that is wide open.
* This would need to be locked down.
* Source: http://stackoverflow.com/questions/39565438/no-access-control-allow-origin-error-with-spring-restful-hosted-in-pivotal-web
*/
@Component
public class CORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
} |
Oliver Drotbohm commented Comments on the PR |
Mark Paluch commented PR comments addressed |
Oliver Drotbohm commented That's merged and in place. |
Bruce Edge opened DATAREST-573 and commented
There's no mechanism for using the new CorsConfiguration CORS support within spring-data-rest.
See comments in the CORS post: https://spring.io/blog/2015/06/08/cors-support-in-spring-framework
AFAICT, one needs to put the
@CrossOrigin
directive in the controller, which doesn't exist in spring-data-rest repositoriesReferenced from: pull request #233, and commits 40bb8e8, 273dac7, b223a2e, a3870ca, 7e8b137
39 votes, 32 watchers
The text was updated successfully, but these errors were encountered: