網站首頁 編程語言 正文
之前說過用攔截器去處理瀏覽器的OPTIONS請求來解決跨域問題,但是如果項目中配置了一些校驗請求的過濾器則會使其失效,因為過濾器比攔截器要早,所以有可能會使其失效。
攔截器解決跨域:http://www.shdianci.com/article/6501.html
核心代碼
這個過濾器的核心也是對OPTIONS請求進行了一個處理
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
@Configuration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*"); // 1允許任何域名使用
corsConfiguration.addAllowedHeader("*"); // 2允許任何頭
corsConfiguration.addAllowedMethod("*"); // 3允許任何方法(post、get等)
corsConfiguration.setAllowCredentials(true); // 允許cookies跨域
return corsConfiguration;
}
@Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new CorsFilter(source));
// 設置 Filter 的優先級為最高優先級(如果有多個過濾器這些過濾器會有一個先后順序的問題)
filterRegistrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
return filterRegistrationBean;
}
}
原文鏈接:https://blog.csdn.net/weixin_44953227/article/details/121378084
相關推薦
- 2022-04-10 Android中shape的自定義藝術效果使用_Android
- 2022-07-21 SystemVerilog中使用string所遇到的問題
- 2022-05-12 Kotlin 集合也可以進行+= -= 還可以根據條件進行刪除(removeIf)
- 2022-09-18 在while中使用cin>>a?為條件及注意事項說明_C 語言
- 2022-04-14 解決 zsh:command not found
- 2022-07-07 通過Golang編寫一個AES加密解密工具_Golang
- 2023-09-17 POM文件中使用<exclusions>解決jar沖突問題
- 2024-02-27 credentials to a set of origins, list them explici
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支