Skip to content

Extend CorsDsl with CorsConfigurationSource property #9333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,15 +18,19 @@ package org.springframework.security.config.web.servlet

import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configurers.CorsConfigurer
import org.springframework.web.cors.CorsConfigurationSource

/**
* A Kotlin DSL to configure [HttpSecurity] CORS using idiomatic Kotlin code.
*
* @author Eleftheria Stein
* @since 5.3
* @property configurationSource the [CorsConfigurationSource] to use.
*/
@SecurityMarker
class CorsDsl {
var configurationSource: CorsConfigurationSource? = null

private var disabled = false

/**
Expand All @@ -38,6 +42,7 @@ class CorsDsl {

internal fun get(): (CorsConfigurer<HttpSecurity>) -> Unit {
return { cors ->
configurationSource?.also { cors.configurationSource(configurationSource) }
if (disabled) {
cors.disable()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -67,7 +67,7 @@ class CorsDslTests {

@Test
fun `CORS when CORS configuration source bean then responds with CORS header`() {
this.spring.register(CorsCrossOriginConfig::class.java).autowire()
this.spring.register(CorsCrossOriginBeanConfig::class.java).autowire()

this.mockMvc.get("/")
{
Expand All @@ -79,7 +79,7 @@ class CorsDslTests {

@EnableWebMvc
@EnableWebSecurity
open class CorsCrossOriginConfig : WebSecurityConfigurerAdapter() {
open class CorsCrossOriginBeanConfig : WebSecurityConfigurerAdapter() {
override fun configure(http: HttpSecurity) {
http {
cors { }
Expand Down Expand Up @@ -135,4 +135,35 @@ class CorsDslTests {
return source
}
}

@Test
fun `CORS when CORS configuration source dsl then responds with CORS header`() {
this.spring.register(CorsCrossOriginBeanConfig::class.java).autowire()

this.mockMvc.get("/")
{
header(HttpHeaders.ORIGIN, "https://example.com")
}.andExpect {
header { exists("Access-Control-Allow-Origin") }
}
}

@EnableWebMvc
@EnableWebSecurity
open class CorsCrossOriginSourceConfig : WebSecurityConfigurerAdapter() {
override fun configure(http: HttpSecurity) {
val source = UrlBasedCorsConfigurationSource()
val corsConfiguration = CorsConfiguration()
corsConfiguration.allowedOrigins = listOf("*")
corsConfiguration.allowedMethods = listOf(
RequestMethod.GET.name,
RequestMethod.POST.name)
source.registerCorsConfiguration("/**", corsConfiguration)
http {
cors {
configurationSource = source
}
}
}
}
}