Tomcat设置跨域访问

  • 2019-08-29
  • 4
  • 0

今天在本地调试,没想到在前端项目中要访问服务器资源也提示跨域无法访问问题………..

解决方案:

一:在tomcat中的lib文件夹下增加两个jar包

  • cors-filter-2.4.jar
  • java-property-utils-1.9.1.jar

下载地址:jar

二:在tomcat中的conf文件夹下的web.xml文件中的web-app代码下增加如下代码

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

 

本文地址:Tomcat设置跨域访问

评论

还没有任何评论,你来说两句吧