SpringBoot encapsulation Cors automatic configuration
Cross-domain resource sharing (CORS) is a common problem in modern web development. Spring Boot provides a flexible way to handle CORS configuration.
This article will explain how to configure CORS globally in Spring Boot applications through automatic configuration.
background
When the browser requests resources from another domain name from a web page, a cross-domain request occurs. For security reasons, the browser blocks such requests by default. Therefore, we need to make appropriate configuration on the server side to allow these cross-domain requests.
Spring Boot providesCorsRegistry
andWebMvcConfigurer
Interface to CORS configuration. However, if we want to manage CORS settings uniformly throughout the application, we can consider using an automatic configuration approach.
Implementation steps
We will create two main classes:
- GlobalCorsProperties: Configuration properties used to store CORS.
- GlobalCorsAutoConfiguration: Used to automatically configure CORS based on configuration properties.
1. Create the GlobalCorsProperties class
This class will be responsible for reading CORS properties in the configuration file and exposing them to other components for use.
/** * Cors global configuration * * @author Shan Hongyu * @since 2025/2/18 17:18 */ @Data @ConfigurationProperties("") public class GlobalCorsProperties { /** * Whether to enable CORS global configuration */ private boolean enabled = false; /** * CORS configuration map */ private final Map<String, CorsConfiguration> corsConfigurations = new LinkedHashMap<>(); }
2. Create the GlobalCorsAutoConfiguration class
This class will set CORS when Spring Boot starts according to the configuration in GlobalCorsProperties.
/** * Global Cors configuration * * @author Shan Hongyu * @since 2025/2/18 17:33 */ @AutoConfiguration @Import() @ConditionalOnClass() @ConditionalOnProperty(name = "", havingValue = "true") public class GlobalCorsAutoConfiguration implements InitializingBean { /** * RequestMappingHandlerMapping instance */ @Autowired private RequestMappingHandlerMapping requestMappingHandlerMapping; @Override public void afterPropertiesSet() throws Exception { // Get GlobalCorsProperties and set CORS configuration (() .getBean().getCorsConfigurations()); } }
3. Configuration File
Finally, we add the corresponding configuration item to the file.
=true -configurations.[/**].allow-credentials=true -configurations.[/**].allowed-headers=* -configurations.[/**].allowed-methods=GET,POST,PUT,DELETE,OPTIONS -configurations.[/**].allowed-origin-patterns=http://localhost:3000 -configurations.[/**].max-age=1800
If you encapsulate this automatic configuration to your ownstarter
In the middle, you need toGlobalCorsAutoConfiguration
Class added to
META-INF/spring/
in the file.
Summarize
Through the above steps, we successfully implemented automatic configuration of CORS in our Spring Boot application.
This approach not only simplifies the CORS configuration process, but also makes our code more modular and easy to maintain.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.