Skip to content

Commit 91ee5e7

Browse files
committed
Add RequestMatcher Migration Path for CAS
Issue gh-16417
1 parent 15d9c13 commit 91ee5e7

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

cas/src/main/java/org/springframework/security/cas/web/CasAuthenticationFilter.java

+15
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
5252
import org.springframework.security.web.savedrequest.RequestCache;
5353
import org.springframework.security.web.savedrequest.SavedRequest;
54+
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
5455
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
5556
import org.springframework.security.web.util.matcher.RequestMatcher;
5657
import org.springframework.util.Assert;
@@ -215,6 +216,8 @@ public class CasAuthenticationFilter extends AbstractAuthenticationProcessingFil
215216

216217
public CasAuthenticationFilter() {
217218
super("/login/cas");
219+
RequestMatcher processUri = PathPatternRequestMatcher.withDefaults().matcher("/login/cas");
220+
setRequiresAuthenticationRequestMatcher(processUri);
218221
setAuthenticationFailureHandler(new SimpleUrlAuthenticationFailureHandler());
219222
setSecurityContextRepository(this.securityContextRepository);
220223
}
@@ -319,6 +322,18 @@ public final void setAuthenticationFailureHandler(AuthenticationFailureHandler f
319322
super.setAuthenticationFailureHandler(new CasAuthenticationFailureHandler(failureHandler));
320323
}
321324

325+
/**
326+
* Use this {@code RequestMatcher} to match proxy receptor requests. Without setting
327+
* this matcher, {@link CasAuthenticationFilter} will not capture any proxy receptor
328+
* requets.
329+
* @param proxyReceptorMatcher the {@link RequestMatcher} to use
330+
* @since 6.5
331+
*/
332+
public final void setProxyReceptorMatcher(RequestMatcher proxyReceptorMatcher) {
333+
Assert.notNull(proxyReceptorMatcher, "proxyReceptorMatcher cannot be null");
334+
this.proxyReceptorMatcher = proxyReceptorMatcher;
335+
}
336+
322337
public final void setProxyReceptorUrl(final String proxyReceptorUrl) {
323338
this.proxyReceptorMatcher = new AntPathRequestMatcher("/**" + proxyReceptorUrl);
324339
}

cas/src/test/java/org/springframework/security/cas/web/CasAuthenticationFilterTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
4444
import org.springframework.security.web.context.SecurityContextRepository;
4545
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
46+
import org.springframework.security.web.servlet.util.matcher.PathPatternRequestMatcher;
4647
import org.springframework.test.util.ReflectionTestUtils;
4748

4849
import static org.assertj.core.api.Assertions.assertThat;
@@ -267,4 +268,20 @@ void successfulAuthenticationWhenSecurityContextHolderStrategySetThenUses() thro
267268
verify(securityContextRepository).setContext(any(SecurityContext.class));
268269
}
269270

271+
@Test
272+
public void requiresAuthenticationWhenProxyRequestMatcherThenMatches() {
273+
CasAuthenticationFilter filter = new CasAuthenticationFilter();
274+
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/pgtCallback");
275+
MockHttpServletResponse response = new MockHttpServletResponse();
276+
request.setServletPath("/pgtCallback");
277+
assertThat(filter.requiresAuthentication(request, response)).isFalse();
278+
filter.setProxyReceptorMatcher(PathPatternRequestMatcher.withDefaults().matcher(request.getServletPath()));
279+
assertThat(filter.requiresAuthentication(request, response)).isFalse();
280+
filter.setProxyGrantingTicketStorage(mock(ProxyGrantingTicketStorage.class));
281+
assertThat(filter.requiresAuthentication(request, response)).isTrue();
282+
request.setRequestURI("/other");
283+
request.setServletPath("/other");
284+
assertThat(filter.requiresAuthentication(request, response)).isFalse();
285+
}
286+
270287
}

docs/modules/ROOT/pages/migration/web.adoc

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

97+
=== Migrate CAS Proxy Receptor Request Matcher
98+
99+
Spring Security 6 converts any configured `proxyReceptorUrl` to a request matcher that matches the end of the request, that is `/**/proxy/receptor`.
100+
In Spring Security 7, this pattern is not allowed and will change to using `PathPatternRequestMatcher`.
101+
Also in Spring Security 7m the URL should by absolute, excluding any context path, like so: `/proxy/receptor`.
102+
103+
So to prepare for these change, you can use `setProxyReceptorRequestMatcher` instead of `setProxyReceptorUrl`.
104+
105+
That is, change this:
106+
[tabs]
107+
======
108+
Java::
109+
+
110+
[source,java,role="primary"]
111+
----
112+
casAuthentication.setProxyReceptorUrl("/proxy/receptor");
113+
----
114+
115+
Kotlin::
116+
+
117+
[source,kotlin,role="secondary"]
118+
----
119+
casAuthentication.setProxyReceptorUrl("/proxy/receptor")
120+
----
121+
======
122+
123+
to this:
124+
125+
[tabs]
126+
======
127+
Java::
128+
+
129+
[source,java,role="primary"]
130+
----
131+
casAuthentication.setProxyReceptorUrl(PathPatternRequestMatcher.withDefaults().matcher("/proxy/receptor"));
132+
----
133+
134+
Kotlin::
135+
+
136+
[source,kotlin,role="secondary"]
137+
----
138+
casAuthentication.setProxyReceptorUrl(PathPatternRequestMatcher.withDefaults().matcher("/proxy/receptor"))
139+
----
140+
======
141+
97142
== Include the Servlet Path Prefix in Authorization Rules
98143

99144
For many applications <<use-path-pattern, the above>> will make no difference since most commonly all URIs listed are matched by the default servlet.

0 commit comments

Comments
 (0)