Skip to content

Commit 1a1a8a7

Browse files
author
Steve Riesenberg
committed
Merge branch '5.8.x'
# Conflicts: # config/src/test/kotlin/org/springframework/security/config/annotation/web/HttpSecurityDslTests.kt
2 parents 1aee40d + 45bbd86 commit 1a1a8a7

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

config/src/main/kotlin/org/springframework/security/config/annotation/web/HttpSecurityDsl.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ package org.springframework.security.config.annotation.web
1818

1919
import org.springframework.context.ApplicationContext
2020
import org.springframework.security.authentication.AuthenticationManager
21+
import org.springframework.security.config.annotation.SecurityConfigurerAdapter
2122
import org.springframework.security.config.annotation.web.builders.HttpSecurity
2223
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository
2324
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistrationRepository
25+
import org.springframework.security.web.DefaultSecurityFilterChain
2426
import org.springframework.security.web.util.matcher.RequestMatcher
2527
import org.springframework.util.ClassUtils
2628
import jakarta.servlet.Filter
@@ -76,6 +78,35 @@ class HttpSecurityDsl(private val http: HttpSecurity, private val init: HttpSecu
7678

7779
var authenticationManager: AuthenticationManager? = null
7880

81+
/**
82+
* Applies a [SecurityConfigurerAdapter] to this [HttpSecurity]
83+
*
84+
* Example:
85+
*
86+
* ```
87+
* @Configuration
88+
* @EnableWebSecurity
89+
* class SecurityConfig {
90+
*
91+
* @Bean
92+
* fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
93+
* http {
94+
* apply(CustomSecurityConfigurer<HttpSecurity>()) {
95+
* customProperty = "..."
96+
* }
97+
* }
98+
* return http.build()
99+
* }
100+
* }
101+
* ```
102+
*
103+
* @param configurer
104+
* the [SecurityConfigurerAdapter] for further customizations
105+
*/
106+
fun <C : SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity>> apply(configurer: C, configuration: C.() -> Unit = { }): C {
107+
return this.http.apply(configurer).apply(configuration)
108+
}
109+
79110
/**
80111
* Allows configuring the [HttpSecurity] to only be invoked when matching the
81112
* provided pattern.

config/src/test/kotlin/org/springframework/security/config/annotation/web/HttpSecurityDslTests.kt

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import io.mockk.verify
2222
import org.assertj.core.api.Assertions.assertThat
2323
import org.junit.jupiter.api.Test
2424
import org.junit.jupiter.api.extension.ExtendWith
25+
import org.junit.jupiter.params.ParameterizedTest
26+
import org.junit.jupiter.params.provider.ValueSource
2527
import org.springframework.beans.factory.annotation.Autowired
2628
import org.springframework.context.annotation.Bean
2729
import org.springframework.context.annotation.Configuration
@@ -32,13 +34,15 @@ import org.springframework.security.authentication.TestingAuthenticationProvider
3234
import org.springframework.security.authentication.TestingAuthenticationToken
3335
import org.springframework.security.config.annotation.web.builders.HttpSecurity
3436
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
37+
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer
3538
import org.springframework.security.config.test.SpringTestContext
3639
import org.springframework.security.config.test.SpringTestContextExtension
3740
import org.springframework.security.core.userdetails.User
3841
import org.springframework.security.core.userdetails.UserDetailsService
3942
import org.springframework.security.provisioning.InMemoryUserDetailsManager
4043
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic
4144
import org.springframework.security.web.FilterChainProxy
45+
import org.springframework.security.web.SecurityFilterChain
4246
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
4347
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter
4448
import org.springframework.security.web.server.header.ContentTypeOptionsServerHttpHeadersWriter
@@ -52,9 +56,6 @@ import org.springframework.test.web.servlet.post
5256
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
5357
import org.springframework.web.servlet.config.annotation.EnableWebMvc
5458
import jakarta.servlet.Filter
55-
import org.junit.jupiter.params.ParameterizedTest
56-
import org.junit.jupiter.params.provider.ValueSource
57-
import org.springframework.security.web.SecurityFilterChain
5859

5960
/**
6061
* Tests for [HttpSecurityDsl]
@@ -516,4 +517,42 @@ class HttpSecurityDslTests {
516517
}
517518

518519
class CustomFilter : UsernamePasswordAuthenticationFilter()
520+
521+
@Test
522+
fun `HTTP security when apply custom security configurer then custom filter added to filter chain`() {
523+
this.spring.register(CustomSecurityConfigurerConfig::class.java).autowire()
524+
525+
val filterChain = spring.context.getBean(FilterChainProxy::class.java)
526+
val filterClasses: List<Class<out Filter>> = filterChain.getFilters("/").map { it.javaClass }
527+
528+
assertThat(filterClasses).contains(
529+
CustomFilter::class.java
530+
)
531+
}
532+
533+
@Configuration
534+
@EnableWebSecurity
535+
@EnableWebMvc
536+
open class CustomSecurityConfigurerConfig {
537+
@Bean
538+
open fun securityFilterChain(http: HttpSecurity): SecurityFilterChain {
539+
http {
540+
apply(CustomSecurityConfigurer<HttpSecurity>()) {
541+
filter = CustomFilter()
542+
}
543+
}
544+
return http.build()
545+
}
546+
}
547+
548+
class CustomSecurityConfigurer<H : HttpSecurityBuilder<H>> : AbstractHttpConfigurer<CustomSecurityConfigurer<H>, H>() {
549+
var filter: Filter? = null
550+
override fun init(builder: H) {
551+
filter = filter ?: UsernamePasswordAuthenticationFilter()
552+
}
553+
554+
override fun configure(builder: H) {
555+
builder.addFilterBefore(CustomFilter(), UsernamePasswordAuthenticationFilter::class.java)
556+
}
557+
}
519558
}

0 commit comments

Comments
 (0)