2016-08-14 58 views
0

當我調用使用http.post要求通過Angular2我的服務,我得到無法啓用CORS的JAX-RS Web服務

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. 

我的JAX-RS服務

@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(); 
     } 
    } 

而且我已經試過添加簡單的HTTP過濾器,爲所有請求定義所有必需的標頭

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); 
    } 

但它沒有他LP。我究竟做錯了什麼?

爲了以防萬一,我使用的是Java EE 6,但我無法實現ContainerResponseFilter。我相信沒有它就可以完成。

回答

0

這是內置於JBoss EAP 6.2(RestEasy 2.3.7.final)中的JAX-RS實現中的一個錯誤。 升級到JBoss EAP 7.0已經解決了這個問題。