Signed-off-by: Andrey Litvitski <andrey1010102008@gmail.com>
This commit is contained in:
Andrey Litvitski 2026-03-27 23:31:55 +03:00 committed by Josh Cummings
parent c3e0b98b7e
commit 6335caabae
7 changed files with 97 additions and 36 deletions

View File

@ -248,13 +248,13 @@ The previous description already gives us a clue on where to add the filter, sin
Based on the rule of thumb, you can add it before xref:servlet/authentication/logout.adoc[`LogoutFilter`], like so:
include-code::./SecurityConfig[tag=snippet-before,indent=0]
include-code::./SecurityConfigBefore[tag=snippet,indent=0]
<1> Use `HttpSecurity#addFilterBefore` to add the `TenantFilter` before the `LogoutFilter`.
Or after xref:servlet/authentication/anonymous.adoc[`AnonymousAuthenticationFilter`], the last authentication filter in the chain, like so:
include-code::./SecurityConfig[tag=snippet-after,indent=0]
include-code::./SecurityConfigAfter[tag=snippet,indent=0]
<1> Use `HttpSecurity#addFilterAfter` to add the `TenantFilter` after the `AnonymousAuthenticationFilter`.

View File

@ -47,7 +47,7 @@ import org.springframework.web.bind.annotation.RestController;
@ContextConfiguration(classes = {
CustomFilterTests.UserDetailsConfig.class,
CustomFilterTests.ApiController.class,
SecurityConfig.class })
SecurityConfigBefore.class })
@WebAppConfiguration
public class CustomFilterTests {

View File

@ -22,32 +22,21 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.test.context.ContextConfiguration;
@Configuration
@ContextConfiguration(classes = { SecurityConfig.class })
@ContextConfiguration(classes = { SecurityConfigAfter.class })
@EnableWebSecurity
public class SecurityConfig {
public class SecurityConfigAfter {
// tag::snippet-before[]
// tag::snippet[]
@Bean
public SecurityFilterChain filterChainBefore(HttpSecurity http) throws Exception {
http
// ...
.addFilterBefore(new TenantFilter(), LogoutFilter.class); // <1>
return http.build();
}
// end::snippet-before[]
// tag::snippet-after[]
@Bean
public SecurityFilterChain filterChainAfter(HttpSecurity http) throws Exception {
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
// ...
.addFilterAfter(new TenantFilter(), AnonymousAuthenticationFilter.class); // <1>
return http.build();
}
// end::snippet-after[]
// end::snippet[]
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2004-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.docs.servlet.addingcustomfilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.test.context.ContextConfiguration;
@Configuration
@ContextConfiguration(classes = { SecurityConfigBefore.class })
@EnableWebSecurity
public class SecurityConfigBefore {
// tag::snippet[]
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
// ...
.addFilterBefore(new TenantFilter(), LogoutFilter.class); // <1>
return http.build();
}
// end::snippet[]
}

View File

@ -47,7 +47,7 @@ import org.springframework.web.bind.annotation.RestController
classes = [
CustomFilterTests.UserDetailsConfig::class,
CustomFilterTests.ApiController::class,
SecurityConfig::class
SecurityConfigBefore::class
]
)
@WebAppConfiguration

View File

@ -0,0 +1,42 @@
/*
* Copyright 2004-present 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.kt.docs.servlet.addingcustomfilter
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.invoke
import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter
@Configuration
@EnableWebSecurity
open class SecurityConfigAfter {
// tag::snippet[]
@Bean
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
http {
// ...
addFilterAfter<AnonymousAuthenticationFilter>(TenantFilter()) // <1>
}
return http.build()
}
// end::snippet[]
}

View File

@ -22,33 +22,21 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.invoke
import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter
import org.springframework.security.web.authentication.logout.LogoutFilter
@Configuration
@EnableWebSecurity
open class SecurityConfig {
open class SecurityConfigBefore {
// tag::snippet-before[]
// tag::snippet[]
@Bean
open fun filterChainBefore(http: HttpSecurity): SecurityFilterChain {
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
http {
// ...
addFilterBefore<LogoutFilter>(TenantFilter()) // <1>
}
return http.build()
}
// end::snippet-before[]
// tag::snippet-after[]
@Bean
open fun filterChainAfter(http: HttpSecurity): SecurityFilterChain {
http {
// ...
addFilterAfter<AnonymousAuthenticationFilter>(TenantFilter()) // <1>
}
return http.build()
}
// end::snippet-after[]
// end::snippet[]
}