CAS实现 SSO的原理和方法简介
1. 了解一下cas 实现single sign out的原理,如图所示:
图一 演示了单点登陆的工作原理。
当一个web浏览器登录到应用服务器时,应用服务器(application)会检测用户的session,如果没有session,则应用服务器会把url跳转到CAS server上,要求用户登录,用户登录成功后,CAS server会记请求的application的url和该用户的sessionId(在应用服务器跳转url时,通过参数传给CAS server)。此时在CAS服务器会种下TGC Cookie值到webbrowser.拥有该TGC Cookie的webbrowser可以无需登录进入所有建立sso服务的应用服务器application。 图二 演示了单点登出的工作原理。当一个web浏览器要求登退应用服务器,应用服务器(application)会把url跳转到CAS server上的 /cas/logout url资源上,CAS server接受请求后,会检测用户的TCG Cookie,把对应的session清除,同时会找到所有通过该TGC sso登录的应用服务器URL提交请求,所有的回调请求中,包含一个参数logoutRequest,内容格式如下:
2. 结合源代码来讲述一下内部的代码怎么实现的首先,要实现SSO在 应用服务器application端的web.xml要加入以下配置
public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException { final HttpServletRequest request = (HttpServletRequest) servletRequest; if ("POST".equals(request.getMethod())) { final String logoutRequest = request.getParameter("logoutRequest"); if (CommonUtils.isNotBlank(logoutRequest)) { if (log.isTraceEnabled()) { log.trace ("Logout request=[" + logoutRequest + "]");
} //从xml中解析 SessionIndex key值 final String sessionIdentifier = XmlUtils.getTextForElement(logoutRequest, "SessionIndex"); if (CommonUtils.isNotBlank(sessionIdentifier)) { //根据sessionId取得session对象 final HttpSession session = SESSION_MAPPING_STORAGE.removeSessionByMappingId(sessionIdentifier); if (session != null) { String sessionID = session.getId(); if (log.isDebugEnabled()) { log.debug ("Invalidating session [" + sessionID + "] for ST [" + sessionIdentifier + "]"); } try { //让session失效 session.invalidate(); } catch (final IllegalStateException e) { log.debug(e,e); } } return; }
} } else {//get方式 表示登录,把session对象放到 SESSION_MAPPING_STORAGE(map对象中) final String artifact = request.getParameter(this.artifactParameterName); final HttpSession session = request.getSession(); if (log.isDebugEnabled() && session != null) { log.debug("Storing session identifier for " + session.getId()); } if (CommonUtils.isNotBlank(artifact)) { SESSION_MAPPING_STORAGE.addSessionById(artifact, session); } } filterChain.doFilter(servletRequest, servletResponse); }
SingleSignOutHttpSessionListener实现了javax.servlet.http.HttpSessionListener接口,用于监听session销毁事件
public final class SingleSignOutHttpSessionListener implements HttpSessionListener { private Log log = LogFactory.getLog(getClass()); private SessionMappingStorage SESSION_MAPPING_STORAGE; public void sessionCreated(final HttpSessionEvent event) { // nothing to do at the moment } //session销毁时 public void sessionDestroyed(final HttpSessionEvent event) { if (SESSION_MAPPING_STORAGE == null) {//如果为空,创建一个sessionMappingStorage 对象 SESSION_MAPPING_STORAGE = getSessionMappingStorage(); } final HttpSession session = event.getSession();//取得当然要销毁的session对象 if (log.isDebugEnabled()) { log.debug("Removing HttpSession: " + session.getId()); } //从SESSION_MAPPING_STORAGE map根据sessionId移去session对象 SESSION_MAPPING_STORAGE.removeBySessionById(session.getId()); }
protected static SessionMappingStorage getSessionMappingStorage() {
return SingleSignOutFilter.getSessionMappingStorage();
}
}
3 CAS server端回调是怎么实现的先来看一下配置,我们知道CAS server所有的用户登录,登出操作,都是由CentralAuthenticationServiceImpl对象来管理。我们就先把到CentralAuthenticationServiceImpl的spring配置,在applicationContext.xml文件中
配置使用了spring2.0的xsd。CentralAuthenticationServiceImpl有一个属性叫uniqueTicketIdGeneratorsForService,它是一个map对象它的key值是所有实现org.jasig.cas.authentication.principal.Service接口的类名,用于保存Principal对象和进行单点登出回调4 TGC ticket的产生application server时使用 value值为org.jasig.cas.util.DefaultUniqueTicketIdGenerator对象,用于生成唯一的TGC ticket。该属性引用的uniqueIdGeneratorsMap bean在uniqueIdGenerators.xml配置文件中。
那CentralAuthenticationServiceImpl是怎么调用的呢?我们跟踪一下代码,在创建ticket的方法 public String createTicketGrantingTicket(final Credentials credentials)中可以找到以下这样一段代码:
//创建 TicketGrantingTicketImpl 实例 final TicketGrantingTicket ticketGrantingTicket = new TicketGrantingTicketImpl( this.ticketGrantingTicketUniqueTicketIdGenerator .getNewTicketId(TicketGrantingTicket.PREFIX), authentication, this.ticketGrantingTicketExpirationPolicy); //并把该对象保存到 ticketRegistry中7 this.ticketRegistry.addTicket(ticketGrantingTicket);
5 TGC ticket 的销毁ticketRegistry对象保存了创建的TicketGrantingTicketImpl对象,下面我们看一下当ticket销毁的时候,会做什么
代码如下:
public void destroyTicketGrantingTicket(final String ticketGrantingTicketId) { Assert.notNull(ticketGrantingTicketId); if (log.isDebugEnabled()) { log.debug("Removing ticket [" + ticketGrantingTicketId + "] from registry."); } //从 ticketRegistry对象中,取得TicketGrantingTicket对象 final TicketGrantingTicket ticket = (TicketGrantingTicket) this.ticketRegistry .getTicket(ticketGrantingTicketId, TicketGrantingTicket.class); if (ticket == null) { return; } if (log.isDebugEnabled()) { log.debug("Ticket found. Expiring and then deleting."); } ticket.expire();//调用expire()方法,让ticket过期失效 this.ticketRegistry.deleteTicket(ticketGrantingTicketId);//从ticketRegistry中删除的ticket 对象 }
我们看到,它是从ticketRegistry对象中取得TicketGrantingTicket对象后,调用expire方法。接下来,要关心的就是expire方法做什么事情
public synchronized void expire() { this.expired.set(true); logOutOfServices();
}
private void logOutOfServices() { for (final Entry
从代码可以看到,它是遍历每个 Service对象,并执行logOutOfService方法,参数是String sessionIdentifier现在我们可以对应到它存放的Service就是在uniqueIdGeneratorsMap bean定义中的那些实现类因为logOutOfService方法的实现,所有实现类都是由它们继承的抽象类AbstractWebApplicationService来实现,我们来看一下AbstractWebApplicationService的logOutOfService方法,就可以最终找出,实现SSO的真正实现代码,下面是主要代码片段:
public synchronized boolean logOutOfService(final String sessionIdentifier) { if (this.loggedOutAlready) { return true; } LOG.debug("Sending logout request for: " + getId()); //组装 logoutRequest参数内容 final String logoutRequest = "
xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\">@NOT_USED@
this.loggedOutAlready = true; //回调所有的application,getOriginalUrl()是取得回调的application url if (this.httpClient != null) {
return this.httpClient.sendMessageToEndPoint(getOriginalUrl(), logoutRequest); } return false; }
至此,已经通过源代码把 CAS实现 single sign out的实现原理和方法完整叙述了一遍
本文来源:https://www.2haoxitong.net/k/doc/e581ecf7fab069dc502201f9.html
文档为doc格式