@@ -22,6 +22,8 @@ import io.mockk.verify
22
22
import org.assertj.core.api.Assertions.assertThat
23
23
import org.junit.jupiter.api.Test
24
24
import org.junit.jupiter.api.extension.ExtendWith
25
+ import org.junit.jupiter.params.ParameterizedTest
26
+ import org.junit.jupiter.params.provider.ValueSource
25
27
import org.springframework.beans.factory.annotation.Autowired
26
28
import org.springframework.context.annotation.Bean
27
29
import org.springframework.context.annotation.Configuration
@@ -32,13 +34,15 @@ import org.springframework.security.authentication.TestingAuthenticationProvider
32
34
import org.springframework.security.authentication.TestingAuthenticationToken
33
35
import org.springframework.security.config.annotation.web.builders.HttpSecurity
34
36
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
37
+ import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer
35
38
import org.springframework.security.config.test.SpringTestContext
36
39
import org.springframework.security.config.test.SpringTestContextExtension
37
40
import org.springframework.security.core.userdetails.User
38
41
import org.springframework.security.core.userdetails.UserDetailsService
39
42
import org.springframework.security.provisioning.InMemoryUserDetailsManager
40
43
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic
41
44
import org.springframework.security.web.FilterChainProxy
45
+ import org.springframework.security.web.SecurityFilterChain
42
46
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
43
47
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter
44
48
import org.springframework.security.web.server.header.ContentTypeOptionsServerHttpHeadersWriter
@@ -52,9 +56,6 @@ import org.springframework.test.web.servlet.post
52
56
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
53
57
import org.springframework.web.servlet.config.annotation.EnableWebMvc
54
58
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
58
59
59
60
/* *
60
61
* Tests for [HttpSecurityDsl]
@@ -516,4 +517,42 @@ class HttpSecurityDslTests {
516
517
}
517
518
518
519
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
+ }
519
558
}
0 commit comments