Skip to content

Commit 8b7751f

Browse files
committed
Polish Multiple Filter Chains Docs
Issue gh-9178
1 parent 69336fb commit 8b7751f

File tree

1 file changed

+44
-43
lines changed
  • docs/manual/src/docs/asciidoc/_includes/reactive

1 file changed

+44
-43
lines changed

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

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -128,57 +128,58 @@ From here you can easily make the changes to the defaults.
128128
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].
129129

130130
[[jc-webflux-multiple-filter-chains]]
131-
=== Multiple chains support
131+
=== Multiple Chains Support
132132

133-
We can configure multiple `SecurityWebFilterChain` instances.
133+
You can configure multiple `SecurityWebFilterChain` instances to separate configuration by `RequestMatcher` s.
134134

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.
135+
For example, you can isolate configuration for URLs that start with `/api`, like so:
136136

137137
[source,java]
138138
----
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-
}
139+
@Configuration
140+
@EnableWebFluxSecurity
141+
static class MultiSecurityHttpConfig {
173142
174-
}
143+
@Order(Ordered.HIGHEST_PRECEDENCE) <1>
144+
@Bean
145+
SecurityWebFilterChain apiHttpSecurity(ServerHttpSecurity http) {
146+
http
147+
.securityMatcher(new PathPatternParserServerWebExchangeMatcher("/api/**")) <2>
148+
.authorizeExchange((exchanges) -> exchanges
149+
.anyExchange().authenticated()
150+
)
151+
.oauth2ResourceServer(OAuth2ResourceServerSpec::jwt); <3>
152+
return http.build();
153+
}
175154
155+
@Bean
156+
SecurityWebFilterChain webHttpSecurity(ServerHttpSecurity http) { <4>
157+
http
158+
.authorizeExchange((exchanges) -> exchanges
159+
.anyExchange().authenticated()
160+
)
161+
.httpBasic(withDefaults()) <5>
162+
return http.build();
163+
}
164+
165+
@Bean
166+
ReactiveUserDetailsService userDetailsService() {
167+
return new MapReactiveUserDetailsService(
168+
PasswordEncodedUser.user(), PasswordEncodedUser.admin());
169+
}
170+
171+
}
176172
----
177173

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`.
174+
<1> Configure a `SecurityWebFilterChain` with an `@Order` to specify which `SecurityWebFilterChain` Spring Security should consider first
175+
<2> Use `PathPatternParserServerWebExchangeMatcher` to state that this `SecurityWebFilterChain` will only apply to URL paths that start with `/api/`
176+
<3> Specify the authentication mechanisms that will be used for `/api/**` endpoints
177+
<4> Create another instance of `SecurityWebFilterChain` with lower precedence to match all other URLs
178+
<5> Specify the authentication mechanisms that will be used for the rest of the application
179+
180+
Spring Security will select one `SecurityWebFilterChain` `@Bean` for each request.
181+
It will match the requests in order by the `securityMatcher` definition.
182182

183-
If the URL does not start with `/api/` the `webFormHttpSecurity` configuration will be used.
183+
In this case, that means that if the URL path starts with `/api`, then Spring Security will use `apiHttpSecurity`.
184+
If the URL does not start with `/api` then Spring Security will default to `webHttpSecurity`, which has an implied `securityMatcher` that matches any request.
184185

0 commit comments

Comments
 (0)