Skip to content

Commit 69336fb

Browse files
gberche-orangejzheaux
authored andcommitted
Add Multiple Filter Chains Docs
Closes gh-9178
1 parent ab9a310 commit 69336fb

File tree

1 file changed

+56
-1
lines changed
  • docs/manual/src/docs/asciidoc/_includes/reactive

1 file changed

+56
-1
lines changed

docs/manual/src/docs/asciidoc/_includes/reactive/webflux.adoc

+56-1
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,60 @@ class HelloWebfluxSecurityConfig {
125125
This configuration explicitly sets up all the same things as our minimal configuration.
126126
From here you can easily make the changes to the defaults.
127127

128-
You can find more examples of explicit configuration in unit tests, by searching https://github.com/spring-projects/spring-security/search?q=path%3Aconfig%2Fsrc%2Ftest%2F+EnableWebFluxSecurity[EnableWebFluxSecurity in the `config/src/test/` directory], e.g. https://github.com/spring-projects/spring-security/blob/9cf3129d7afa2abb439aba6aadfee0a2c8c784bf/config/src/test/java/org/springframework/security/config/annotation/web/reactive/EnableWebFluxSecurityTests.java#L349-L366[MultiSecurityHttpConfig] illustrating multiple `SecurityWebFilterChain` beans.
128+
You can find more examples of explicit configuration in unit tests, by searching https://github.com/spring-projects/spring-security/search?q=path%3Aconfig%2Fsrc%2Ftest%2F+EnableWebFluxSecurity[EnableWebFluxSecurity in the `config/src/test/` directory].
129+
130+
[[jc-webflux-multiple-filter-chains]]
131+
=== Multiple chains support
132+
133+
We can configure multiple `SecurityWebFilterChain` instances.
134+
135+
For example, the following is an example of having a specific configuration for URL's that start with `/api/`. This overrides the form login configuration with lower precedence.
136+
137+
[source,java]
138+
----
139+
@EnableWebFluxSecurity
140+
@Import(ReactiveAuthenticationTestConfiguration.class)
141+
static class MultiSecurityHttpConfig {
142+
143+
@Order(Ordered.HIGHEST_PRECEDENCE) <1>
144+
@Bean
145+
SecurityWebFilterChain apiHttpSecurity(ServerHttpSecurity http) {
146+
http
147+
.securityMatcher(new PathPatternParserServerWebExchangeMatcher("/api/**")) <2>
148+
.authorizeExchange()
149+
.anyExchange().denyAll();
150+
return http.build();
151+
}
152+
153+
@Bean
154+
SecurityWebFilterChain webFormHttpSecurity(ServerHttpSecurity http) { <3>
155+
http
156+
.authorizeExchange((exchanges) ->
157+
exchanges
158+
.pathMatchers("/login").permitAll()
159+
.anyExchange().authenticated()
160+
)
161+
.httpBasic(withDefaults())
162+
.formLogin((formLogin) -> <4>
163+
formLogin
164+
.loginPage("/login")
165+
);
166+
return http.build();
167+
}
168+
169+
@Bean
170+
public static ReactiveUserDetailsService userDetailsService() {
171+
return new MapReactiveUserDetailsService(PasswordEncodedUser.user(), PasswordEncodedUser.admin());
172+
}
173+
174+
}
175+
176+
----
177+
178+
<1> Configure a SecurityWebFilterChain with an `@Order` to specify which `SecurityWebFilterChain` should be considered first
179+
<2> The `PathPatternParserServerWebExchangeMatcher` states that this `SecurityWebFilterChain` will only be applicable to URLs that start with `/api/`
180+
<3> Create another instance of `SecurityWebFilterChain` with lower precedence.
181+
<4> Some configurations applies to all path matchers within the `webFormHttpSecurity` but not to `apiHttpSecurity` `SecurityWebFilterChain`.
182+
183+
If the URL does not start with `/api/` the `webFormHttpSecurity` configuration will be used.
129184

0 commit comments

Comments
 (0)