Skip to content

Commit 296b1fc

Browse files
committed
Support casServerUrlPrefix config option
`casServerUrlPrefix` is used for validation and single logout, but before this commit it couldn't be used by the auth filter. As a result, web.xml needed to (for the typical usecase) contain at least two references to the cas server. Now, only one context init-param reference is necessary, as long as the login page is hosted at {prefix}/login (which seems to be mandated by cas protocol spec).
1 parent 1e9b356 commit 296b1fc

5 files changed

Lines changed: 57 additions & 13 deletions

File tree

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ The `AuthenticationFilter` is what detects whether a user needs to be authentica
176176
<filter-name>CAS Authentication Filter</filter-name>
177177
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
178178
<init-param>
179-
<param-name>casServerLoginUrl</param-name>
180-
<param-value>https://battags.ad.ess.rutgers.edu:8443/cas/login</param-value>
179+
<param-name>casServerUrlPrefix</param-name>
180+
<param-value>https://battags.ad.ess.rutgers.edu:8443/cas</param-value>
181181
</init-param>
182182
<init-param>
183183
<param-name>serverName</param-name>
@@ -192,7 +192,8 @@ The `AuthenticationFilter` is what detects whether a user needs to be authentica
192192

193193
| Property | Description | Required
194194
|----------|-------|-----------
195-
| `casServerLoginUrl` | Defines the location of the CAS server login URL, i.e. `https://localhost:8443/cas/login` | Yes
195+
| `casServerUrlPrefix` | The start of the CAS server URL, i.e. `https://localhost:8443/cas` | Yes (unless `casServerLoginUrl` is set)
196+
| `casServerLoginUrl` | Defines the location of the CAS server login URL, i.e. `https://localhost:8443/cas/login`. This overrides `casServerUrlPrefix`, if set. | Yes (unless `casServerUrlPrefix` is set)
196197
| `serverName` | The name of the server this application is hosted on. Service URL will be dynamically constructed using this, i.e. https://localhost:8443 (you must include the protocol, but port is optional if it's a standard port). | Yes
197198
| `service` | The service URL to send to the CAS server, i.e. `https://localhost:8443/yourwebapp/index.html` | No
198199
| `renew` | specifies whether `renew=true` should be sent to the CAS server. Valid values are either `true/false` (or no value at all). Note that `renew` cannot be specified as local `init-param` setting. | No
@@ -230,7 +231,8 @@ The SAML 1.1 `AuthenticationFilter` is what detects whether a user needs to be a
230231

231232
| Property | Description | Required
232233
|----------|-------|-----------
233-
| `casServerLoginUrl` | Defines the location of the CAS server login URL, i.e. `https://localhost:8443/cas/login` | Yes
234+
| `casServerUrlPrefix` | The start of the CAS server URL, i.e. `https://localhost:8443/cas` | Yes (unless `casServerLoginUrl` is set)
235+
| `casServerLoginUrl` | Defines the location of the CAS server login URL, i.e. `https://localhost:8443/cas/login`. This overrides `casServerUrlPrefix`, if set. | Yes (unless `casServerUrlPrefix` is set)
234236
| `serverName` | The name of the server this application is hosted on. Service URL will be dynamically constructed using this, i.e. https://localhost:8443 (you must include the protocol, but port is optional if it's a standard port). | Yes
235237
| `service` | The service URL to send to the CAS server, i.e. `https://localhost:8443/yourwebapp/index.html` | No
236238
| `renew` | specifies whether `renew=true` should be sent to the CAS server. Valid values are either `true/false` (or no value at all). Note that `renew` cannot be specified as local `init-param` setting. | No

cas-client-core/src/main/java/org/jasig/cas/client/authentication/AuthenticationFilter.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,14 @@ protected AuthenticationFilter(final Protocol protocol) {
9393
protected void initInternal(final FilterConfig filterConfig) throws ServletException {
9494
if (!isIgnoreInitConfiguration()) {
9595
super.initInternal(filterConfig);
96-
setCasServerLoginUrl(getString(ConfigurationKeys.CAS_SERVER_LOGIN_URL));
96+
97+
String loginUrl = getString(ConfigurationKeys.CAS_SERVER_LOGIN_URL);
98+
if (loginUrl != null) {
99+
setCasServerLoginUrl(loginUrl);
100+
} else {
101+
setCasServerUrlPrefix(getString(ConfigurationKeys.CAS_SERVER_URL_PREFIX));
102+
}
103+
97104
setRenew(getBoolean(ConfigurationKeys.RENEW));
98105
setGateway(getBoolean(ConfigurationKeys.GATEWAY));
99106

@@ -133,7 +140,13 @@ protected void initInternal(final FilterConfig filterConfig) throws ServletExcep
133140

134141
public void init() {
135142
super.init();
136-
CommonUtils.assertNotNull(this.casServerLoginUrl, "casServerLoginUrl cannot be null.");
143+
144+
String message = String.format(
145+
"one of %s and %s must not be null.",
146+
ConfigurationKeys.CAS_SERVER_LOGIN_URL.getName(),
147+
ConfigurationKeys.CAS_SERVER_URL_PREFIX.getName());
148+
149+
CommonUtils.assertNotNull(this.casServerLoginUrl, message);
137150
}
138151

139152
public final void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
@@ -192,6 +205,10 @@ public final void setGateway(final boolean gateway) {
192205
this.gateway = gateway;
193206
}
194207

208+
public final void setCasServerUrlPrefix(final String casServerUrlPrefix) {
209+
setCasServerLoginUrl(CommonUtils.addTrailingSlash(casServerUrlPrefix) + "login");
210+
}
211+
195212
public final void setCasServerLoginUrl(final String casServerLoginUrl) {
196213
this.casServerLoginUrl = casServerLoginUrl;
197214
}

cas-client-core/src/main/java/org/jasig/cas/client/util/CommonUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,4 +719,13 @@ public static int toInt(final String str, final int defaultValue) {
719719
}
720720
}
721721

722+
/**
723+
* Adds a trailing slash to the given uri, if it doesn't already have one.
724+
*
725+
* @param uri a string that may or may not end with a slash
726+
* @return the same string, except with a slash suffix (if necessary).
727+
*/
728+
public static String addTrailingSlash(String uri) {
729+
return uri.endsWith("/") ? uri : uri + "/";
730+
}
722731
}

cas-client-core/src/main/java/org/jasig/cas/client/validation/AbstractUrlBasedTicketValidator.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public abstract class AbstractUrlBasedTicketValidator implements TicketValidator
7171
* @param casServerUrlPrefix the location of the CAS server.
7272
*/
7373
protected AbstractUrlBasedTicketValidator(final String casServerUrlPrefix) {
74-
this.casServerUrlPrefix = casServerUrlPrefix;
75-
CommonUtils.assertNotNull(this.casServerUrlPrefix, "casServerUrlPrefix cannot be null.");
74+
CommonUtils.assertNotNull(casServerUrlPrefix, "casServerUrlPrefix cannot be null.");
75+
this.casServerUrlPrefix = CommonUtils.addTrailingSlash(casServerUrlPrefix);
7676
}
7777

7878
/**
@@ -124,9 +124,6 @@ protected final String constructValidationUrl(final String ticket, final String
124124
int i = 0;
125125

126126
buffer.append(this.casServerUrlPrefix);
127-
if (!this.casServerUrlPrefix.endsWith("/")) {
128-
buffer.append("/");
129-
}
130127
buffer.append(suffix);
131128

132129
for (Map.Entry<String, String> entry : urlParameters.entrySet()) {

cas-client-core/src/test/java/org/jasig/cas/client/authentication/AuthenticationFilterTests.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public final class AuthenticationFilterTests {
4747

4848
private static final String CAS_SERVICE_URL = "https://localhost:8443/service";
4949

50-
private static final String CAS_LOGIN_URL = "https://localhost:8443/cas/login";
50+
private static final String CAS_PREFIX = "https://localhost:8443/cas";
51+
private static final String CAS_LOGIN_URL = CAS_PREFIX + "/login";
5152

5253
private AuthenticationFilter filter;
5354

@@ -66,7 +67,25 @@ public void tearDown() throws Exception {
6667
}
6768

6869
@Test
69-
public void testRedirect() throws Exception {
70+
public void testRedirectWithLoginUrlConfig() throws Exception {
71+
doRedirectTest();
72+
}
73+
74+
@Test
75+
public void testRedirectWithCasServerPrefixConfig() throws Exception {
76+
replaceFilterWithPrefixConfiguredFilter();
77+
doRedirectTest();
78+
}
79+
80+
private void replaceFilterWithPrefixConfiguredFilter() throws ServletException {
81+
this.filter = new AuthenticationFilter();
82+
final MockFilterConfig config = new MockFilterConfig();
83+
config.addInitParameter("casServerUrlPrefix", CAS_PREFIX);
84+
config.addInitParameter("service", CAS_SERVICE_URL);
85+
this.filter.init(config);
86+
}
87+
88+
private void doRedirectTest() throws IOException, ServletException {
7089
final MockHttpSession session = new MockHttpSession();
7190
final MockHttpServletRequest request = new MockHttpServletRequest();
7291
final MockHttpServletResponse response = new MockHttpServletResponse();

0 commit comments

Comments
 (0)