Tomcat CORSfilter
我想启用tomcat CORSfilter,我将其添加到web.xml:
CorsFilter org.apache.catalina.filters.CorsFilter CorsFilter /*
但它不起作用。 我尝试使用自定义filter:
SimpleCORSFilter com.common.SimpleCORSFilter SimpleCORSFilter /*
有了这个class级:
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。
filterorg.apache.catalina.filters.CorsFilter
首先在请求中寻找一个标头: Origin
。 如果此标头不存在,则filter不会在响应中添加任何标头。 也许因为这个原因不起作用。
此外,在POST
请求中,查找标题Content-Type
。 类似的事情发生在其他方法上。 你想看看这个filter的代码吗? 换句话说,有一个流程图 :
我得到了一个类似的问题,我在tomcat上发现了一些适合我的东西doc tomcat-doc-CORSFilter我使用filter和init-param如下:
CorsFilter org.apache.catalina.filters.CorsFilter cors.allowed.origins * cors.allowed.methods GET,POST,HEAD,OPTIONS,PUT cors.allowed.headers Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers cors.exposed.headers Access-Control-Allow-Origin,Access-Control-Allow-Credentials cors.support.credentials true cors.preflight.maxage 10 CorsFilter /*
希望能帮助到你!
在Ueditor的情况下, X-Requested-With
应为X_Requested_With
。
- 这个CORS处理程序安全吗?
- 即使存在所有CORS标头,也会出现跨源资源共享问题
- Google App Engine和CORS
- 使用Spring Framework为OPTIONS请求启用CORS
- 在预检响应中,Access-Control-Allow-Methods不允许使用方法DELETE
- IE11 CORS拒绝https上的OPTIONS
- 请求的资源上不存在Access-Control-Allow-Origin标头
- 使用application / json contentType的AJAX POST调用获取“no’Access-Control-Allow-Origin’标头”错误
- 预检的响应具有无效的HTTP状态代码401 – Spring