2
0

cors.adoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. ====
  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. or in XML
  60. [source,xml]
  61. ----
  62. <http>
  63. <cors configuration-source-ref="corsSource"/>
  64. ...
  65. </http>
  66. <b:bean id="corsSource" class="org.springframework.web.cors.UrlBasedCorsConfigurationSource">
  67. ...
  68. </b:bean>
  69. ----
  70. 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.
  71. ====
  72. .Java
  73. [source,java,role="primary"]
  74. ----
  75. @EnableWebSecurity
  76. public class WebSecurityConfig {
  77. @Bean
  78. public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
  79. http
  80. // if Spring MVC is on classpath and no CorsConfigurationSource is provided,
  81. // Spring Security will use CORS configuration provided to Spring MVC
  82. .cors(withDefaults())
  83. ...
  84. return http.build();
  85. }
  86. }
  87. ----
  88. .Kotlin
  89. [source,kotlin,role="secondary"]
  90. ----
  91. @EnableWebSecurity
  92. open class WebSecurityConfig {
  93. @Bean
  94. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  95. http {
  96. // if Spring MVC is on classpath and no CorsConfigurationSource is provided,
  97. // Spring Security will use CORS configuration provided to Spring MVC
  98. cors { }
  99. // ...
  100. }
  101. return http.build()
  102. }
  103. }
  104. ----
  105. ====
  106. or in XML
  107. [source,xml]
  108. ----
  109. <http>
  110. <!-- Default to Spring MVC's CORS configuration -->
  111. <cors />
  112. ...
  113. </http>
  114. ----