1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -67,7 +67,7 @@ class CorsDslTests {
67
67
68
68
@Test
69
69
fun `CORS when CORS configuration source bean then responds with CORS header` () {
70
- this .spring.register(CorsCrossOriginConfig ::class .java).autowire()
70
+ this .spring.register(CorsCrossOriginBeanConfig ::class .java).autowire()
71
71
72
72
this .mockMvc.get(" /" )
73
73
{
@@ -79,7 +79,7 @@ class CorsDslTests {
79
79
80
80
@EnableWebMvc
81
81
@EnableWebSecurity
82
- open class CorsCrossOriginConfig : WebSecurityConfigurerAdapter () {
82
+ open class CorsCrossOriginBeanConfig : WebSecurityConfigurerAdapter () {
83
83
override fun configure (http : HttpSecurity ) {
84
84
http {
85
85
cors { }
@@ -135,4 +135,35 @@ class CorsDslTests {
135
135
return source
136
136
}
137
137
}
138
+
139
+ @Test
140
+ fun `CORS when CORS configuration source dsl then responds with CORS header` () {
141
+ this .spring.register(CorsCrossOriginBeanConfig ::class .java).autowire()
142
+
143
+ this .mockMvc.get(" /" )
144
+ {
145
+ header(HttpHeaders .ORIGIN , " https://example.com" )
146
+ }.andExpect {
147
+ header { exists(" Access-Control-Allow-Origin" ) }
148
+ }
149
+ }
150
+
151
+ @EnableWebMvc
152
+ @EnableWebSecurity
153
+ open class CorsCrossOriginSourceConfig : WebSecurityConfigurerAdapter () {
154
+ override fun configure (http : HttpSecurity ) {
155
+ val source = UrlBasedCorsConfigurationSource ()
156
+ val corsConfiguration = CorsConfiguration ()
157
+ corsConfiguration.allowedOrigins = listOf (" *" )
158
+ corsConfiguration.allowedMethods = listOf (
159
+ RequestMethod .GET .name,
160
+ RequestMethod .POST .name)
161
+ source.registerCorsConfiguration(" /**" , corsConfiguration)
162
+ http {
163
+ cors {
164
+ configurationSource = source
165
+ }
166
+ }
167
+ }
168
+ }
138
169
}
0 commit comments