首页 > 代码库 > tomcat跨域访问

tomcat跨域访问

什么是跨域:

简单地说就是请求几个不同域名之间的资源,出于对安全的考虑,浏览器默认拒绝跨域访问,导致获取资源失败.

方案一:

在tomcat.6.0.27以上版本,跨域问题有简单解决方法

在tomcat 的conf 目录下面 编辑:context.xml

将里面的Context修改为以下即可:

<ContextsessionCookiePath=""sessionCookieDomain=".×××.cn"/>

 

方案二:

导入cors-filter-1.7.jar,java-property-utils-1.9.jar两个jar包

  在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</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跨域访问