无法为JAX-RS Web服务启用CORS

时间:2016-08-14 18:55:12

标签: java cors jax-rs

当我使用for j in range(i,4): 请求通过Angular2调用我的服务时,我得到了

http.post

我的JAX-RS服务

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:3000' is therefore not allowed access.

此外,我还尝试添加简单的HTTP过滤器,为所有请求定义所有必需的标头

@Override
    @POST
    @Path("/getWsCallReports")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getWsCallReports(Query query) {
        try {
            LOG.info("getWsCallReports called for " + query);
            return Response.status(200)
                .header("Access-Control-Allow-Origin", "*")
                .header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization")
                .header("Access-Control-Allow-Credentials", "true")
                .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
                .header("Access-Control-Max-Age", "1209600").entity(wsCallReportDao.getReports(query)).build();
        } catch (Exception e) {
            return Response.serverError().build();
        }
    }

但它没有帮助。我做错了什么?

以防万一,我正在使用Java EE 6而我无法实现public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) servletResponse; // TODO: development only response.addHeader("Access-Control-Allow-Origin", "*"); response.addHeader("Access-Control-Allow-Headers", "origin, content-type, accept, authorization"); response.addHeader("Access-Control-Allow-Credentials", "true"); response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); response.addHeader("Access-Control-Max-Age", "1209600"); filterChain.doFilter(servletRequest, servletResponse); } 。我确信没有它就可以完成。

1 个答案:

答案 0 :(得分:0)

这是JAX-RS实现中的一个错误,内置于JBoss EAP 6.2(RestEasy 2.3.7.final)。 升级到JBoss EAP 7.0解决了这个问题。