Description
This library enables the CORS configuration in application property
files such as application.properties / application.yml / cors.yml
In general. no coding is required to configure CORS for different
environments of your application. All you need to do is
- add the feature to your project changing maven / gradle / other build tool config
- enable the feature in project property file
- configure the CORS in the same or separate property file, that's up to you to decide
How to add the feature to Spring Boot
To enable the feature, please add the dependency to your project (navigate to this links):
group: io.github.iruzhnikov
name: spring-webmvc-cors-properties-autoconfigure
group: io.github.iruzhnikov
name: spring-webflux-cors-properties-autoconfigure
To enable the feature in Spring Boot project
Add this property to application.properties
file for adding reading configuration from different file
application.properties
file content:
spring.config.import: optional:classpath:cors.yml
Configure the CORS rules in cors.yml
properties file
cors.yml
file content:
spring:
web:
cors:
enabled: true
mappings:
anyName:
paths:
- /path/to/api
- /path/to/api/**
allowed-methods: GET
allowed-origin-patterns: .*
The 'anyName' in configuration lets you configure the different groups, e.g. 'path /api' and 'path /api/admin' may
have CORS configurations. To make the configurations different, please different sections, e.g. 'anyName: api' and
'anyName: apiAdmin'.
Actually, that's all you need to do basic configuration. The further configs are to let you make tiny
configurations.
To specify details of CORS configuration you may use a separate file cors.yml (of course you may use default
application.properties file).
more details about spring.config.import
in Spring
Don't use this library for CORS configuration for Spring Actuator
,
because this library has self specific config (props name like of management.*)
more details in Spring
For application.yml files that contains several 'spring.profiles', please use property file annotation (one line)
for spring.web.cors.enabled
, otherwise spring boot ignores the config line.
more details in stackoverflow
Autoconfigure for allowed-methods (GET, POST etc.) by path patterns
If you like to have the automatic discovery of the allowed-methods in your CORS configuration, please
remove @EnableWebMvc in your code.
if you extended corresponding class:
import org.springframework.webmvc.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.reactive.config.DelegatingWebFluxConfiguration;
please change extended class to next class or open it for learning how to it works
(I mean createRequestMappingHandlerMapping
function)
import io.github.iruzhnikov.webmvc.servlet.CorsPropWebMvcConfigurationSupport;
import io.github.iruzhnikov.webflux.servlet.CorsPropWebFluxConfigurationSupport;
To enable the feature in Spring Framework
You must register property apply bean
import io.github.iruzhnikov.webmvc.servlet.SpringMvcCorsConfigurer;
import io.github.iruzhnikov.webflux.servlet.SpringFluxCorsConfigurer;
and you should register autoconfiguration for allowed-methods (not required)
import io.github.iruzhnikov.webmvc.servlet.CorsEndpointHandlerMapping;
import io.github.iruzhnikov.webflux.servlet.CorsEndpointHandlerMapping;