4

On client side I'm using Ajax.post (jquery 1.5) with json. On server side I'm using rest resteasy-jaxrs-2.0.1.GA. I found somewhere that i should add couple of headers to server response and I've done with following filter:

public void doFilter(   ServletRequest req,
                        ServletResponse res,
                        FilterChain filterChain)
    throws IOException, ServletException {

    MyServletRequestWrapper httpReq    = new MyServletRequestWrapper((HttpServletRequest)req);
    HttpServletResponse    httpRes   = (HttpServletResponse)res;

    HttpSession session = httpReq.getSession();


    httpRes.addHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*");
    httpRes.addHeader(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");

   if (((HttpServletRequest) req).getMethod().equals("OPTIONS")){
        httpRes.addHeader(ACCESS_CONTROL_ALLOW_METHODS, "GET, POST, OPTIONS, PUT, DELETE");
        httpRes.addHeader(ACCESS_CONTROL_ALLOW_HEADERS, "content-type, x-requested-with, x-requested-by");
   }

  filterChain.doFilter(httpReq, httpRes);

}

It works fine cause to every single GET response above headers are added. Problem appears when I want to use POST request. When I use Ajax.post, at first server gets OPTIONS request and I've got following error:

Failed executing OPTIONS [REST_PATH] org.jboss.resteasy.spi.DefaultOptionsMethodException: No resource method found for options, return OK with Allow header

To solve above error I was trying to add method invoke with the same path as POST ([REST_PATH]) but with @OPTION annotation. In that case javac told me that symbol :class OPTIONS could not be found, even there is a OPTION.class in attached jaxrs library.

Any ideas to fix it? I would be very grateful for any clues.

4

4 回答 4

1

这个问题已经很老了,但作为其他有类似问题的人的参考 - 就在最近我遇到了一个不错的“CORS过滤器”,您可能想考虑使用。只需将以下行添加到您的 web.xml 中,它就像一个魅力。

<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>CORS</filter-name>
    <servlet-name>MyServletNameGoesHere</servlet-name>
</filter-mapping>

和maven依赖:

<dependency>
    <groupId>com.thetransactioncompany</groupId>
    <artifactId>cors-filter</artifactId>
    <version>1.5.1</version>
</dependency>
于 2013-04-06T20:19:26.637 回答
1

当我尝试使用 Angular 2 客户端调用我的服务时,RestEasy 以 HTTP 状态 500 和以下错误响应预检请求。

RESTEASY003655:未找到选项的资源方法,返回 OK 并带有 Allow 标头

我尝试了 RestEasy 的 CorsFilter,但我想提出一个简单的替代方案。如果您不想为每个端点编写处理 OPTIONS 调用的代码怎么办?

我实现了一个简单的过滤器:

  1. 将所需的 CORS 标头应用于响应。
  2. 使用 OPTIONS 方法调用端点时返回 HTTP 状态代码 200。您告诉客户其 CORS 预检请求已被接受。

这是代码。如果您只想在查询“真实”端点时发回 200,请随意优化过滤器。

@Provider 
public class CorsFilter implements ContainerResponseFilter {  

  @Override
  public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    MultivaluedMap<String, Object> headers = responseContext.getHeaders();
    headers.add("Access-Control-Allow-Origin", "*"); // If you want to be more restrictive it could be localhost:4200
    headers.add("Access-Control-Allow-Methods", "GET, PUT, POST, OPTIONS"); // You can add HEAD, DELETE, TRACE, PATCH
    headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept, Accept-Language"); // You can add many more

    if (requestContext.getMethod().equals("OPTIONS"))
        responseContext.setStatus(200);
}}

确保也了解 CORS

于 2017-08-10T13:53:34.520 回答
0

在 tomcat 中有一个内置的 CORS 过滤器。 http://tomcat.apache.org/tomcat-7.0-doc/config/filter.html

<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>
于 2015-01-22T17:39:32.763 回答
0

CORS 过滤器可能没问题。我想指出你写的“为了解决上述错误,我试图添加与 POST ([REST_PATH]) 相同的路径但带有 @OPTION 注释的方法调用。在这种情况下,javac 告诉我符号 :class OPTIONS 可以找不到,“你有一个错字而不是你写的没有:)@OPTIONS@OPTIONS

于 2014-10-09T08:44:16.507 回答