Skip to content

Commit dc6b8ce

Browse files
authored
Add addFilterAfter and addFilterBefore to Kotlin DSL
Fixes gh-8316
1 parent 1de0cf5 commit dc6b8ce

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,56 @@ class HttpSecurityDsl(private val http: HttpSecurity, private val init: HttpSecu
669669
this.http.addFilterAt(filter, atFilter)
670670
}
671671

672+
/**
673+
* Adds the [Filter] after the location of the specified [Filter] class.
674+
*
675+
* Example:
676+
*
677+
* ```
678+
* @EnableWebSecurity
679+
* class SecurityConfig : WebSecurityConfigurerAdapter() {
680+
*
681+
* override fun configure(http: HttpSecurity) {
682+
* http {
683+
* addFilterAfter(CustomFilter(), UsernamePasswordAuthenticationFilter::class.java)
684+
* }
685+
* }
686+
* }
687+
* ```
688+
*
689+
* @param filter the [Filter] to register
690+
* @param afterFilter the location of another [Filter] that is already registered
691+
* (i.e. known) with Spring Security.
692+
*/
693+
fun addFilterAfter(filter: Filter, afterFilter: Class<out Filter>) {
694+
this.http.addFilterAfter(filter, afterFilter)
695+
}
696+
697+
/**
698+
* Adds the [Filter] before the location of the specified [Filter] class.
699+
*
700+
* Example:
701+
*
702+
* ```
703+
* @EnableWebSecurity
704+
* class SecurityConfig : WebSecurityConfigurerAdapter() {
705+
*
706+
* override fun configure(http: HttpSecurity) {
707+
* http {
708+
* addFilterBefore(CustomFilter(), UsernamePasswordAuthenticationFilter::class.java)
709+
* }
710+
* }
711+
* }
712+
* ```
713+
*
714+
* @param filter the [Filter] to register
715+
* @param beforeFilter the location of another [Filter] that is already registered
716+
* (i.e. known) with Spring Security.
717+
*/
718+
fun addFilterBefore(filter: Filter, beforeFilter: Class<out Filter>) {
719+
this.http.addFilterBefore(filter, beforeFilter)
720+
}
721+
672722
/**
673723
* Apply all configurations to the provided [HttpSecurity]
674724
*/

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ class HttpSecurityDslTests {
225225
val filters: List<Filter> = filterChain.getFilters("/")
226226

227227
assertThat(filters).hasSize(1)
228-
assertThat(filters[0]).isExactlyInstanceOf(CustomFilterConfig.CustomFilter::class.java)
228+
assertThat(filters[0]).isExactlyInstanceOf(CustomFilter::class.java)
229229
}
230230

231231
@EnableWebSecurity
@@ -236,7 +236,55 @@ class HttpSecurityDslTests {
236236
addFilterAt(CustomFilter(), UsernamePasswordAuthenticationFilter::class.java)
237237
}
238238
}
239+
}
240+
241+
@Test
242+
fun `HTTP security when custom filter configured then custom filter added after specific filter to filter chain`() {
243+
this.spring.register(CustomFilterAfterConfig::class.java).autowire()
244+
245+
val filterChain = spring.context.getBean(FilterChainProxy::class.java)
246+
val filters: List<Class<out Filter>> = filterChain.getFilters("/").map { it.javaClass }
247+
248+
assertThat(filters).containsSubsequence(
249+
UsernamePasswordAuthenticationFilter::class.java,
250+
CustomFilter::class.java
251+
)
252+
}
253+
254+
@EnableWebSecurity
255+
@EnableWebMvc
256+
open class CustomFilterAfterConfig : WebSecurityConfigurerAdapter() {
257+
override fun configure(http: HttpSecurity) {
258+
http {
259+
addFilterAfter(CustomFilter(), UsernamePasswordAuthenticationFilter::class.java)
260+
formLogin {}
261+
}
262+
}
263+
}
264+
265+
@Test
266+
fun `HTTP security when custom filter configured then custom filter added before specific filter to filter chain`() {
267+
this.spring.register(CustomFilterBeforeConfig::class.java).autowire()
268+
269+
val filterChain = spring.context.getBean(FilterChainProxy::class.java)
270+
val filters: List<Class<out Filter>> = filterChain.getFilters("/").map { it.javaClass }
271+
272+
assertThat(filters).containsSubsequence(
273+
CustomFilter::class.java,
274+
UsernamePasswordAuthenticationFilter::class.java
275+
)
276+
}
239277

240-
class CustomFilter : UsernamePasswordAuthenticationFilter()
278+
@EnableWebSecurity
279+
@EnableWebMvc
280+
open class CustomFilterBeforeConfig : WebSecurityConfigurerAdapter() {
281+
override fun configure(http: HttpSecurity) {
282+
http {
283+
addFilterBefore(CustomFilter(), UsernamePasswordAuthenticationFilter::class.java)
284+
formLogin {}
285+
}
286+
}
241287
}
288+
289+
class CustomFilter : UsernamePasswordAuthenticationFilter()
242290
}

0 commit comments

Comments
 (0)