cors.adoc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. @EnableWebSecurity
  13. public class WebSecurityConfig {
  14. @Bean
  15. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  16. http
  17. // by default uses a Bean by the name of corsConfigurationSource
  18. .cors(withDefaults())
  19. ...
  20. return http.build();
  21. }
  22. @Bean
  23. CorsConfigurationSource corsConfigurationSource() {
  24. CorsConfiguration configuration = new CorsConfiguration();
  25. configuration.setAllowedOrigins(Arrays.asList("https://example.com"));
  26. configuration.setAllowedMethods(Arrays.asList("GET","POST"));
  27. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  28. source.registerCorsConfiguration("/**", configuration);
  29. return source;
  30. }
  31. }
  32. ----
  33. .Kotlin
  34. [source,kotlin,role="secondary"]
  35. ----
  36. @EnableWebSecurity
  37. open class WebSecurityConfig {
  38. @Bean
  39. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  40. http {
  41. // by default uses a Bean by the name of corsConfigurationSource
  42. cors { }
  43. // ...
  44. }
  45. return http.build()
  46. }
  47. @Bean
  48. open fun corsConfigurationSource(): CorsConfigurationSource {
  49. val configuration = CorsConfiguration()
  50. configuration.allowedOrigins = listOf("https://example.com")
  51. configuration.allowedMethods = listOf("GET", "POST")
  52. val source = UrlBasedCorsConfigurationSource()
  53. source.registerCorsConfiguration("/**", configuration)
  54. return source
  55. }
  56. }
  57. ----
  58. ====
  59. The following listing does the same thing in XML:
  60. ====
  61. [source,xml]
  62. ----
  63. <http>
  64. <cors configuration-source-ref="corsSource"/>
  65. ...
  66. </http>
  67. <b:bean id="corsSource" class="org.springframework.web.cors.UrlBasedCorsConfigurationSource">
  68. ...
  69. </b:bean>
  70. ----
  71. ====
  72. 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:
  73. ====
  74. .Java
  75. [source,java,role="primary"]
  76. ----
  77. @EnableWebSecurity
  78. public class WebSecurityConfig {
  79. @Bean
  80. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  81. http
  82. // if Spring MVC is on classpath and no CorsConfigurationSource is provided,
  83. // Spring Security will use CORS configuration provided to Spring MVC
  84. .cors(withDefaults())
  85. ...
  86. return http.build();
  87. }
  88. }
  89. ----
  90. .Kotlin
  91. [source,kotlin,role="secondary"]
  92. ----
  93. @EnableWebSecurity
  94. open class WebSecurityConfig {
  95. @Bean
  96. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  97. http {
  98. // if Spring MVC is on classpath and no CorsConfigurationSource is provided,
  99. // Spring Security will use CORS configuration provided to Spring MVC
  100. cors { }
  101. // ...
  102. }
  103. return http.build()
  104. }
  105. }
  106. ----
  107. ====
  108. The following listing does the same thing in XML:
  109. ====
  110. [source,xml]
  111. ----
  112. <http>
  113. <!-- Default to Spring MVC's CORS configuration -->
  114. <cors />
  115. ...
  116. </http>
  117. ----
  118. ====