Tomcat CORS过滤器

时间:2014-06-24 12:31:27

标签: java tomcat web-applications cors

我想启用tomcat CORS过滤器,我将其添加到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>

但它不起作用。 我尝试使用自定义过滤器:

<filter>
    <filter-name>SimpleCORSFilter</filter-name>
    <filter-class>com.common.SimpleCORSFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>SimpleCORSFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

使用此类:

public class SimpleCORSFilter 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, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        chain.doFilter(req, res);
    }
}

这很好用,你能告诉我为什么吗? 我不知道它是否重要但我使用Spring Framework。

4 个答案:

答案 0 :(得分:43)

过滤器org.apache.catalina.filters.CorsFilter首先在请求中搜索标头:Origin。如果此标头不存在,则过滤器不会在响应中添加任何标头。也许因为这个原因不起作用。

此外,在POST请求中,查找标头Content-Type。类似的事情发生在其他方法上。您是否希望查看此过滤器的code。换句话说,有一个flowchart

CORS Flow Chart

答案 1 :(得分:17)

我遇到了类似的问题,我在tomcat doc tomcat-doc-CORSFilter上发现了一些对我有用的东西 我使用过滤器和 init-param ,如下所示:

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
  <init-param>
    <param-name>cors.allowed.origins</param-name>
    <param-value>*</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.methods</param-name>
    <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.headers</param-name>
    <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
  </init-param>
  <init-param>
    <param-name>cors.exposed.headers</param-name>
    <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
  </init-param>
  <init-param>
    <param-name>cors.support.credentials</param-name>
    <param-value>true</param-value>
  </init-param>
  <init-param>
    <param-name>cors.preflight.maxage</param-name>
    <param-value>10</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

希望它有所帮助!

答案 2 :(得分:2)

我没有足够的声誉发表评论,但感谢您的回答@Krikza :)

使用 Tomcat 9 我遇到了以下问题:javax.servlet.ServletException: It is not allowed to configure supportsCredentials=[true] when allowedOrigins=[*]

只需删除参数即可修复它

<init-param>
  <param-name>cors.support.credentials</param-name>
  <param-value>true</param-value>
</init-param>

答案 3 :(得分:0)

在我使用Ueditor的情况下,X-Requested-With应为X_Requested_With