Tomcat 服务器配置解决跨域问题解决方式一分析

你猜 阅读:256 2021-03-31 17:01:19 评论:0

修改tomcat所在目录的web.xml配置文件(E:\apache-tomcat-gcxt\conf\web.xml)

添加如下代码:

<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>

注意:此拦截器是tomcat 服务系统自带jar[catalina.jar],源码截图如下:

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号