2
0

cors.adoc 3.4 KB

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