Skip to content

Commit 4724f16

Browse files
Add Alerting About Deprecated Authorize Config
Closes gh-16213
1 parent 6d4bd07 commit 4724f16

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/builders/WebSecurityFilterChainValidator.java

+34
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@
1818

1919
import java.util.List;
2020

21+
import jakarta.servlet.Filter;
22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
2125
import org.springframework.security.web.DefaultSecurityFilterChain;
2226
import org.springframework.security.web.FilterChainProxy;
2327
import org.springframework.security.web.SecurityFilterChain;
2428
import org.springframework.security.web.UnreachableFilterChainException;
29+
import org.springframework.security.web.access.intercept.AuthorizationFilter;
30+
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
2531
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
2632

2733
/**
@@ -33,11 +39,14 @@
3339
*/
3440
final class WebSecurityFilterChainValidator implements FilterChainProxy.FilterChainValidator {
3541

42+
private final Log logger = LogFactory.getLog(getClass());
43+
3644
@Override
3745
public void validate(FilterChainProxy filterChainProxy) {
3846
List<SecurityFilterChain> chains = filterChainProxy.getFilterChains();
3947
checkForAnyRequestRequestMatcher(chains);
4048
checkForDuplicateMatchers(chains);
49+
checkAuthorizationFilters(chains);
4150
}
4251

4352
private void checkForAnyRequestRequestMatcher(List<SecurityFilterChain> chains) {
@@ -76,4 +85,29 @@ private void checkForDuplicateMatchers(List<SecurityFilterChain> chains) {
7685
}
7786
}
7887

88+
private void checkAuthorizationFilters(List<SecurityFilterChain> chains) {
89+
Filter authorizationFilter = null;
90+
Filter filterSecurityInterceptor = null;
91+
for (SecurityFilterChain chain : chains) {
92+
for (Filter filter : chain.getFilters()) {
93+
if (filter instanceof AuthorizationFilter) {
94+
authorizationFilter = filter;
95+
}
96+
if (filter instanceof FilterSecurityInterceptor) {
97+
filterSecurityInterceptor = filter;
98+
}
99+
}
100+
if (authorizationFilter != null && filterSecurityInterceptor != null) {
101+
logger.warn(
102+
"It is not recommended to use authorizeRequests in the configuration. Please only use authorizeHttpRequests");
103+
}
104+
if (filterSecurityInterceptor != null) {
105+
logger.warn(
106+
"Usage of authorizeRequests is deprecated. Please use authorizeHttpRequests in the configuration");
107+
}
108+
authorizationFilter = null;
109+
filterSecurityInterceptor = null;
110+
}
111+
}
112+
79113
}

config/src/main/java/org/springframework/security/config/http/DefaultFilterChainValidator.java

+26
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public void validate(FilterChainProxy fcp) {
6969
}
7070
checkPathOrder(new ArrayList<>(fcp.getFilterChains()));
7171
checkForDuplicateMatchers(new ArrayList<>(fcp.getFilterChains()));
72+
checkAuthorizationFilters(new ArrayList<>(fcp.getFilterChains()));
7273
}
7374

7475
private void checkPathOrder(List<SecurityFilterChain> filterChains) {
@@ -107,6 +108,31 @@ private void checkForDuplicateMatchers(List<SecurityFilterChain> chains) {
107108
}
108109
}
109110

111+
private void checkAuthorizationFilters(List<SecurityFilterChain> chains) {
112+
Filter authorizationFilter = null;
113+
Filter filterSecurityInterceptor = null;
114+
for (SecurityFilterChain chain : chains) {
115+
for (Filter filter : chain.getFilters()) {
116+
if (filter instanceof AuthorizationFilter) {
117+
authorizationFilter = filter;
118+
}
119+
if (filter instanceof FilterSecurityInterceptor) {
120+
filterSecurityInterceptor = filter;
121+
}
122+
}
123+
if (authorizationFilter != null && filterSecurityInterceptor != null) {
124+
this.logger.warn(
125+
"It is not recommended to use authorizeRequests in the configuration. Please only use authorizeHttpRequests");
126+
}
127+
if (filterSecurityInterceptor != null) {
128+
this.logger.warn(
129+
"Usage of authorizeRequests is deprecated. Please use authorizeHttpRequests in the configuration");
130+
}
131+
authorizationFilter = null;
132+
filterSecurityInterceptor = null;
133+
}
134+
}
135+
110136
@SuppressWarnings({ "unchecked" })
111137
private <F extends Filter> F getFilter(Class<F> type, List<Filter> filters) {
112138
for (Filter f : filters) {

config/src/test/java/org/springframework/security/config/annotation/web/builders/WebSecurityFilterChainValidatorTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.security.web.util.matcher.AnyRequestMatcher;
3636

3737
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
38+
import static org.assertj.core.api.Assertions.assertThatNoException;
3839

3940
/**
4041
* Tests for {@link WebSecurityFilterChainValidator}
@@ -55,6 +56,15 @@ public class WebSecurityFilterChainValidatorTests {
5556
@Mock
5657
private FilterSecurityInterceptor authorizationInterceptor;
5758

59+
@Test
60+
void validateWhenFilterSecurityInterceptorConfiguredThenValidates() {
61+
SecurityFilterChain chain = new DefaultSecurityFilterChain(AntPathRequestMatcher.antMatcher("/api"),
62+
this.authenticationFilter, this.exceptionTranslationFilter, this.authorizationInterceptor);
63+
FilterChainProxy proxy = new FilterChainProxy(List.of(chain));
64+
65+
assertThatNoException().isThrownBy(() -> this.validator.validate(proxy));
66+
}
67+
5868
@Test
5969
void validateWhenAnyRequestMatcherIsPresentThenUnreachableFilterChainException() {
6070
SecurityFilterChain chain1 = new DefaultSecurityFilterChain(AntPathRequestMatcher.antMatcher("/api"),

config/src/test/java/org/springframework/security/config/http/DefaultFilterChainValidatorTests.java

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.springframework.test.util.ReflectionTestUtils;
5050

5151
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
52+
import static org.assertj.core.api.Assertions.assertThatNoException;
5253
import static org.mockito.ArgumentMatchers.any;
5354
import static org.mockito.BDDMockito.given;
5455
import static org.mockito.BDDMockito.willThrow;
@@ -103,6 +104,11 @@ public void setUp() {
103104
ReflectionTestUtils.setField(this.validator, "logger", this.logger);
104105
}
105106

107+
@Test
108+
void validateWhenFilterSecurityInterceptorConfiguredThenValidates() {
109+
assertThatNoException().isThrownBy(() -> this.validator.validate(this.chain));
110+
}
111+
106112
// SEC-1878
107113
@SuppressWarnings("unchecked")
108114
@Test

0 commit comments

Comments
 (0)