CAS单点登出后,默认会跳到它自带的注销界面(这里建立在已部署好CAS—Server的基础上,详情见上篇文章),如下图:
对应的jsp如下目录:
1、修改cas-servlet.xml配置
打开【apache-tomcat-6.0.33\webapps\cas\WEB-INF】目录下的cas-servlet.xml
修改cas-servlet.xml文件的bean的id为logoutController下的p:followServiceRedirects属性为“true”,如下图:
2、修改注销时的链接
在自己系统要配置的系统“退出”链接后加上“?service=退出返回后的地址”,例如:CAS测试用的两个客户端的配置。例如:<ahref="http://localhost:8080/cas/logout?service=http://www.baidu.com">退出</a>
如下图:
3、原理
3.1 先看cas\WEB-INF目录下的web.xml
由上图可知,所有/logout的请求都交给SafeDispatcherServlet去分发了,查看代码可以知道这个Servlet只是对org.springframework.web.servlet.DispatcherServlet一次包装,将所有请求都交给org.springframework.web.servlet.DispatcherServlet去处理了。
3.2 然后看cas\WEB-INF目录下的cas-servlet.xml
从上图handlerMappingC的bean里面有一段配置:/logout—logoutController。可知所有/logout的请求,都交给一个beanid为logoutController的Bean去处理了。
那么我们看看org.jasig.cas.web.LogoutController到底做了什么事情,我们第一步中修改的配置正是这个Controller的配置:
下面看它的核心源码你就明白了:
<pre name="code" class="java">protected ModelAndView handleRequestInternal(
final HttpServletRequest request, final HttpServletResponse response)
throws Exception {
//取得TGT_ID
final String ticketGrantingTicketId = this.ticketGrantingTicketCookieGenerator.retrieveCookieValue(request);
// 取得service参数数据,这个参数是可选参数
final String service = request.getParameter("service");
//如果TGT不为空
if (ticketGrantingTicketId != null) {
//那么在centralAuthenticationService中销毁
this.centralAuthenticationService
.destroyTicketGrantingTicket(ticketGrantingTicketId);
//ticketGrantingTicketCookieGenerator 中销毁cookie
this.ticketGrantingTicketCookieGenerator.removeCookie(response);
//warnCookieGenerator 中销毁
this.warnCookieGenerator.removeCookie(response);
}
// 如果参数:followServiceRedirects为true 同时service不会空的时候,跳转到service指定的URL
if (this.followServiceRedirects && service != null) {
return new ModelAndView(new RedirectView(service));
}
//否则,跳转到logoutView指定的页面
return new ModelAndView(this.logoutView);
}
相信,看到下面这句话,你就明白为什么配置第一步和第二步了
4、总结
/logout: ( 对应实现类 org.jasig.cas.web.LogoutController ),注销的处理逻辑如下:
(1) removeCookie
(2) 在服务端删除TicketGrantingTicket 对象(此对象封装了cookie 的value 值)
(3) redirect 到退出页面,有2 种选择:
l 如果LogoutController 的followServiceRedirects 属性为true 值,且url 里的service 参数非空,则redirect 到 sevice 参数标识的url;
l 否则, redirect 到内置的casLogoutView ,如果url 里有url 参数,则此url 参数标识的链接会显示在casLogoutView 页面上。
耐心的看看这些开源框架的源码,你很容易就明白了为什么这么做;当然如果熟读源码的话,你就可以按着自己的想法随意的修改框架的各个部分。