1,簡短說明
ajax跨域請求,常見的有jsonp,httpClient,CORS插件等
這次主要介紹CORS插件的使用
幾個問題
1,什麼是跨域:浏覽器同源政策,協定、域名和端口,三者有一個不同,則跨域
2,什麼是jsonp及如何使用【這裡不做重點介紹】
3,什麼是httpClient及如何使用【這裡不重點介紹】
4,什麼是CORS及如何使用【重點介紹】
2,CORS插件配置
CORS(Cross-Origin Resource Sharing,跨源資源共享)是W3C出的一個标準
其思想是使用自定義的HTTP頭部讓浏覽器與伺服器進行溝通
進而決定請求或響應是成功還是失敗
環境:JDK1.7,mvn-v3.3.9
下載下傳CORS插件:cors-filter-2.6.jar和java-property-utils-1.13.jar
将兩個jar包打到maven倉庫中【maven本地打jar包,這裡不做重點介紹】
打封包件夾位置可參考jar包下載下傳頁面中給出的預設位址
xml配置
pom.xml:
<!--https://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter -->
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.thetransactioncompany/java-property-utils -->
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>java-property-utils</artifactId>
<version>1.13</version>
</dependency>
web.xml:
<!-- CORS跨域請求 -->
<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>
3,參考連結
https://www.cnblogs.com/shirly77/p/6440635.html?utm_source=itdadao&utm_medium=referral
https://www.cnblogs.com/yanglang/p/6720887.html
http://www.ruanyifeng.com/blog/2016/04/cors.html
https://blog.csdn.net/wxb880114/article/details/80606434
http://www.uxuew.cn/vuejs/7135.html
http://mvnrepository.com/artifact/com.thetransactioncompany/cors-filter/1.7