Skip to content

Commit ccb19db

Browse files
Add XML support for shouldFilterAllDispatcherTypes
Closes gh-11492
1 parent 8a5aed2 commit ccb19db

File tree

6 files changed

+116
-3
lines changed

6 files changed

+116
-3
lines changed

Diff for: config/src/main/java/org/springframework/security/config/http/AuthorizationFilterParser.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -60,6 +60,8 @@ class AuthorizationFilterParser implements BeanDefinitionParser {
6060

6161
private static final String ATT_SERVLET_PATH = "servlet-path";
6262

63+
private static final String ATT_FILTER_ALL_DISPATCHER_TYPES = "filter-all-dispatcher-types";
64+
6365
private String authorizationManagerRef;
6466

6567
private final BeanMetadataElement securityContextHolderStrategy;
@@ -83,7 +85,11 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
8385
this.authorizationManagerRef = createAuthorizationManager(element, parserContext);
8486
BeanDefinitionBuilder filterBuilder = BeanDefinitionBuilder.rootBeanDefinition(AuthorizationFilter.class);
8587
filterBuilder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
86-
BeanDefinition filter = filterBuilder.addConstructorArgReference(this.authorizationManagerRef)
88+
filterBuilder.addConstructorArgReference(this.authorizationManagerRef);
89+
if ("true".equals(element.getAttribute(ATT_FILTER_ALL_DISPATCHER_TYPES))) {
90+
filterBuilder.addPropertyValue("shouldFilterAllDispatcherTypes", Boolean.TRUE);
91+
}
92+
BeanDefinition filter = filterBuilder
8793
.addPropertyValue("securityContextHolderStrategy", this.securityContextHolderStrategy)
8894
.getBeanDefinition();
8995
String id = element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE);

Diff for: config/src/main/resources/org/springframework/security/config/spring-security-5.8.rnc

+3
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ http.attlist &=
385385
http.attlist &=
386386
## Corresponds to the observeOncePerRequest property of FilterSecurityInterceptor. Defaults to "true"
387387
attribute once-per-request {xsd:boolean}?
388+
http.attlist &=
389+
## Corresponds to the shouldFilterAllDispatcherTypes property of AuthorizationFilter. Only works when use-authorization-manager=true. Defauls to "false".
390+
attribute filter-all-dispatcher-types {xsd:boolean}?
388391
http.attlist &=
389392
## Prevents the jsessionid parameter from being added to rendered URLs. Defaults to "true" (rewriting is disabled).
390393
attribute disable-url-rewriting {xsd:boolean}?

Diff for: config/src/main/resources/org/springframework/security/config/spring-security-5.8.xsd

+7
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,13 @@
13851385
</xs:documentation>
13861386
</xs:annotation>
13871387
</xs:attribute>
1388+
<xs:attribute name="filter-all-dispatcher-types" type="xs:boolean">
1389+
<xs:annotation>
1390+
<xs:documentation>Corresponds to the shouldFilterAllDispatcherTypes property of AuthorizationFilter. Only
1391+
works when use-authorization-manager=true. Defauls to "false".
1392+
</xs:documentation>
1393+
</xs:annotation>
1394+
</xs:attribute>
13881395
<xs:attribute name="disable-url-rewriting" type="xs:boolean">
13891396
<xs:annotation>
13901397
<xs:documentation>Prevents the jsessionid parameter from being added to rendered URLs. Defaults to "true"

Diff for: config/src/test/java/org/springframework/security/config/http/InterceptUrlConfigTests.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.Map;
2121

22+
import javax.servlet.DispatcherType;
2223
import javax.servlet.ServletRegistration;
2324

2425
import org.junit.jupiter.api.Test;
@@ -33,10 +34,12 @@
3334
import org.springframework.security.config.test.SpringTestContextExtension;
3435
import org.springframework.test.web.servlet.MockMvc;
3536
import org.springframework.test.web.servlet.request.RequestPostProcessor;
37+
import org.springframework.web.bind.annotation.GetMapping;
3638
import org.springframework.web.bind.annotation.PathVariable;
3739
import org.springframework.web.bind.annotation.RequestMapping;
3840
import org.springframework.web.bind.annotation.RestController;
3941
import org.springframework.web.context.ConfigurableWebApplicationContext;
42+
import org.springframework.web.util.WebUtils;
4043

4144
import static org.assertj.core.api.Assertions.assertThat;
4245
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -373,6 +376,29 @@ public void configureWhenUsingDefaultMatcherAndServletPathAndAuthorizationManage
373376
.configLocations(this.xml("DefaultMatcherServletPathAuthorizationManager")).autowire());
374377
}
375378

379+
@Test
380+
public void requestWhenUsingFilterAllDispatcherTypesAndAuthorizationManagerThenAuthorizesRequestsAccordingly()
381+
throws Exception {
382+
this.spring.configLocations(this.xml("AuthorizationManagerFilterAllDispatcherTypes")).autowire();
383+
// @formatter:off
384+
this.mvc.perform(get("/path").with(userCredentials()))
385+
.andExpect(status().isOk());
386+
this.mvc.perform(get("/path").with(adminCredentials()))
387+
.andExpect(status().isForbidden());
388+
this.mvc.perform(get("/error").with((request) -> {
389+
request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/error");
390+
request.setDispatcherType(DispatcherType.ERROR);
391+
return request;
392+
})).andExpect(status().isOk());
393+
this.mvc.perform(get("/path").with((request) -> {
394+
request.setAttribute(WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, "/path");
395+
request.setDispatcherType(DispatcherType.ERROR);
396+
return request;
397+
})).andExpect(status().isUnauthorized());
398+
// @formatter:on
399+
assertThat(this.spring.getContext().getBean(AuthorizationManager.class)).isNotNull();
400+
}
401+
376402
private static RequestPostProcessor adminCredentials() {
377403
return httpBasic("admin", "password");
378404
}
@@ -410,6 +436,16 @@ String path(@PathVariable("un") String name) {
410436

411437
}
412438

439+
@RestController
440+
static class ErrorController {
441+
442+
@GetMapping("/error")
443+
String error() {
444+
return "error";
445+
}
446+
447+
}
448+
413449
public static class Id {
414450

415451
public boolean isOne(int i) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2002-2022 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xmlns="http://www.springframework.org/schema/security"
21+
xsi:schemaLocation="
22+
http://www.springframework.org/schema/security
23+
https://www.springframework.org/schema/security/spring-security.xsd
24+
http://www.springframework.org/schema/beans
25+
https://www.springframework.org/schema/beans/spring-beans.xsd">
26+
27+
<http auto-config="true" use-authorization-manager="true" filter-all-dispatcher-types="true">
28+
<intercept-url request-matcher-ref="pathErrorRequestMatcher" access="permitAll()" />
29+
<intercept-url request-matcher-ref="errorRequestMatcher" access="authenticated" />
30+
<intercept-url pattern="/**" access="hasRole('USER')"/>
31+
<http-basic/>
32+
</http>
33+
34+
<b:bean name="path" class="org.springframework.security.config.http.InterceptUrlConfigTests.PathController"/>
35+
<b:bean name="error" class="org.springframework.security.config.http.InterceptUrlConfigTests.ErrorController"/>
36+
37+
<b:bean name="errorRequestMatcher" class="org.springframework.security.web.util.matcher.DispatcherTypeRequestMatcher">
38+
<b:constructor-arg value="ERROR"/>
39+
</b:bean>
40+
41+
<b:bean name="errorPathRequestMatcher" class="org.springframework.security.web.util.matcher.AntPathRequestMatcher">
42+
<b:constructor-arg value="/error"/>
43+
</b:bean>
44+
45+
<b:bean name="pathErrorRequestMatcher" class="org.springframework.security.web.util.matcher.AndRequestMatcher">
46+
<b:constructor-arg>
47+
<b:list>
48+
<b:ref bean="errorRequestMatcher"/>
49+
<b:ref bean="errorPathRequestMatcher"/>
50+
</b:list>
51+
</b:constructor-arg>
52+
</b:bean>
53+
54+
<b:import resource="userservice.xml"/>
55+
</b:beans>

Diff for: docs/modules/ROOT/pages/servlet/appendix/namespace/http.adoc

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ Corresponds to the `observeOncePerRequest` property of `FilterSecurityIntercepto
9797
Defaults to `true`.
9898

9999

100+
[[nsa-http-filter-all-dispatcher-types]]
101+
* **filter-all-dispatcher-types**
102+
Corresponds to the `shouldFilterAllDispatcherTypes` property of the `AuthorizationFilter`. Only works when `use-authorization-manager=true`.
103+
Defaults to `false`.
104+
105+
100106
[[nsa-http-pattern]]
101107
* **pattern**
102108
Defining a pattern for the <<nsa-http,http>> element controls the requests which will be filtered through the list of filters which it defines.

0 commit comments

Comments
 (0)