網站首頁 編程語言 正文
之前說過用攔截器去處理瀏覽器的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-05-23 c++?qt自定義搜索編輯框的實現方法_C 語言
- 2023-01-11 Python?基于xml.etree.ElementTree實現XML對比示例詳解_python
- 2022-12-04 Android多功能視頻播放器GSYVideoPlayer開發流程_Android
- 2022-06-19 LINQ基礎之Intersect、Except和Distinct子句_C#教程
- 2022-09-02 ORACLE正則匹配查詢LIKE查詢多個值檢索數據庫對象_oracle
- 2022-08-17 C++詳解Primer文本查詢程序的實現_C 語言
- 2022-10-19 python用opencv將標注提取畫框到對應的圖像中_python
- 2023-02-27 Python?input()函數案例教程_python
- 最近更新
-
- 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同步修改后的遠程分支