@@ -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
@@ -30,16 +32,19 @@ import org.springframework.security.authentication.AuthenticationManager
30
32
import org.springframework.security.authentication.ProviderManager
31
33
import org.springframework.security.authentication.TestingAuthenticationProvider
32
34
import org.springframework.security.authentication.TestingAuthenticationToken
35
+ import org.springframework.security.config.annotation.web.HttpSecurityBuilder
33
36
import org.springframework.security.config.annotation.web.builders.HttpSecurity
34
37
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
35
38
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
39
+ import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer
36
40
import org.springframework.security.config.test.SpringTestContext
37
41
import org.springframework.security.config.test.SpringTestContextExtension
38
42
import org.springframework.security.core.userdetails.User
39
43
import org.springframework.security.core.userdetails.UserDetailsService
40
44
import org.springframework.security.provisioning.InMemoryUserDetailsManager
41
45
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic
42
46
import org.springframework.security.web.FilterChainProxy
47
+ import org.springframework.security.web.SecurityFilterChain
43
48
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
44
49
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter
45
50
import org.springframework.security.web.server.header.ContentTypeOptionsServerHttpHeadersWriter
@@ -53,9 +58,6 @@ import org.springframework.test.web.servlet.post
53
58
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
54
59
import org.springframework.web.servlet.config.annotation.EnableWebMvc
55
60
import javax.servlet.Filter
56
- import org.junit.jupiter.params.ParameterizedTest
57
- import org.junit.jupiter.params.provider.ValueSource
58
- import org.springframework.security.web.SecurityFilterChain
59
61
60
62
/* *
61
63
* Tests for [HttpSecurityDsl]
@@ -483,4 +485,42 @@ class HttpSecurityDslTests {
483
485
}
484
486
485
487
class CustomFilter : UsernamePasswordAuthenticationFilter ()
488
+
489
+ @Test
490
+ fun `HTTP security when apply custom security configurer then custom filter added to filter chain` () {
491
+ this .spring.register(CustomSecurityConfigurerConfig ::class .java).autowire()
492
+
493
+ val filterChain = spring.context.getBean(FilterChainProxy ::class .java)
494
+ val filterClasses: List <Class <out Filter >> = filterChain.getFilters(" /" ).map { it.javaClass }
495
+
496
+ assertThat(filterClasses).contains(
497
+ CustomFilter ::class .java
498
+ )
499
+ }
500
+
501
+ @Configuration
502
+ @EnableWebSecurity
503
+ @EnableWebMvc
504
+ open class CustomSecurityConfigurerConfig {
505
+ @Bean
506
+ open fun securityFilterChain (http : HttpSecurity ): SecurityFilterChain {
507
+ http {
508
+ apply (CustomSecurityConfigurer <HttpSecurity >()) {
509
+ filter = CustomFilter ()
510
+ }
511
+ }
512
+ return http.build()
513
+ }
514
+ }
515
+
516
+ class CustomSecurityConfigurer <H : HttpSecurityBuilder <H >> : AbstractHttpConfigurer <CustomSecurityConfigurer <H >, H >() {
517
+ var filter: Filter ? = null
518
+ override fun init (builder : H ) {
519
+ filter = filter ? : UsernamePasswordAuthenticationFilter ()
520
+ }
521
+
522
+ override fun configure (builder : H ) {
523
+ builder.addFilterBefore(CustomFilter (), UsernamePasswordAuthenticationFilter ::class .java)
524
+ }
525
+ }
486
526
}
0 commit comments