Skip to content

Commit f471206

Browse files
Allow customization of redirect strategy in CasAuthenticationEntrypoint
Closes gh-14881
1 parent 1fbfaa1 commit f471206

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -28,6 +28,7 @@
2828
import org.springframework.security.core.AuthenticationException;
2929
import org.springframework.security.web.AuthenticationEntryPoint;
3030
import org.springframework.security.web.DefaultRedirectStrategy;
31+
import org.springframework.security.web.RedirectStrategy;
3132
import org.springframework.util.Assert;
3233

3334
/**
@@ -61,6 +62,8 @@ public class CasAuthenticationEntryPoint implements AuthenticationEntryPoint, In
6162
*/
6263
private boolean encodeServiceUrlWithSessionId = true;
6364

65+
private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
66+
6467
@Override
6568
public void afterPropertiesSet() {
6669
Assert.hasLength(this.loginUrl, "loginUrl must be specified");
@@ -74,8 +77,7 @@ public final void commence(final HttpServletRequest servletRequest, HttpServletR
7477
String urlEncodedService = createServiceUrl(servletRequest, response);
7578
String redirectUrl = createRedirectUrl(urlEncodedService);
7679
preCommence(servletRequest, response);
77-
new DefaultRedirectStrategy().sendRedirect(servletRequest, response, redirectUrl);
78-
// response.sendRedirect(redirectUrl);
80+
this.redirectStrategy.sendRedirect(servletRequest, response, redirectUrl);
7981
}
8082

8183
/**
@@ -149,4 +151,14 @@ protected boolean getEncodeServiceUrlWithSessionId() {
149151
return this.encodeServiceUrlWithSessionId;
150152
}
151153

154+
/**
155+
* Sets the {@link RedirectStrategy} to use
156+
* @param redirectStrategy the {@link RedirectStrategy} to use
157+
* @since 6.3
158+
*/
159+
public void setRedirectStrategy(RedirectStrategy redirectStrategy) {
160+
Assert.notNull(redirectStrategy, "redirectStrategy cannot be null");
161+
this.redirectStrategy = redirectStrategy;
162+
}
163+
152164
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@
1616

1717
package org.springframework.security.cas.web;
1818

19+
import java.io.IOException;
1920
import java.net.URLEncoder;
2021

2122
import org.junit.jupiter.api.Test;
2223

2324
import org.springframework.mock.web.MockHttpServletRequest;
2425
import org.springframework.mock.web.MockHttpServletResponse;
26+
import org.springframework.security.authentication.BadCredentialsException;
2527
import org.springframework.security.cas.ServiceProperties;
28+
import org.springframework.security.web.RedirectStrategy;
2629

2730
import static org.assertj.core.api.Assertions.assertThat;
2831
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
32+
import static org.mockito.ArgumentMatchers.eq;
33+
import static org.mockito.Mockito.mock;
34+
import static org.mockito.Mockito.verify;
2935

3036
/**
3137
* Tests {@link CasAuthenticationEntryPoint}.
@@ -95,4 +101,25 @@ public void testNormalOperationWithRenewTrue() throws Exception {
95101
.isEqualTo(response.getRedirectedUrl());
96102
}
97103

104+
@Test
105+
void setRedirectStrategyThenUses() throws IOException {
106+
CasAuthenticationEntryPoint ep = new CasAuthenticationEntryPoint();
107+
ServiceProperties sp = new ServiceProperties();
108+
109+
sp.setService("https://mycompany.com/login/cas");
110+
ep.setServiceProperties(sp);
111+
ep.setLoginUrl("https://cas/login");
112+
113+
RedirectStrategy redirectStrategy = mock();
114+
115+
ep.setRedirectStrategy(redirectStrategy);
116+
MockHttpServletRequest req = new MockHttpServletRequest();
117+
MockHttpServletResponse res = new MockHttpServletResponse();
118+
119+
ep.commence(req, res, new BadCredentialsException("bad credentials"));
120+
121+
verify(redirectStrategy).sendRedirect(eq(req), eq(res),
122+
eq("https://cas/login?service=https%3A%2F%2Fmycompany.com%2Flogin%2Fcas"));
123+
}
124+
98125
}

0 commit comments

Comments
 (0)