在Eclipse中部署但不在Tomcat中部署时,rest服务可以正常工作

时间:2016-08-04 18:18:01

标签: eclipse spring rest tomcat xmlhttprequest

我使用Spring实现了休息Web服务。当我在Eclipse中将它作为Spring Boot应用程序部署时,它可以工作。但是,当我在同一台机器上的Tomcat 7中部署它时,它不起作用。错误消息如下:

XMLHttpRequest cannot load http://localhost:8080/ristoreService/oauth/token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8081' is therefore not allowed access.

我的CORS过滤器如下所示:

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CORSFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        response.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8081");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me, "
                + "Origin,Access-Control-Request-Method, Access-Control-Request-Headers, Authorization");
        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            chain.doFilter(req, res);
        }
    }

如果我发表评论response.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:8081");,我仍然会收到同样的错误。即使我在Eclipse中部署,它也不会在没有这条线的情况下工作。为什么在同一个ip上的不同环境下部署的行为不同?

编辑: 我用休息客户端测试人员测试了网址http://localhost:8080/ristoreService/oauth/token" CocoaRestClient"因此我编写了一个显然不存在的URL http://localhost:8080/xxxxx并在UI(angularjs)中运行它并再次得到CORS错误。我认为错误有点误导,它毕竟是404.但是为什么在Tomcat的webapps下使用名称ristoreService.war成功部署战争时却没有发现它?

3 个答案:

答案 0 :(得分:1)

尝试使用FilterRegistrationBean。在Java Config中看起来像这样:

@Bean
public FilterRegistrationBean authorizationFilter(){
    FilterRegistrationBean filterRegBean = new FilterRegistrationBean();
    filterRegBean.setFilter(authorizationFilter);
    List<String> urlPatterns = new ArrayList<String>();
    urlPatterns.add("/v1/*");
    filterRegBean.setUrlPatterns(urlPatterns);
    return filterRegBean;
}

您没有使用Spring Boot的CORS功能的原因是什么?它已经开箱即用,您只需配置即可。您可以像这样全局启用它:

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**").allowedOrigins("*");
            }
        };
    }

答案 1 :(得分:0)

根据How to deploy Spring Boot application,我必须主要申请延长SpringBootServletInitializer。一旦我添加了它,它就可以了。

答案 2 :(得分:0)

为了解决CORS问题,我使用了@CrossOrigin。我没有实现自己的CORS过滤器。春天已经提供了很少addition solutions for CORS issue

如果您只需要过滤器,可以这样使用它:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.addFilterBefore(yourFilter);
    ...
}