Skip to content

Commit f4d78d0

Browse files
IvanPavlov1995eleftherias
authored andcommitted
Extend CorsDsl with CorsConfigurationSource property
Issue: gh-9314
1 parent 0201c31 commit f4d78d0

File tree

2 files changed

+40
-4
lines changed
  • config/src
    • main/kotlin/org/springframework/security/config/web/servlet
    • test/kotlin/org/springframework/security/config/web/servlet

2 files changed

+40
-4
lines changed

config/src/main/kotlin/org/springframework/security/config/web/servlet/CorsDsl.kt

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,15 +18,19 @@ package org.springframework.security.config.web.servlet
1818

1919
import org.springframework.security.config.annotation.web.builders.HttpSecurity
2020
import org.springframework.security.config.annotation.web.configurers.CorsConfigurer
21+
import org.springframework.web.cors.CorsConfigurationSource
2122

2223
/**
2324
* A Kotlin DSL to configure [HttpSecurity] CORS using idiomatic Kotlin code.
2425
*
2526
* @author Eleftheria Stein
2627
* @since 5.3
28+
* @property configurationSource the [CorsConfigurationSource] to use.
2729
*/
2830
@SecurityMarker
2931
class CorsDsl {
32+
var configurationSource: CorsConfigurationSource? = null
33+
3034
private var disabled = false
3135

3236
/**
@@ -38,6 +42,7 @@ class CorsDsl {
3842

3943
internal fun get(): (CorsConfigurer<HttpSecurity>) -> Unit {
4044
return { cors ->
45+
configurationSource?.also { cors.configurationSource(configurationSource) }
4146
if (disabled) {
4247
cors.disable()
4348
}

config/src/test/kotlin/org/springframework/security/config/web/servlet/CorsDslTests.kt

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,7 +67,7 @@ class CorsDslTests {
6767

6868
@Test
6969
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()
7171

7272
this.mockMvc.get("/")
7373
{
@@ -79,7 +79,7 @@ class CorsDslTests {
7979

8080
@EnableWebMvc
8181
@EnableWebSecurity
82-
open class CorsCrossOriginConfig : WebSecurityConfigurerAdapter() {
82+
open class CorsCrossOriginBeanConfig : WebSecurityConfigurerAdapter() {
8383
override fun configure(http: HttpSecurity) {
8484
http {
8585
cors { }
@@ -135,4 +135,35 @@ class CorsDslTests {
135135
return source
136136
}
137137
}
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+
}
138169
}

0 commit comments

Comments
 (0)