Spring的@CrossOrigin注解使用與CrossFilter對象自定義詳解
一、跨域概念解析(@CrossOrigin)
跨域,指的是瀏覽器不能執(zhí)行其他網(wǎng)站的腳本。它是由瀏覽器的同源策略造成的,是瀏覽器施加的安全限制。
所謂同源是指,域名,協(xié)議,端口均相同,不明白沒關(guān)系,舉個栗子:
- //a.yagnxigua.com/index.html 調(diào)用 //a.yagnxigua.com/server.php (非跨域)
- //www.123.com/index.html 調(diào)用 //www.456.com/server.php (主域名不同:123/456,跨域)
- //a.yagnxigua.com/index.html 調(diào)用 //b.yagnxigua.com/server.php (子域名不同:abc/def,跨域)
- //a.yagnxigua.com:8080/index.html 調(diào)用 /a.yagnxigua.com:8081/server.php (端口不同:8080/8081,跨域)
- //a.yagnxigua.com/index.html 調(diào)用 https://a.yagnxigua.com/server.php (協(xié)議不同:http/https,跨域)
請注意:localhost和127.0.0.1雖然都指向本機(jī),但也屬于跨域,同理域名和域名對應(yīng)的ip也屬于跨域。
瀏覽器執(zhí)行javascript腳本時,會檢查這個腳本屬于哪個頁面,如果不是同源頁面,就不會被執(zhí)行。
二、Spring Boot跨域(@CrossOrigin)
當(dāng)然這里雖然指SpringBoot但是SpringMVC也是一樣的,要求在Spring4.2及以上的版本
1、@CrossOrigin使用場景要求
- jdk1.8+
- Spring4.2+
2、@CrossOrigin源碼解析(網(wǎng)絡(luò)復(fù)制)
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CrossOrigin {
String[] DEFAULT_ORIGINS = { "*" };
String[] DEFAULT_ALLOWED_HEADERS = { "*" };
boolean DEFAULT_ALLOW_CREDENTIALS = true;
long DEFAULT_MAX_AGE = 1800;
/**
* 同origins屬性一樣
*/
@AliasFor("origins")
String[] value() default {};
/**
* 所有支持域的集合,例如"http://domain1.com"。
* <p>這些值都顯示在請求頭中的Access-Control-Allow-Origin
* "*"代表所有域的請求都支持
* <p>如果沒有定義,所有請求的域都支持
* @see #value
*/
@AliasFor("value")
String[] origins() default {};
/**
* 允許請求頭重的header,默認(rèn)都支持
*/
String[] allowedHeaders() default {};
/**
* 響應(yīng)頭中允許訪問的header,默認(rèn)為空
*/
String[] exposedHeaders() default {};
/**
* 請求支持的方法,例如"{RequestMethod.GET, RequestMethod.POST}"}。
* 默認(rèn)支持RequestMapping中設(shè)置的方法
*/
RequestMethod[] methods() default {};
/**
* 是否允許cookie隨請求發(fā)送,使用時必須指定具體的域
*/
String allowCredentials() default "";
/**
* 預(yù)請求的結(jié)果的有效期,默認(rèn)30分鐘
*/
long maxAge() default -1;
}三、@CrossOrigin使用
Spring Boot下的請求處理控制器
package com.example.demo.controller;
import com.example.demo.domain.User;
import com.example.demo.service.IUserFind;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
//實現(xiàn)跨域注解
//origin="*"代表所有域名都可訪問
//maxAge飛行前響應(yīng)的緩存持續(xù)時間的最大年齡,簡單來說就是Cookie的有效期 單位為秒
//若maxAge是負(fù)數(shù),則代表為臨時Cookie,不會被持久化,Cookie信息保存在瀏覽器內(nèi)存中,瀏覽器關(guān)閉Cookie就消失
@CrossOrigin(origins = "*",maxAge = 3600)
public class UserController {
@Resource
private IUserFind userFind;
@GetMapping("finduser")
public User finduser(@RequestParam(value="id") Integer id){
.......
}
}到此這篇關(guān)于Spring的@CrossOrigin注解使用與CrossFilter對象自定義詳解的文章就介紹到這了,更多相關(guān)@CrossOrigin注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot2.0 整合 Dubbo框架實現(xiàn)RPC服務(wù)遠(yuǎn)程調(diào)用方法
這篇文章主要介紹了SpringBoot2.0 整合 Dubbo框架 實現(xiàn)RPC服務(wù)遠(yuǎn)程調(diào)用 ,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07
springboot2.3 整合mybatis-plus 高級功能及用法詳解
這篇文章主要介紹了springboot2.3 整合mybatis-plus 高級功能,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Springboot3利用redis生成唯一訂單號的實現(xiàn)示例
本文主要介紹了Springboot3利用redis生成唯一訂單號的實現(xiàn)示例,包括UUID、雪花算法和數(shù)據(jù)庫約束,具有一定的參考價值,感興趣的可以了解一下2025-03-03
java中httpclient封裝post請求和get的請求實例
這篇文章主要介紹了java中httpclient封裝post請求和get的請求實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

