123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- [[cors]]
- = CORS
- Spring Framework provides https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-cors[first class support for CORS].
- CORS must be processed before Spring Security because the pre-flight request will not contain any cookies (i.e. the `JSESSIONID`).
- If the request does not contain any cookies and Spring Security is first, the request will determine the user is not authenticated (since there are no cookies in the request) and reject it.
- The easiest way to ensure that CORS is handled first is to use the `CorsFilter`.
- Users can integrate the `CorsFilter` with Spring Security by providing a `CorsConfigurationSource` using the following:
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- @EnableWebSecurity
- public class WebSecurityConfig {
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- // by default uses a Bean by the name of corsConfigurationSource
- .cors(withDefaults())
- ...
- return http.build();
- }
- @Bean
- CorsConfigurationSource corsConfigurationSource() {
- CorsConfiguration configuration = new CorsConfiguration();
- configuration.setAllowedOrigins(Arrays.asList("https://example.com"));
- configuration.setAllowedMethods(Arrays.asList("GET","POST"));
- UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
- source.registerCorsConfiguration("/**", configuration);
- return source;
- }
- }
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- @EnableWebSecurity
- open class WebSecurityConfig {
- @Bean
- open fun filterChain(http: HttpSecurity): SecurityFilterChain {
- http {
- // by default uses a Bean by the name of corsConfigurationSource
- cors { }
- // ...
- }
- return http.build()
- }
- @Bean
- open fun corsConfigurationSource(): CorsConfigurationSource {
- val configuration = CorsConfiguration()
- configuration.allowedOrigins = listOf("https://example.com")
- configuration.allowedMethods = listOf("GET", "POST")
- val source = UrlBasedCorsConfigurationSource()
- source.registerCorsConfiguration("/**", configuration)
- return source
- }
- }
- ----
- ======
- or in XML
- [source,xml]
- ----
- <http>
- <cors configuration-source-ref="corsSource"/>
- ...
- </http>
- <b:bean id="corsSource" class="org.springframework.web.cors.UrlBasedCorsConfigurationSource">
- ...
- </b:bean>
- ----
- If you are using Spring MVC's CORS support, you can omit specifying the `CorsConfigurationSource` and Spring Security will leverage the CORS configuration provided to Spring MVC.
- [tabs]
- ======
- Java::
- +
- [source,java,role="primary"]
- ----
- @EnableWebSecurity
- public class WebSecurityConfig {
- @Bean
- public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
- http
- // if Spring MVC is on classpath and no CorsConfigurationSource is provided,
- // Spring Security will use CORS configuration provided to Spring MVC
- .cors(withDefaults())
- ...
- return http.build();
- }
- }
- ----
- Kotlin::
- +
- [source,kotlin,role="secondary"]
- ----
- @EnableWebSecurity
- open class WebSecurityConfig {
- @Bean
- open fun filterChain(http: HttpSecurity): SecurityFilterChain {
- http {
- // if Spring MVC is on classpath and no CorsConfigurationSource is provided,
- // Spring Security will use CORS configuration provided to Spring MVC
- cors { }
- // ...
- }
- return http.build()
- }
- }
- ----
- ======
- or in XML
- [source,xml]
- ----
- <http>
- <!-- Default to Spring MVC's CORS configuration -->
- <cors />
- ...
- </http>
- ----
|