cors.adoc 3.5 KB

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