源代码解读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感兴趣的朋友有所帮忙,
如果有什么问题也希望大家提出和指正。
先来看一下配置,我们知道CAS server所有的用户登录,登出操作,都是由CentralAuthenticationServiceImpl对象来管理。
我们就先把到CentralAuthenticationServiceImpl的spring配置,在applicationContext.xml文件中
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配置文件中。
value-ref="serviceTicketUniqueIdGenerator" />
value-ref="serviceTicketUniqueIdGenerator" />
value-ref="samlServiceTicketUniqueIdGenerator" />
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
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 = "
10 + "\" Version=\"2.0\" IssueInstant=\"" + SamlUtils.getCurrentDateAndTime()
11 + "\">
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感兴趣的朋友有所帮忙,
如果有什么问题也希望大家提出和指正。
源代码解读Cas实现单点登出(single sign out)功能实现原理
WebSphere环境下的SSO(Single sign-on:单点登录、全网漫游)实现之...
耶鲁CAS Single Sign On--星月夜
用Yale CAS Server 来实现单点登陆(SSO)
Liferay中使用CAS实现单点登陆(SSO)
计算机科学论坛--一个yale-cas实现单点登录的简单例子实现
在Tomcat中使用Yale CAS实现单点登陆(SSO) - 程序世界 - CSDNBl...
在Tomcat中使用Yale CAS实现单点登陆(SSO) 文章合集dsf
使用cas实现sso
实现单点登陆(SSO)
Liferay的CAS SSO实现思路
技术文章-单点登录的简单实现
Jetspeed2.0中单点登录的实现
Jetspeed2.0中单点登录的实现
P2P之UDP穿透NAT的原理与实现(附源代码)
P2P之UDP穿透NAT的原理与实现(shootingstars)--增强篇(附源代码)
RMI原理及实现
taglib 原理和实现
RMI原理及实现
ORM的实现原理
LCD驱动实现原理
RSS原理和实现
LCD驱动实现原理
实现关键字搜索功能