Skip to content

Commit 57d6ab7

Browse files
Improve docs on dispatcherTypeMatcher
Closes gh-11467
1 parent 624fdfa commit 57d6ab7

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

docs/modules/ROOT/pages/servlet/authorization/authorize-http-requests.adoc

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,78 @@ open fun web(http: HttpSecurity): SecurityFilterChain {
205205
}
206206
----
207207
====
208+
209+
Now with the authorization rules applying to all dispatcher types, you have more control of the authorization on them.
210+
For example, you may want to configure `shouldFilterAllDispatcherTypes` to `true` but not apply authorization on requests with dispatcher type `ASYNC` or `FORWARD`.
211+
212+
.Permit ASYNC and FORWARD dispatcher type
213+
====
214+
.Java
215+
[source,java,role="primary"]
216+
----
217+
@Bean
218+
SecurityFilterChain web(HttpSecurity http) throws Exception {
219+
http
220+
.authorizeHttpRequests((authorize) -> authorize
221+
.shouldFilterAllDispatcherTypes(true)
222+
.dispatcherTypeMatchers(DispatcherType.ASYNC, DispatcherType.FORWARD).permitAll()
223+
.anyRequest().authenticated()
224+
)
225+
// ...
226+
227+
return http.build();
228+
}
229+
----
230+
.Kotlin
231+
[source,kotlin,role="secondary"]
232+
----
233+
@Bean
234+
open fun web(http: HttpSecurity): SecurityFilterChain {
235+
http {
236+
authorizeHttpRequests {
237+
shouldFilterAllDispatcherTypes = true
238+
authorize(DispatcherTypeRequestMatcher(DispatcherType.ASYNC, DispatcherType.FORWARD), permitAll)
239+
authorize(anyRequest, authenticated)
240+
}
241+
}
242+
return http.build()
243+
}
244+
----
245+
====
246+
247+
You can also customize it to require a specific role for a dispatcher type:
248+
249+
.Require ADMIN for Dispatcher Type ERROR
250+
====
251+
.Java
252+
[source,java,role="primary"]
253+
----
254+
@Bean
255+
SecurityFilterChain web(HttpSecurity http) throws Exception {
256+
http
257+
.authorizeHttpRequests((authorize) -> authorize
258+
.shouldFilterAllDispatcherTypes(true)
259+
.dispatcherTypeMatchers(DispatcherType.ERROR).hasRole("ADMIN")
260+
.anyRequest().authenticated()
261+
)
262+
// ...
263+
264+
return http.build();
265+
}
266+
----
267+
.Kotlin
268+
[source,kotlin,role="secondary"]
269+
----
270+
@Bean
271+
open fun web(http: HttpSecurity): SecurityFilterChain {
272+
http {
273+
authorizeHttpRequests {
274+
shouldFilterAllDispatcherTypes = true
275+
authorize(DispatcherTypeRequestMatcher(DispatcherType.ERROR), hasRole("ADMIN"))
276+
authorize(anyRequest, authenticated)
277+
}
278+
}
279+
return http.build()
280+
}
281+
----
282+
====

docs/modules/ROOT/pages/servlet/authorization/authorize-requests.adoc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,71 @@ You will notice that since we are invoking the `hasRole` method we do not need t
137137
You will notice that since we are using the `hasRole` expression we do not need to specify the "ROLE_" prefix.
138138
<5> Any URL that has not already been matched on is denied access.
139139
This is a good strategy if you do not want to accidentally forget to update your authorization rules.
140+
141+
[[filtersecurityinterceptor-every-request]]
142+
== Apply FilterSecurityInterceptor to every request
143+
144+
By default, the `FilterSecurityInterceptor` only applies once to a request.
145+
This means that if a request is dispatched from a request that was already filtered, the `FilterSecurityInterceptor` will back-off and not perform any authorization checks.
146+
In some scenarios, you may want to apply the filter to every request.
147+
You can configure Spring Security to apply the authorization rules to every request by using the `filterSecurityInterceptorOncePerRequest` method:
148+
149+
.Set filterSecurityInterceptorOncePerRequest to false
150+
====
151+
.Java
152+
[source,java,role="primary"]
153+
----
154+
@Bean
155+
SecurityFilterChain web(HttpSecurity http) throws Exception {
156+
http
157+
.authorizeRequests((authorize) -> authorize
158+
.filterSecurityInterceptorOncePerRequest(false)
159+
.anyRequest.authenticated()
160+
)
161+
// ...
162+
163+
return http.build();
164+
}
165+
----
166+
.XML
167+
[source,xml]
168+
----
169+
<http once-per-request="false">
170+
<intercept-url pattern="/**" access="authenticated"/>
171+
</http>
172+
----
173+
====
174+
175+
You can also configure authorization based on the request dispatcher type:
176+
177+
.Permit ASYNC dispatcher type
178+
====
179+
.Java
180+
[source,java,role="primary"]
181+
----
182+
@Bean
183+
SecurityFilterChain web(HttpSecurity http) throws Exception {
184+
http
185+
.authorizeRequests((authorize) -> authorize
186+
.filterSecurityInterceptorOncePerRequest(false)
187+
.dispatcherTypeMatchers(DispatcherType.ASYNC).permitAll()
188+
.anyRequest.authenticated()
189+
)
190+
// ...
191+
192+
return http.build();
193+
}
194+
----
195+
.XML
196+
[source,xml]
197+
----
198+
<http auto-config="true" once-per-request="false">
199+
<intercept-url request-matcher-ref="dispatcherTypeMatcher" access="permitAll" />
200+
<intercept-url pattern="/**" access="authenticated"/>
201+
</http>
202+
203+
<b:bean id="dispatcherTypeMatcher" class="org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher">
204+
<b:constructor-arg value="ASYNC"/>
205+
</b:bean>
206+
----
207+
====

0 commit comments

Comments
 (0)