cors.adoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. [[cors]]
  2. = CORS
  3. Spring Framework provides https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-cors[first class support for CORS].
  4. CORS must be processed before Spring Security, because the pre-flight request does not contain any cookies (that is, the `JSESSIONID`).
  5. If the request does not contain any cookies and Spring Security is first, the request determines that the user is not authenticated (since there are no cookies in the request) and rejects it.
  6. The easiest way to ensure that CORS is handled first is to use the `CorsFilter`.
  7. Users can integrate the `CorsFilter` with Spring Security by providing a `CorsConfigurationSource` that uses the following:
  8. ====
  9. .Java
  10. [source,java,role="primary"]
  11. ----
  12. @Configuration
  13. @EnableWebSecurity
  14. public class WebSecurityConfig {
  15. @Bean
  16. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  17. http
  18. // by default uses a Bean by the name of corsConfigurationSource
  19. .cors(withDefaults())
  20. ...
  21. return http.build();
  22. }
  23. @Bean
  24. CorsConfigurationSource corsConfigurationSource() {
  25. CorsConfiguration configuration = new CorsConfiguration();
  26. configuration.setAllowedOrigins(Arrays.asList("https://example.com"));
  27. configuration.setAllowedMethods(Arrays.asList("GET","POST"));
  28. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  29. source.registerCorsConfiguration("/**", configuration);
  30. return source;
  31. }
  32. }
  33. ----
  34. .Kotlin
  35. [source,kotlin,role="secondary"]
  36. ----
  37. @Configuration
  38. @EnableWebSecurity
  39. open class WebSecurityConfig {
  40. @Bean
  41. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  42. http {
  43. // by default uses a Bean by the name of corsConfigurationSource
  44. cors { }
  45. // ...
  46. }
  47. return http.build()
  48. }
  49. @Bean
  50. open fun corsConfigurationSource(): CorsConfigurationSource {
  51. val configuration = CorsConfiguration()
  52. configuration.allowedOrigins = listOf("https://example.com")
  53. configuration.allowedMethods = listOf("GET", "POST")
  54. val source = UrlBasedCorsConfigurationSource()
  55. source.registerCorsConfiguration("/**", configuration)
  56. return source
  57. }
  58. }
  59. ----
  60. ====
  61. The following listing does the same thing in XML:
  62. ====
  63. [source,xml]
  64. ----
  65. <http>
  66. <cors configuration-source-ref="corsSource"/>
  67. ...
  68. </http>
  69. <b:bean id="corsSource" class="org.springframework.web.cors.UrlBasedCorsConfigurationSource">
  70. ...
  71. </b:bean>
  72. ----
  73. ====
  74. If you use Spring MVC's CORS support, you can omit specifying the `CorsConfigurationSource` and Spring Security uses the CORS configuration provided to Spring MVC:
  75. ====
  76. .Java
  77. [source,java,role="primary"]
  78. ----
  79. @Configuration
  80. @EnableWebSecurity
  81. public class WebSecurityConfig {
  82. @Bean
  83. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  84. http
  85. // if Spring MVC is on classpath and no CorsConfigurationSource is provided,
  86. // Spring Security will use CORS configuration provided to Spring MVC
  87. .cors(withDefaults())
  88. ...
  89. return http.build();
  90. }
  91. }
  92. ----
  93. .Kotlin
  94. [source,kotlin,role="secondary"]
  95. ----
  96. @Configuration
  97. @EnableWebSecurity
  98. open class WebSecurityConfig {
  99. @Bean
  100. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  101. http {
  102. // if Spring MVC is on classpath and no CorsConfigurationSource is provided,
  103. // Spring Security will use CORS configuration provided to Spring MVC
  104. cors { }
  105. // ...
  106. }
  107. return http.build()
  108. }
  109. }
  110. ----
  111. ====
  112. The following listing does the same thing in XML:
  113. ====
  114. [source,xml]
  115. ----
  116. <http>
  117. <!-- Default to Spring MVC's CORS configuration -->
  118. <cors />
  119. ...
  120. </http>
  121. ----
  122. ====