cors.adoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 will not contain any cookies (i.e. the `JSESSIONID`).
  5. 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.
  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` using the following:
  8. [tabs]
  9. ======
  10. Java::
  11. +
  12. [source,java,role="primary"]
  13. ----
  14. @EnableWebSecurity
  15. public class WebSecurityConfig {
  16. @Bean
  17. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  18. http
  19. // by default uses a Bean by the name of corsConfigurationSource
  20. .cors(withDefaults())
  21. ...
  22. return http.build();
  23. }
  24. @Bean
  25. CorsConfigurationSource corsConfigurationSource() {
  26. CorsConfiguration configuration = new CorsConfiguration();
  27. configuration.setAllowedOrigins(Arrays.asList("https://example.com"));
  28. configuration.setAllowedMethods(Arrays.asList("GET","POST"));
  29. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  30. source.registerCorsConfiguration("/**", configuration);
  31. return source;
  32. }
  33. }
  34. ----
  35. Kotlin::
  36. +
  37. [source,kotlin,role="secondary"]
  38. ----
  39. @EnableWebSecurity
  40. open class WebSecurityConfig {
  41. @Bean
  42. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  43. http {
  44. // by default uses a Bean by the name of corsConfigurationSource
  45. cors { }
  46. // ...
  47. }
  48. return http.build()
  49. }
  50. @Bean
  51. open fun corsConfigurationSource(): CorsConfigurationSource {
  52. val configuration = CorsConfiguration()
  53. configuration.allowedOrigins = listOf("https://example.com")
  54. configuration.allowedMethods = listOf("GET", "POST")
  55. val source = UrlBasedCorsConfigurationSource()
  56. source.registerCorsConfiguration("/**", configuration)
  57. return source
  58. }
  59. }
  60. ----
  61. ======
  62. or in XML
  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. 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.
  74. [tabs]
  75. ======
  76. Java::
  77. +
  78. [source,java,role="primary"]
  79. ----
  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. +
  95. [source,kotlin,role="secondary"]
  96. ----
  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. or in XML
  113. [source,xml]
  114. ----
  115. <http>
  116. <!-- Default to Spring MVC's CORS configuration -->
  117. <cors />
  118. ...
  119. </http>
  120. ----