CORS在球衣上的支持?

时间:2014-08-08 18:42:02

标签: tomcat jersey cors

我已经看到了允许CORS支持的帖子......但是我试试,我运气不好。

这是我的angular.js上CORS的函数:

var config = {
        method: 'GET',
        url: "//localhost:8080/ScienceCompanionApp/service/bpms/task/5497d038-82da-4a4e-83bd-2ef01b30de1a",
        headers: {
            'BPM-CONTEXT-ID':'CONTEXT',
            'Accept': 'application/json',
            'Content-type': 'application/json'

        }

    }
    $http(config).success(function(data,status,headers){  //do stuff here }

我已将以下内容添加到我的web.xml中:

  <filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

他们说这是关于它的......但是在尝试拨打CORS电话时似乎没有做到这一点:

enter image description here

然后我尝试手动将其添加到我的REST API中:

 @Path("/task/{taskId}")
    @GET
    @ApiOperation(value="Get BPMS Task", notes="You will need to JS decrypt Header param before sending")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response getTask(@ApiParam(value="bpmWorklistTaskId in the BPMS url")@PathParam("taskId") final String taskId,
                        @ApiParam(value="bpmWorklistContext in the bpms url")@HeaderParam(WORKFLOW_CONTEXT_HEADER) final String contextId) {

        logger.info("REST : getTask(" + taskId + ", " + contextId + ")");
        try {
            return Response.status(200).entity(requestProcessor.getTaskDetails(taskId, contextId)).header("Access-Control-Allow-Origin", "*")
                    .header("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS").header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia").build();
        }  catch (Exception e) {
            throw createWebException(e);
        }
    }

 @Path("/task/{taskId}")
    @OPTIONS
    public Response getOptions(@ApiParam(value="bpmWorklistTaskId in the BPMS url")@PathParam("taskId") final String taskId,
            @ApiParam(value="bpmWorklistContext in the bpms url")@HeaderParam(WORKFLOW_CONTEXT_HEADER) final String contextId) 
        {
            return Response.ok()
                      .header("Access-Control-Allow-Origin", "*")
                      .header("Access-Control-Allow-Methods", "POST, GET, PUT, UPDATE, OPTIONS")
                      .header("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With,BPM-CONTEXT-ID,accept,bpm-context-id").build();
        }

仍然没有运气:(

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

执行以下步骤

  1. 创建一个过滤器类,比方说       公共类CORSResponseFilter实现Filter {

    public CORSResponseFilter(){

    }

    @覆盖 public void destroy(){ // TODO自动生成的方法存根

    }

    public void doFilter(ServletRequest请求,ServletResponse响应,FilterChain链)抛出IOException,ServletException {

    String method =((HttpServletRequest)request).getMethod();

    ((HttpServletResponse)响应).addHeader(“Access-Control-Allow-Origin”,“*”);

    if(method.equalsIgnoreCase(“OPTIONS”)){

    ((HttpServletResponse)响应).addHeader(“Access-Control-Allow-Methods”,       “POST,GET,OPTIONS,PUT,DELETE,HEAD”);

    ((HttpServletResponse)响应).addHeader(“Access-Control-Allow-Headers”,       “来源,接受,内容类型,授权”);

    ((HttpServletResponse)回复).addHeader(“Vary”,“Origin”);

    ((HttpServletResponse)响应).addHeader(“Access-Control-Max-Age”,“1728000”);

    返回; }

    chain.doFilter(request,response); }

    @覆盖 public void init(FilterConfig arg0)抛出ServletException {

    // TODO自动生成的方法存根

    }

  2. 在web.xml中输入

    <filter-name>CORSFilter</filter-name>
    
    <filter-class>filter.CORSResponseFilter</filter-class>
    

    &LT; /滤光器&gt;

    &LT;过滤器映射&GT;

    <filter-name>CORSFilter</filter-name>
    
    <url-pattern>/*</url-pattern>
    

    &LT;器/过滤器映射&GT;

  3. 希望这能帮到你..