SpringSecurity?跨域資源共享(CORS)的實(shí)現(xiàn)
瀏覽器的同源策略(Same-Origin Policy, SOP) 是瀏覽器最核心、最基礎(chǔ)的安全機(jī)制之一,本質(zhì)是為了隔離不同域名的資源,防止惡意網(wǎng)站竊取或篡改其他網(wǎng)站的敏感數(shù)據(jù),保障用戶隱私和網(wǎng)絡(luò)安全。“同源” 指的是兩個(gè)資源的 “協(xié)議(Protocol)、域名(Domain)、端口(Port)” 三者完全一致,缺一不可。只要任意一項(xiàng)不同,就屬于 “跨源(Cross-Origin)”。
CORS(Cross-Origin Resource Sharing)即跨域資源共享,應(yīng)用程序會(huì)通過(guò) CORS(跨域資源共享)機(jī)制來(lái)放寬這一嚴(yán)格的(同源)策略,從而允許在特定條件下進(jìn)行跨源請(qǐng)求。在當(dāng)前流行的前后端分離架構(gòu)模式下很可能會(huì)用到 CORS。
創(chuàng)建一個(gè)頁(yè)面 src\main\resources\static\index.html,這是一個(gè)靜態(tài)文件,可以直接被訪問(wèn),頁(yè)面的訪問(wèn)地址是 http://localhost/index.html,頁(yè)面中有一個(gè)按鈕,點(diǎn)擊按鈕的時(shí)候使用 fetch 發(fā)起對(duì) http://localhost/hello 的請(qǐng)求。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="getHelloBtn">get hello</button>
<script>
const getHelloBtn = document.getElementById('getHelloBtn');
getHelloBtn.addEventListener('click', () => {
fetch('http://127.0.0.1/hello')
.then(res => res.text())
.then(data => {
console.log(data);
})
})
</script>
</body>
</html>
因?yàn)轫?yè)面使用的域名是 localhost 和接口使用的是 127.0.0.1 IP 地址,域名不一致,所以請(qǐng)求的響應(yīng)被阻止了。

此時(shí)可以通過(guò)配置 CORS 就可以來(lái)解決這個(gè)同源策略的限制。Spring 提供了 @CrossOrigin 注解,把這個(gè)注解加在控制器上:
@RestController
public class HelloController {
@RequestMapping(value = { "/hello" }, method = RequestMethod.GET)
// 允許 http://localhost 域發(fā)起對(duì)該接口的請(qǐng)求
@CrossOrigin("http://localhost")
public String requestMethodName() {
return "Hello, Spring!";
}
}再次點(diǎn)擊按鈕,瀏覽器控制臺(tái)中打印出了接口響應(yīng)的內(nèi)容:

@CrossOrigin 除了可以用在方法上之外,還可以用在類上,這樣整個(gè)控制器就都允許跨域訪問(wèn)了。@CrossOrigin 還提供了其他配置參數(shù)
public @interface CrossOrigin {
@AliasFor("origins")
String[] value() default {}; // 允許跨域請(qǐng)求的源
@AliasFor("value")
String[] origins() default {}; // 允許跨域請(qǐng)求的源
String[] originPatterns() default {}; // 通過(guò)通配符模式匹配允許的來(lái)源
String[] allowedHeaders() default {}; // 指定跨域請(qǐng)求中允許攜帶的請(qǐng)求頭
String[] exposedHeaders() default {}; // 指定跨域響應(yīng)中允許前端讀取的響應(yīng)頭
RequestMethod[] methods() default {}; // 指定允許的 HTTP 請(qǐng)求方法
String allowCredentials() default ""; // 指定是否允許跨域請(qǐng)求攜帶身份憑證,
// 若設(shè)為 true,origins 不能為 *(必須指定具體來(lái)源),否則瀏覽器會(huì)拒絕響應(yīng)。
String allowPrivateNetwork() default ""; // 控制是否允許來(lái)自私有網(wǎng)絡(luò)(如局域網(wǎng))的跨域請(qǐng)求
long maxAge() default -1L; // 指定預(yù)檢請(qǐng)求(Preflight Request)的結(jié)果緩存時(shí)間(單位:秒)
}通過(guò) @CrossOrigin 注解可以很簡(jiǎn)單地解決跨域問(wèn)題,但需要每個(gè)控制器都要配置一次,就顯得比較啰嗦了,而且一旦需要修改配置工作量就比較大,而且還可能產(chǎn)生一些風(fēng)險(xiǎn)。
Spring Security 也提供了 CORS 的配置,可以進(jìn)行全局的配置:
@Configuration
public class SecurityConfig {
@Resource
private CustomAuthenticationProvider customAuthenticationProvider;
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authenticationProvider(customAuthenticationProvider)
.httpBasic(Customizer.withDefaults());
http.csrf(c -> c.disable());
http.cors(c -> {
CorsConfigurationSource source = request -> {
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(Arrays.asList("http://localhost")); // 允許跨域請(qǐng)求的源
config.setAllowedMethods(Arrays.asList("GET", "POST")); // 指定允許的 HTTP 請(qǐng)求方法
config.setAllowedHeaders(Arrays.asList("*")); // 指定跨域響應(yīng)中允許前端讀取的響應(yīng)頭
return config;
};
c.configurationSource(source);
});
http.authorizeHttpRequests(
auth -> auth
.anyRequest().permitAll());
return http.build();
}
}如果需要不同的路徑有不同的跨域配置,也沒(méi)問(wèn)題:
@Configuration
public class SecurityConfig {
@Resource
private CustomAuthenticationProvider customAuthenticationProvider;
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authenticationProvider(customAuthenticationProvider)
.httpBasic(Customizer.withDefaults());
http.csrf(c -> c.disable());
http.cors(c -> {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowedOrigins(Arrays.asList("http://localhost"));
config.setAllowedMethods(Arrays.asList("GET"));
config.setAllowedHeaders(Arrays.asList("*"));
source.registerCorsConfiguration("/hello", config); // 給指定的路徑綁定 CORS 配置
CorsConfiguration config2 = new CorsConfiguration();
config2.setAllowedOrigins(Arrays.asList("http://localhost"));
config2.setAllowedMethods(Arrays.asList("POST"));
config2.setAllowedHeaders(Arrays.asList("*"));
source.registerCorsConfiguration("/aa/bb/cc", config2); // 給指定的路徑綁定 CORS 配置
c.configurationSource(source);
});
http.authorizeHttpRequests(
auth -> auth
.anyRequest().permitAll());
return http.build();
}
}到此這篇關(guān)于SpringSecurity 跨域資源共享(CORS)的文章就介紹到這了,更多相關(guān)SpringSecurity 跨域資源共享內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring File Storage文件的對(duì)象存儲(chǔ)框架基本使用小結(jié)
在開發(fā)過(guò)程當(dāng)中,會(huì)使用到存文檔、圖片、視頻、音頻等等,這些都會(huì)涉及存儲(chǔ)的問(wèn)題,文件可以直接存服務(wù)器,但需要考慮帶寬和存儲(chǔ)空間,另外一種方式就是使用云存儲(chǔ),這篇文章主要介紹了Spring File Storage文件的對(duì)象存儲(chǔ)框架基本使用小結(jié),需要的朋友可以參考下2024-08-08
Spring boot實(shí)現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了Spring boot實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
springMVC中HttpMessageConverter的具體使用
HttpMessageConverter,報(bào)文信息轉(zhuǎn)換器,將請(qǐng)求報(bào)文轉(zhuǎn)換為Java對(duì)象,本文主要介紹了springMVC中HttpMessageConverter的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
java?fastjson傳輸long數(shù)據(jù)卻接收到了int的問(wèn)題
這篇文章主要介紹了java?fastjson傳輸long數(shù)據(jù)卻接收到了int的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
spring data jpa @Query注解中delete語(yǔ)句報(bào)錯(cuò)的解決
這篇文章主要介紹了spring data jpa @Query注解中delete語(yǔ)句報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringSecurity獲取當(dāng)前登錄用戶的信息的幾種方法實(shí)現(xiàn)
本文主要介紹了SpringSecurity中獲取當(dāng)前登錄用戶信息的多種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
springboot 異步調(diào)用的實(shí)現(xiàn)方法
這篇文章主要介紹了springboot 異步調(diào)用的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04

