源代码解读Cas实现单点登出(single sign out)功能实现原理

来源:百度文库 编辑:神马文学网 时间:2024/04/27 13:55:30
接下来,我们来看一下CAS server端回调是怎么实现的
先来看一下配置,我们知道CAS server所有的用户登录,登出操作,都是由CentralAuthenticationServiceImpl对象来管理。
我们就先把到CentralAuthenticationServiceImpl的spring配置,在applicationContext.xml文件中

p:ticketGrantingTicketExpirationPolicy-ref="grantingTicketExpirationPolicy"
p:serviceTicketExpirationPolicy-ref="serviceTicketExpirationPolicy"
p:authenticationManager-ref="authenticationManager"
p:ticketGrantingTicketUniqueTicketIdGenerator-ref="ticketGrantingTicketUniqueIdGenerator"
p:ticketRegistry-ref="ticketRegistry"
p:servicesManager-ref="servicesManager"
p:persistentIdGenerator-ref="persistentIdGenerator"
p:uniqueTicketIdGeneratorsForService-ref="uniqueIdGeneratorsMap" />
配置使用了spring2.0的xsd。CentralAuthenticationServiceImpl有一个属性叫uniqueTicketIdGeneratorsForService,它是一个map对象
它的key值是所有实现org.jasig.cas.authentication.principal.Service接口的类名,用于保存Principal对象和进行单点登出回调

application server时使用 value值为org.jasig.cas.util.DefaultUniqueTicketIdGenerator对象,用于生成唯一的TGC ticket。
该属性引用的uniqueIdGeneratorsMap bean在uniqueIdGenerators.xml配置文件中。

key="org.jasig.cas.authentication.principal.SimpleWebApplicationServiceImpl"
value-ref="serviceTicketUniqueIdGenerator" />
key="org.jasig.cas.support.openid.authentication.principal.OpenIdService"
value-ref="serviceTicketUniqueIdGenerator" />
key="org.jasig.cas.authentication.principal.SamlService"
value-ref="samlServiceTicketUniqueIdGenerator" />
key="org.jasig.cas.authentication.principal.GoogleAccountsService"
value-ref="serviceTicketUniqueIdGenerator" />

那CentralAuthenticationServiceImpl是怎么调用的呢?
我们跟踪一下代码,在创建ticket的方法 public String createTicketGrantingTicket(final Credentials credentials)中
可以找到以下这样一段代码:
1         //创建 TicketGrantingTicketImpl 实例
2             final TicketGrantingTicket ticketGrantingTicket = new TicketGrantingTicketImpl(
3                 this.ticketGrantingTicketUniqueTicketIdGenerator
4                     .getNewTicketId(TicketGrantingTicket.PREFIX),
5                 authentication, this.ticketGrantingTicketExpirationPolicy);
6         //并把该对象保存到 ticketRegistry中
7         this.ticketRegistry.addTicket(ticketGrantingTicket);
上面的代码,看到ticketRegistry对象保存了创建的TicketGrantingTicketImpl对象,下面我们看一下当ticket销毁的时候,会做什么
事情,代码如下:
 1     public void destroyTicketGrantingTicket(final String ticketGrantingTicketId) {
 2         Assert.notNull(ticketGrantingTicketId);
 3 
 4         if (log.isDebugEnabled()) {
 5             log.debug("Removing ticket [" + ticketGrantingTicketId
 6                 + "] from registry.");
 7         }
 8     //从 ticketRegistry对象中,取得TicketGrantingTicket对象
 9         final TicketGrantingTicket ticket = (TicketGrantingTicket) this.ticketRegistry
10             .getTicket(ticketGrantingTicketId, TicketGrantingTicket.class);
11 
12         if (ticket == null) {
13             return;
14         }
15 
16         if (log.isDebugEnabled()) {
17             log.debug("Ticket found.  Expiring and then deleting.");
18         }
19         ticket.expire();//调用expire()方法,让ticket过期失效
20         this.ticketRegistry.deleteTicket(ticketGrantingTicketId);//从ticketRegistry中删除的ticket 对象
21     }
我们看到,它是从ticketRegistry对象中取得TicketGrantingTicket对象后,调用expire方法。接下来,要关心的就是expire方法做什么事情
 1     public synchronized void expire() {
 2         this.expired.set(true);
 3         logOutOfServices();
 4     }
 5 
 6     private void logOutOfServices() {
 7         for (final Entry entry : this.services.entrySet()) {
 8             entry.getValue().logOutOfService(entry.getKey());
 9         }
10     }
从代码可以看到,它是遍历每个 Service对象,并执行logOutOfService方法,参数是String sessionIdentifier
现在我们可以对应中,它存放的Service就是在uniqueIdGeneratorsMap bean定义中的那些实现类

因为logOutOfService方法的实现,所有实现类都是由它们继承的抽象类AbstractWebApplicationService来实现,我们来看一下
AbstractWebApplicationService的logOutOfService方法,就可以最终找出,实现single sign out的真正实现代码,下面是主要代码片段:

 1   public synchronized boolean logOutOfService(final String sessionIdentifier) {
 2         if (this.loggedOutAlready) {
 3             return true;
 4         }
 5 
 6         LOG.debug("Sending logout request for: " + getId());
 7         //组装 logoutRequest参数内容
 8         final String logoutRequest = " 9             + GENERATOR.getNewTicketId("LR")
10             + "\" Version=\"2.0\" IssueInstant=\"" + SamlUtils.getCurrentDateAndTime()
11             + "\">12 
13 xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\">@NOT_USED@"
14             + sessionIdentifier + "
";
15         
16         this.loggedOutAlready = true;
17         //回调所有的application,getOriginalUrl()是取得回调的application url
18         if (this.httpClient != null) {
19             return this.httpClient.sendMessageToEndPoint(getOriginalUrl(), logoutRequest);
20         }
21         
22         return false;
23     }
至此,已经通过源代码把 CAS实现 single sign out的实现原理和方法完整叙述了一遍,希望对CAS感兴趣的朋友有所帮忙,
如果有什么问题也希望大家提出和指正。