tomcat 跨域及其session丢失

项目如果没有使用maven,需要从网上自己找资源,并加到classpath路径下,即项目的lib文件夹中,对于maven工程,在项目的pom文件中加入如下内容即可

<dependency>
            <groupId>com.thetransactioncompany</groupId>
            <artifactId>java-property-utils</artifactId>
            <version>1.9</version>
        </dependency>
         
        <dependency>
            <groupId>com.thetransactioncompany</groupId>
            <artifactId>cors-filter</artifactId>
            <version>2.4</version>
        </dependency>

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

前台ajax请求也需要额外的属性

crossDomain: true,
xhrFields: {
	  withCredentials: true
		}

这样子就基本上解决问题了,各个文件的具体含义还没有弄得太懂,出现问题及时度娘才是王道

你可能感兴趣的:(tomcat 跨域及其session丢失)