tomcat 跨域及其session丟失

項目若是沒有使用maven,須要從網上本身找資源,並加到classpath路徑下,即項目的lib文件夾中,對於maven工程,在項目的pom文件中加入以下內容便可java

<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中添加web

<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請求也須要額外的屬性ajax

crossDomain: true,
xhrFields: {
	  withCredentials: true
		}

這樣子就基本上解決問題了,各個文件的具體含義尚未弄得太懂,出現問題及時度娘纔是王道app

相關文章
相關標籤/搜索