CROS跨域 解决方案 之 tomcat 做过滤处理解决
摘自:http://www.cnblogs.com/liuwenhao-1/articles/6963540.html
1 、在项目中常常遇到本地访问服务器上的链接数据访问不到,并出现如下问题:
这是因为tomcate 的配置中过滤了请求方式,
解决方案:
1、在tomcate中引入两个jar包:java-property-utils-1.9.1.jar;cors-filter-1.7.1.jar。 http://pan.baidu.com/s/1jHZYkpK ,将两个包放在配置的tomcate下的lib中。
2、修改tomcate下的conf/web.xml,添加内容如下:
1 <filter> 2 <filter-name>CORS</filter-name> 3 <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class> 4 <init-param> 5 <param-name>cors.allowOrigin</param-name> 6 <param-value>*</param-value> 7 </init-param> 8 <init-param> 9 <param-name>cors.supportedMethods</param-name> 10 <param-value>GET,POST,HEAD,PUT,DELETE</param-value> 11 </init-param> 12 <init-param> 13 <param-name>cors.supportedHeaders</param-name> 14 <param-value>Accept,Origin,X-Requested-With,Content-Type,Last-Modified</param-value> 15 </init-param> 16 <init-param> 17 <param-name>cors.exposedHeaders</param-name> 18 <param-value>Set-Cookie</param-value> 19 </init-param> 20 <init-param> 21 <param-name>cors.supportsCredentials</param-name> 22 <param-value>true</param-value> 23 </init-param> 24 </filter> 25 <filter-mapping> 26 <filter-name>CORS</filter-name> 27 <url-pattern>/*</url-pattern> 28 </filter-mapping>
3、若前台在header中添加了参数,则tomcate默认会过滤掉从而不走后台,例如在header中加入了参数NAME和PASS则在tomcate下的conf/web.xml中添加
解决方案:
<filter> <filter-name>CORS</filter-name> <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class> <init-param> <param-name>cors.allowOrigin</param-name> <param-value>*</param-value> </init-param> <init-param> <param-name>cors.supportedMethods</param-name> <param-value>GET,POST,HEAD,PUT,DELETE</param-value> </init-param> <init-param> <param-name>cors.supportedHeaders</param-name> <param-value>Accept,Origin,X-Requested-With,Content-Type,Last-Modified,NAME,PASS</param-value> </init-param> <init-param> <param-name>cors.exposedHeaders</param-name> <param-value>Set-Cookie</param-value> </init-param> <init-param> <param-name>cors.supportsCredentials</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CORS</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
本文来自博客园,作者:驸马爷,转载请注明原文链接:https://www.cnblogs.com/cnblogs-jcy/p/6963612.html