Skip to content

Commit 9934553

Browse files
committed
Add RequestMatcher Migration Path for AbstractAuthenticationProcessingFilter
Issue gh-16417
1 parent 91ee5e7 commit 9934553

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

Diff for: docs/modules/ROOT/pages/migration/web.adoc

+55
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,61 @@ switchUser.setExitUserMatcher(PathPatternRequestMatcher.withDefaults().matcher(H
9494
----
9595
======
9696

97+
=== Migrate `filterProcessingUrl` Request Matcher in `AbstractAuthenticationProcessingFilter` Implementations
98+
99+
Spring Security 6 converts any processing endpoint configured through `setFilterProcessingUrl` to an `AntPathRequestMatcher`.
100+
In Spring Security 7, this will change to `PathPatternRequestMatcher`.
101+
102+
If you are directly invoking `setFilterProcessingUrl` on a filter that extends `AbstractAuthenticationProcessingFilter`, like `UsernamePasswordAuthenticationFilter`, `OAuth2LoginAuthenticationFilter`, `Saml2WebSsoAuthenticationFilter`, `OneTimeTokenAuthenticationFilter`, or `WebAuthnAuthenticationFilter`, call `setRequiredAuthenticationRequestMatcher` instead to provide this `PathPatternRequestMatcher` in advance.
103+
104+
That is, change this:
105+
[tabs]
106+
======
107+
Java::
108+
+
109+
[source,java,role="primary"]
110+
----
111+
UsernamePasswordAuthenticationFilter usernamePassword = new UsernamePasswordAuthenticationFilter(authenticationManager);
112+
usernamePassword.setFilterProcessingUrl("/my/processing/url");
113+
----
114+
115+
Kotlin::
116+
+
117+
[source,kotlin,role="secondary"]
118+
----
119+
val usernamePassword = UsernamePasswordAuthenticationFilter(authenticationManager)
120+
usernamePassword.setFilterProcessingUrl("/my/processing/url")
121+
----
122+
======
123+
124+
to this:
125+
126+
[tabs]
127+
======
128+
Java::
129+
+
130+
[source,java,role="primary"]
131+
----
132+
UsernamePasswordAuthenticationFilter usernamePassword = new UsernamePasswordAuthenticationFilter(authenticationManager);
133+
RequestMatcher requestMatcher = PathPatternRequestMatcher.withDefaults().matcher("/my/processing/url");
134+
usernamePassword.setRequest(requestMatcher);
135+
----
136+
137+
Kotlin::
138+
+
139+
[source,kotlin,role="secondary"]
140+
----
141+
val usernamePassword = UsernamePasswordAuthenticationFilter(authenticationManager)
142+
val requestMatcher = PathPatternRequestMatcher.withDefaults().matcher("/my/processing/url")
143+
usernamePassword.setRequest(requestMatcher)
144+
----
145+
======
146+
147+
[NOTE]
148+
-----
149+
Most applications use the DSL instead of setting the `filterProcessingUrl` directly on a filter instance.
150+
-----
151+
97152
=== Migrate CAS Proxy Receptor Request Matcher
98153

99154
Spring Security 6 converts any configured `proxyReceptorUrl` to a request matcher that matches the end of the request, that is `/**/proxy/receptor`.

Diff for: oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/web/OAuth2LoginAuthenticationFilter.java

+4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
4040
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
4141
import org.springframework.security.web.context.SecurityContextRepository;
42+
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
4243
import org.springframework.security.web.util.UrlUtils;
44+
import org.springframework.security.web.util.matcher.RequestMatcher;
4345
import org.springframework.util.Assert;
4446
import org.springframework.util.MultiValueMap;
4547
import org.springframework.web.util.UriComponentsBuilder;
@@ -123,6 +125,8 @@ public class OAuth2LoginAuthenticationFilter extends AbstractAuthenticationProce
123125
public OAuth2LoginAuthenticationFilter(ClientRegistrationRepository clientRegistrationRepository,
124126
OAuth2AuthorizedClientService authorizedClientService) {
125127
this(clientRegistrationRepository, authorizedClientService, DEFAULT_FILTER_PROCESSES_URI);
128+
RequestMatcher processUri = PathPatternRequestMatcher.withDefaults().matcher(DEFAULT_FILTER_PROCESSES_URI);
129+
setRequiresAuthenticationRequestMatcher(processUri);
126130
}
127131

128132
/**

Diff for: saml2/saml2-service-provider/src/main/java/org/springframework/security/saml2/provider/service/web/authentication/Saml2WebSsoAuthenticationFilter.java

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public class Saml2WebSsoAuthenticationFilter extends AbstractAuthenticationProce
6363
*/
6464
public Saml2WebSsoAuthenticationFilter(RelyingPartyRegistrationRepository relyingPartyRegistrationRepository) {
6565
this(relyingPartyRegistrationRepository, DEFAULT_FILTER_PROCESSES_URI);
66+
RequestMatcher processUri = PathPatternRequestMatcher.withDefaults().matcher(DEFAULT_FILTER_PROCESSES_URI);
67+
setRequiresAuthenticationRequestMatcher(processUri);
6668
}
6769

6870
/**

Diff for: web/src/main/java/org/springframework/security/web/authentication/AbstractAuthenticationProcessingFilter.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
4747
import org.springframework.security.web.context.RequestAttributeSecurityContextRepository;
4848
import org.springframework.security.web.context.SecurityContextRepository;
49-
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
49+
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
5050
import org.springframework.security.web.util.matcher.RequestMatcher;
5151
import org.springframework.util.Assert;
5252
import org.springframework.web.filter.GenericFilterBean;
@@ -395,7 +395,7 @@ public void setAuthenticationManager(AuthenticationManager authenticationManager
395395
* @param filterProcessesUrl
396396
*/
397397
public void setFilterProcessesUrl(String filterProcessesUrl) {
398-
setRequiresAuthenticationRequestMatcher(PathPatternRequestMatcher.withDefaults().matcher(filterProcessesUrl));
398+
setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher(filterProcessesUrl));
399399
}
400400

401401
public final void setRequiresAuthenticationRequestMatcher(RequestMatcher requestMatcher) {

Diff for: web/src/test/java/org/springframework/security/web/authentication/AbstractAuthenticationProcessingFilterTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public void testStartupDetectsInvalidFilterProcessesUrl() {
273273
filter.setAuthenticationManager(mock(AuthenticationManager.class));
274274
filter.setAuthenticationSuccessHandler(this.successHandler);
275275
assertThatIllegalArgumentException().isThrownBy(() -> filter.setFilterProcessesUrl(null))
276-
.withMessage("pattern cannot be null");
276+
.withMessage("Pattern cannot be null or empty");
277277
}
278278

279279
@Test

0 commit comments

Comments
 (0)