CAS—注销登录后跳转到登录页

来源:互联网 发布:广东奥飞数据董事长 编辑:程序博客网 时间:2024/05/16 06:44

 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的配置:


  


         下面看它的核心源码你就明白了:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <pre name="code" class="java">protected ModelAndView handleRequestInternal(    
  2.      final HttpServletRequest request, final HttpServletResponse response)    
  3.      throws Exception {    
  4.     //取得TGT_ID    
  5.      final String ticketGrantingTicketId = this.ticketGrantingTicketCookieGenerator.retrieveCookieValue(request);    
  6.      // 取得service参数数据,这个参数是可选参数    
  7.      final String service = request.getParameter("service");    
  8.          
  9.      //如果TGT不为空    
  10.      if (ticketGrantingTicketId != null) {    
  11.         //那么在centralAuthenticationService中销毁    
  12.          this.centralAuthenticationService    
  13.              .destroyTicketGrantingTicket(ticketGrantingTicketId);    
  14.          //ticketGrantingTicketCookieGenerator 中销毁cookie    
  15.          this.ticketGrantingTicketCookieGenerator.removeCookie(response);    
  16.          //warnCookieGenerator 中销毁    
  17.          this.warnCookieGenerator.removeCookie(response);    
  18.      }    
  19.      // 如果参数:followServiceRedirects为true 同时service不会空的时候,跳转到service指定的URL    
  20.      if (this.followServiceRedirects && service != null) {    
  21.          return new ModelAndView(new RedirectView(service));    
  22.      }    
  23.      //否则,跳转到logoutView指定的页面    
  24.      return new ModelAndView(this.logoutView);    
  25. }  

    相信,看到下面这句话,你就明白为什么配置第一步和第二步了


  

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 页面上。

         耐心的看看这些开源框架的源码,你很容易就明白了为什么这么做;当然如果熟读源码的话,你就可以按着自己的想法随意的修改框架的各个部分。

0 0