使用Angular JS,Spring Boot和Tomcat的CORS过滤器

时间:2016-03-29 11:52:29

标签: tomcat spring-boot cors jhipster

我有一个带有这个堆栈的项目。 Tomcat服务器上的Spring启动(基于maven)。在客户端(不同的域)上,Angular JS对它进行HTTP调用。

我在Spring项目中有这个

@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, PATCH");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
        response.setHeader("Access-Control-Expose-Headers", "Location");
        chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig) {}

    public void destroy() {}

}

我在tomcat web.xml中有这个 没有为COR的过滤器定义过滤器映射。

AngularJS上的请求是

    var headerValue = "";
headerValue = sessionConfigurationService.getBearerToken(sessionConfigurationService.getAccessTokenData());
            config = {headers: {
                    'Authorization': headerValue,
                    'Accept': 'application/json;odata=verbose',
                    'Content-type': 'application/json'
                },
                async: false
            };

if (methodType === "GET") {
            $http.get(URL, data, config).then(function (getData) {
                        debugAlert("GET method response" + JSON.stringify(getData));
                        deffered.resolve(getData.data);
                    }, function (error) {
                        debugAlert("GET method response error" + JSON.stringify(error));
                        deffered.resolve(error);
                    });
        }

我收到此错误

  

XMLHttpRequest无法加载   http://dev2.intbittech.com:8080/testing-dummydata/api/score/v1/。没有   请求中存在“Access-Control-Allow-Origin”标头   资源。因此不允许来源“http://localhost:8080”   访问。响应的HTTP状态代码为401。

请求的CURL是

  

卷曲'http://localhost:8080/TestingFrontEnd/js/userController.js' - H.   '接受编码:gzip,deflate,sdch'-H'接受语言:   en-US,en; q = 0.8'-H'用户代理:Mozilla / 5.0(X11; Linux x86_64)   AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 49.0.2623.108   Safari / 537.36'-H'接受: / '-H'参考者:   http://localhost:8080/TestingFrontEnd/base.html' - H'Cookie:   _ga = GA1.1.222245231.1459238056'-H'连接:keep-alive' - 压缩

0 个答案:

没有答案