SpringBoot中封裝Cors自動配置方式
SpringBoot封裝Cors自動配置
在現代 Web 開發(fā)中,跨域資源共享(CORS)是一個常見的問題。Spring Boot 提供了靈活的方式來處理 CORS 配置。
本文將介紹如何通過自動配置的方式,在 Spring Boot 應用程序中全局配置 CORS。
背景
當瀏覽器從一個域名的網頁去請求另一個域名的資源時,會發(fā)生跨域請求。為了安全起見,默認情況下瀏覽器會阻止這種請求。因此,我們需要在服務器端進行適當的配置來允許這些跨域請求。
Spring Boot 提供了 CorsRegistry 和 WebMvcConfigurer 接口來進行 CORS 配置。然而,如果我們希望在整個應用程序中統(tǒng)一管理 CORS 設置,可以考慮使用自動配置的方式。
實現步驟
我們將創(chuàng)建兩個主要類:
- GlobalCorsProperties: 用于存儲 CORS 的配置屬性。
- GlobalCorsAutoConfiguration: 用于根據配置屬性自動配置 CORS。
1. 創(chuàng)建 GlobalCorsProperties 類
這個類將負責讀取配置文件中的 CORS 屬性,并將其暴露給其他組件使用。
/**
* Cors全局配置
*
* @author 單紅宇
* @since 2025/2/18 17:18
*/
@Data
@ConfigurationProperties("spring.web.globalcors")
public class GlobalCorsProperties {
/**
* 是否啟用 CORS 全局配置
*/
private boolean enabled = false;
/**
* CORS 配置映射
*/
private final Map<String, CorsConfiguration> corsConfigurations = new LinkedHashMap<>();
}2. 創(chuàng)建 GlobalCorsAutoConfiguration 類
這個類將在 Spring Boot 啟動時根據 GlobalCorsProperties 中的配置來設置 CORS。
/**
* 全局Cors配置
*
* @author 單紅宇
* @since 2025/2/18 17:33
*/
@AutoConfiguration
@Import(GlobalCorsProperties.class)
@ConditionalOnClass(SimpleUrlHandlerMapping.class)
@ConditionalOnProperty(name = "spring.web.globalcors.enabled", havingValue = "true")
public class GlobalCorsAutoConfiguration implements InitializingBean {
/**
* RequestMappingHandlerMapping 實例
*/
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;
@Override
public void afterPropertiesSet() throws Exception {
// 獲取 GlobalCorsProperties 并設置 CORS 配置
requestMappingHandlerMapping.setCorsConfigurations(SpringContextHolder.getApplicationContext()
.getBean(GlobalCorsProperties.class).getCorsConfigurations());
}
}3. 配置 application.properties 文件
最后,我們在 application.properties 文件中添加相應的配置項。
spring.web.globalcors.enabled=true spring.web.globalcors.cors-configurations.[/**].allow-credentials=true spring.web.globalcors.cors-configurations.[/**].allowed-headers=* spring.web.globalcors.cors-configurations.[/**].allowed-methods=GET,POST,PUT,DELETE,OPTIONS spring.web.globalcors.cors-configurations.[/**].allowed-origin-patterns=http://localhost:3000 spring.web.globalcors.cors-configurations.[/**].max-age=1800
如果你把這個自動配置封裝到自己的 starter 中,還需要將 GlobalCorsAutoConfiguration 類添加到
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件中。
總結
通過以上步驟,我們成功地在 Spring Boot 應用程序中實現了 CORS 的自動配置。
這種方式不僅簡化了 CORS 的配置過程,還使得我們的代碼更加模塊化和易于維護。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
淺談@mapper引入不到引入的是@MapperScan的問題
這篇文章主要介紹了淺談@mapper引入不到引入的是@MapperScan的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Spring?Boot?Security認證之Redis緩存用戶信息詳解
本文介紹了如何使用Spring Boot Security進行認證,并通過Redis緩存用戶信息以提高系統(tǒng)性能,通過配置RedisUserDetailsManager,我們成功地將用戶信息存儲到了Redis中,并在Spring Security中進行了集成,需要的朋友可以參考下2024-01-01
springboot整合mybatis-plus逆向工程的實現
這篇文章主要介紹了springboot整合mybatis-plus逆向工程的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08
SpringBoot項目application.yml文件數據庫配置密碼加密的方法
這篇文章主要介紹了SpringBoot項目application.yml文件數據庫配置密碼加密的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03

