讲解基于CAS Client 2.1.1
CAS Client配置
在web.xml中添加如下的代码:
<filter> <filter-name>CASFilter</filter-name> <filter-class>com.goldarmor.live800.cas.Live800CASFilter</filter-class> <init-param> <param-name>edu.yale.its.tp.cas.client.filter.loginUrl</param-name> <param-value>http://jiangboli:8080/cas/login</param-value> </init-param> <init-param> <param-name>edu.yale.its.tp.cas.client.filter.validateUrl</param-name> <param-value>http://jiangboli:8080/cas/serviceValidate</param-value> </init-param> <init-param> <param-name>edu.yale.its.tp.cas.client.filter.serverName</param-name> <param-value>jiangboli:8080</param-value> </init-param> </filter> <filter-mapping> <filter-name>CASFilter</filter-name> <url-pattern>/hello</url-patter </filter-mapping>
大家可能也看到了,<filter-class>com.goldarmor.live800.cas.Live800CASFilter</filter-class>指定的类并不是edu.yale.its.tp.cas.client.filter.CASFilter。没错,我们可以指定自己的filter来完成自己想要到功能。
指定的参数说明:(必须)
edu.yale.its.tp.cas.client.filter.loginUrl | 指定 CAS 提供登录页面的 URL |
edu.yale.its.tp.cas.client.filter.validateUrl | 指定 CAS 提供 service ticket 或 proxy ticket 验证服务的 URL |
edu.yale.its.tp.cas.client.filter.serverName | 指定客户端的域名和端口,是指客户端应用所在机器而不是 CAS Server 所在机器,该参数或 serviceUrl 至少有一个必须指定 |
edu.yale.its.tp.cas.client.filter.serviceUrl | 该参数指定过后将覆盖 serverName 参数,成为登录成功过后重定向的目的地址 |
可选参数:
edu.yale.its.tp.cas.client.filter.proxyCallbackUrl | 用于当前应用需要作为其他服务的代理(proxy)时获取 Proxy Granting Ticket 的地址 |
edu.yale.its.tp.cas.client.filter.authorizedProxy | 用于允许当前应用从代理处获取 proxy tickets,该参数接受以空格分隔开的多个 proxy URLs,但实际使用只需要一个成功即可。当指定该参数过后,需要修改 validateUrl 到 proxyValidate,而不再是 serviceValidate |
edu.yale.its.tp.cas.client.filter.renew | 如果指定为 true,那么受保护的资源每次被访问时均要求用户重新进行验证,而不管之前是否已经通过 |
edu.yale.its.tp.cas.client.filter.wrapRequest | 如果指定为 true,那么 CASFilter 将重新包装 HttpRequest,并且使 getRemoteUser() 方法返回当前登录用户的用户名 |
edu.yale.its.tp.cas.client.filter.gateway | 指定 gateway 属性 |
CAS支持http
CAS默认支持的是https,不过通过修改源代码可以实现http的请求方式:
edu.yale.its.tp.cas.client.filter.CASFilter中的init(FilterConfig config)以下代码注销掉
if (! casValidate.startsWith("https://")){
throw new ServletException("validateUrl must start with https://, its current value is [" + casValidate + "]");
}
edu.yale.its.tp.cas.util.SecureURL的retrieve(String url)中以下代码注销掉
if (!u.getProtocol().equals("https")){
// IOException may not be the best exception we could throw here
// since the problem is with the URL argument we were passed, not
// IO. -awp9
log.error("retrieve(" + url + ") on an illegal URL since protocol was not https.");
throw new IOException("only 'https' URLs are valid for this method");
}
CAS Server 端的修改请查看单点登录CAS Server 介绍