# Java Apereo CAS Client [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.jasig.cas.client/cas-client-core/badge.svg?style=flat)](https://maven-badges.herokuapp.com/maven-central/org.jasig.cas/cas-server) ## Intro This is the official home of the Java Apereo CAS client. The client consists of a collection of Servlet filters that are suitable for most Java-based web applications. It also serves as an API platform to interact with the CAS server programmatically to make authentication requests, validate tickets and consume principal attributes. All client artifacts are published to Maven central. At a minimum, a given application will need to configure the following dependency: ```xml org.jasig.cas.client cas-client-core ${java.cas.client.version} ``` ## Configurtion ### Client Configuration Using `web.xml` The client can be configured via `web.xml` via a series of `context-param`s and filter `init-param`s. Each filter for the client has a required (and optional) set of properties. The filters are designed to look for these properties in the following way: - Check the filter's local `init-param`s for a parameter matching the required property name. - Check the `context-param`s for a parameter matching the required property name. - If two properties are found with the same name in the `init-param`s and the `context-param`s, the `init-param` takes precedence. **Note**: If you're using the `serverName` property, you should note well that the fragment-URI (the stuff after the #) is not sent to the server by all browsers, thus the CAS client can't capture it as part of the URL. An example application that is protected by the client is [available here](https://github.com/UniconLabs/cas-sample-java-webapp). #### org.jasig.cas.client.authentication.AuthenticationFilter The `AuthenticationFilter` is what detects whether a user needs to be authenticated or not. If a user needs to be authenticated, it will redirect the user to the CAS server. ```xml CAS Authentication Filter org.jasig.cas.client.authentication.AuthenticationFilter casServerLoginUrl https://battags.ad.ess.rutgers.edu:8443/cas/login serverName http://www.acme-client.com ``` | Property | Description | Required |----------|-------|----------- | `casServerLoginUrl` | Defines the location of the CAS server login URL, i.e. `https://localhost:8443/cas/login` | Yes | `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 | `service` | The service URL to send to the CAS server, i.e. `https://localhost:8443/yourwebapp/index.html` | No | `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 | `gateway ` | specifies whether `gateway=true` should be sent to the CAS server. Valid values are either `true/false` (or no value at all) | No | `artifactParameterName ` | specifies the name of the request parameter on where to find the artifact (i.e. `ticket`). | No | `serviceParameterName ` | specifies the name of the request parameter on where to find the service (i.e. `service`) | No #### org.jasig.cas.client.authentication.Saml11AuthenticationFilter The SAML 1.1 `AuthenticationFilter` is what detects whether a user needs to be authenticated or not. If a user needs to be authenticated, it will redirect the user to the CAS server. ```xml CAS Authentication Filter org.jasig.cas.client.authentication.Saml11AuthenticationFilter casServerLoginUrl https://somewhere.cas.edu:8443/cas/login serverName http://www.the-client.com ``` | Property | Description | Required |----------|-------|----------- | `casServerLoginUrl` | Defines the location of the CAS server login URL, i.e. `https://localhost:8443/cas/login` | Yes | `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 | `service` | The service URL to send to the CAS server, i.e. `https://localhost:8443/yourwebapp/index.html` | No | `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 | `gateway ` | specifies whether `gateway=true` should be sent to the CAS server. Valid values are either `true/false` (or no value at all) | No | `artifactParameterName ` | specifies the name of the request parameter on where to find the artifact (i.e. `SAMLart`). | No | `serviceParameterName ` | specifies the name of the request parameter on where to find the service (i.e. `TARGET`) | No ####org.jasig.cas.client.validation.Cas10TicketValidationFilter Validates tickets using the CAS 1.0 Protocol. ```xml CAS Validation Filter org.jasig.cas.client.validation.Cas10TicketValidationFilter casServerUrlPrefix https://somewhere.cas.edu:8443/cas ``` | Property | Description | Required |----------|-------|----------- | `casServerUrlPrefix ` | The start of the CAS server URL, i.e. `https://localhost:8443/cas` | Yes | `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 | `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 | `redirectAfterValidation ` | Whether to redirect to the same URL after ticket validation, but without the ticket in the parameter. Defaults to `true`. | No | `useSession ` | Whether to store the Assertion in session or not. If sessions are not used, tickets will be required for each request. Defaults to `true`. | No | `exceptionOnValidationFailure ` | Whether to throw an exception or not on ticket validation failure. Defaults to `true`. | No #### org.jasig.cas.client.validation.Saml11TicketValidationFilter Validates tickets using the SAML 1.1 protocol. ```xml CAS Validation Filter org.jasig.cas.client.validation.Saml11TicketValidationFilter casServerUrlPrefix https://battags.ad.ess.rutgers.edu:8443/cas serverName http://www.acme-client.com ``` | Property | Description | Required |----------|-------|----------- | `casServerUrlPrefix ` | The start of the CAS server URL, i.e. `https://localhost:8443/cas` | Yes | `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 | `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 | `redirectAfterValidation ` | Whether to redirect to the same URL after ticket validation, but without the ticket in the parameter. Defaults to `true`. | No | `useSession ` | Whether to store the Assertion in session or not. If sessions are not used, tickets will be required for each request. Defaults to `true`. | No | `exceptionOnValidationFailure ` | whether to throw an exception or not on ticket validation failure. Defaults to `true` | No | `tolerance ` | The tolerance for drifting clocks when validating SAML tickets. Note that 10 seconds should be more than enough for most environments that have NTP time synchronization. Defaults to `1000 msec` | No #### org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter Validates the tickets using the CAS 2.0 protocol. If you provide either the `acceptAnyProxy` or the `allowedProxyChains` parameters, a `Cas20ProxyTicketValidator` will be constructed. Otherwise a general `Cas20ServiceTicketValidator` will be constructed that does not accept proxy tickets. **Note**: If you are using proxy validation, you should place the `filter-mapping` of the validation filter before the authentication filter. ```xml CAS Validation Filter org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter casServerUrlPrefix https://battags.ad.ess.rutgers.edu:8443/cas serverName http://www.acme-client.com ``` | Property | Description | Required |----------|-------|----------- | `casServerUrlPrefix ` | The start of the CAS server URL, i.e. `https://localhost:8443/cas` | Yes | `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 | `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 | `redirectAfterValidation ` | Whether to redirect to the same URL after ticket validation, but without the ticket in the parameter. Defaults to `true`. | No | `useSession ` | Whether to store the Assertion in session or not. If sessions are not used, tickets will be required for each request. Defaults to `true`. | No | `exceptionOnValidationFailure ` | whether to throw an exception or not on ticket validation failure. Defaults to `true` | No | `proxyReceptorUrl ` | The URL to watch for `PGTIOU/PGT` responses from the CAS server. Should be defined from the root of the context. For example, if your application is deployed in `/cas-client-app` and you want the proxy receptor URL to be `/cas-client-app/my/receptor` you need to configure proxyReceptorUrl to be `/my/receptor`. | No | `acceptAnyProxy ` | Specifies whether any proxy is OK. Defaults to `false`. | No | `allowedProxyChains ` | Specifies the proxy chain. Each acceptable proxy chain should include a space-separated list of URLs. Each acceptable proxy chain should appear on its own line. | No | `proxyCallbackUrl` | The callback URL to provide the CAS server to accept Proxy Granting Tickets. | No | `proxyGrantingTicketStorageClass ` | Specify an implementation of the ProxyGrantingTicketStorage class that has a no-arg constructor. | No ##### Proxy Authentication vs. Distributed Caching The client has support for clustering and distributing the TGT state among application nodes that are behind a load balancer. In order to do so, the parameter needs to be defined as such for the filter. ###### Ehcache Include the following dependency: ```xml org.jasig.cas cas-client-support-distributed-ehcache ${java.cas.client.version} ``` Configure the client: ```xml proxyGrantingTicketStorageClass org.jasig.cas.client.proxy.EhcacheBackedProxyGrantingTicketStorageImpl ``` The setting provides an implementation for proxy storage using EhCache to take advantage of its replication features so that the PGT is successfully replicated and shared among nodes, regardless which node is selected as the result of the load balancer rerouting. Configuration of this parameter is not enough. The EhCache configuration needs to enable the replication mechanism through once of its suggested ways. A sample of that configuration based on RMI replication can be found here. Please note that while the sample is done for a distributed ticket registry implementation, the basic idea and configuration should easily be transferable. When loading from the `web.xml`, the Jasig CAS Client relies on a series of default values, one of which being that the cache must be configured in the default location (i.e. `classpath:ehcache.xml`). ```xml ``` ###### Memcached A similar implementation based on Memcached is also available: Include the following dependency: ```xml org.jasig.cas cas-client-support-distributed-memcached ${java.cas.client.version} ``` Configure the client: ```xml proxyGrantingTicketStorageClass org.jasig.cas.client.proxy. MemcachedBackedProxyGrantingTicketStorageImpl ``` When loading from the `web.xml`, the Client relies on a series of default values, one of which being that the list of memcached servers must be defined in `/cas/casclient_memcached_hosts.txt` on the classpath). The file is a simple list of `:` on separate lines. **BE SURE NOT TO HAVE EXTRA LINE BREAKS**. #### org.jasig.cas.client.util.HttpServletRequestWrapperFilter Wraps an `HttpServletRequest` so that the `getRemoteUser` and `getPrincipal` return the CAS related entries. ```xml CAS HttpServletRequest Wrapper Filter org.jasig.cas.client.util.HttpServletRequestWrapperFilter ``` #### org.jasig.cas.client.util.AssertionThreadLocalFilter Places the `Assertion` in a `ThreadLocal` for portions of the application that need access to it. This is useful when the Web application that this filter "fronts" needs to get the Principal name, but it has no access to the `HttpServletRequest`, hence making `getRemoteUser()` call impossible. ```xml CAS Assertion Thread Local Filter org.jasig.cas.client.util.AssertionThreadLocalFilter ``` ### Client Configuration Using Spring Configuration via Spring IoC will depend heavily on `DelegatingFilterProxy` class. For each filter that will be configured for CAS via Spring, a corresponding `DelegatingFilterProxy` is needed in the web.xml. As the `SingleSignOutFilter`, `HttpServletRequestWrapperFilter` and `AssertionThreadLocalFilter` have no configuration options, we recommend you just configure them in the `web.xml` ```xml CAS Authentication Filter org.springframework.web.filter.DelegatingFilterProxy targetBeanName authenticationFilter CAS Authentication Filter /* ``` #### Bean Configuration ##### AuthenticationFilter ```xml ``` ##### Cas10TicketValidationFilter ```xml ``` ##### Saml11TicketValidationFilter ```xml ``` ##### Cas20ProxyReceivingTicketValidationFilter Configuration to validate tickets: ```xml ``` Configuration to accept a Proxy Granting Ticket: ```xml ``` Configuration to accept any Proxy Ticket (and Proxy Granting Tickets): ```xml ``` Configuration to accept Proxy Ticket from a chain (and Proxy Granting Tickets): ```xml http://proxy1 http://proxy2 ``` The specific filters can be configured in the following ways. Please see the JavaDocs included in the distribution for specific required and optional properties: ### Client Configuration Using JNDI Configuring the CAS client via JNDI is essentially the same as configuring the client via the `web.xml`, except the properties will reside in JNDI and not in the `web.xml`. All properties that are placed in JNDI should be placed under `java:comp/env/cas` We use the following conventions: 1. JNDI will first look in `java:comp/env/cas/{SHORT FILTER NAME}/{PROPERTY NAME}` (i.e. `java:comp/env/cas/AuthenticationFilter/serverName`) 2. JNDI will as a last resort look in `java:comp/env/cas/{PROPERTY NAME}` (i.e. `java:comp/env/cas/serverName`) #### Example This is an update to the `META-INF/context.xml` that is included in Tomcat's Manager application: ```xml ``` ### Configuring Single Sign Out The Single Sign Out support in CAS consists of configuring one `SingleSignOutFilter` and one `ContextListener`. Please note that if you have configured the CAS Client for Java as Web filters, this filter must come before the other filters as described. The `SingleSignOutFilter` can affect character encoding. This becomes most obvious when used in conjunction with Confluence. Its recommended you explicitly configure either the [VT Character Encoding Filter](http://code.google.com/p/vt-middleware/wiki/vtservletfilters#CharacterEncodingFilter) or the [Spring Character Encoding Filter](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/filter/CharacterEncodingFilter.html) with explicit encodings. #### CAS Protocol ```xml CAS Single Sign Out Filter org.jasig.cas.client.session.SingleSignOutFilter ... CAS Single Sign Out Filter /* ... org.jasig.cas.client.session.SingleSignOutHttpSessionListener ``` #### SAML Protocol ```xml filter> CAS Single Sign Out Filter org.jasig.cas.client.session.SingleSignOutFilter artifactParameterName SAMLart ... CAS Single Sign Out Filter /* ... org.jasig.cas.client.session.SingleSignOutHttpSessionListener ``` #### Recommend Logout Procedure The client has no code to help you handle log out. The client merely places objects in session. Therefore, we recommend you do a `session.invalidate()` call when you log a user out. However, that's entirely your application's responsibility. We recommend that text similar to the following appear when the application's session is ended. ```html You have been logged out of [APPLICATION NAME GOES HERE]. To log out of all applications, click here. (provide link to CAS server's logout) ``` ## JBoss Integration ## Tomcat 6 Integration ## Tomcat 7 Integration ## Confluence Integration ## Build ```bash git clone git@github.com:Jasig/java-cas-client.git cd java-cas-client mvn clean package ``` Please note that to be deployed in Maven Central, we mark a number of JARs as provided (related to JBoss and Memcache Clients). In order to build the clients, you must enable the commented out repositories in the appropriate `pom.xml` files in the modules (cas-client-integration-jboss and cas-client-support-distributed-memcached) or follow the instructions on how to install the file manually.