最近研究CAS,先从客户开始来说明CAS的逻辑,可能会结合源代码。
必要说明:http://blog.csdn.net/yuwenruli/article/details/6602180
先来说说配置过滤器需要的参数吧(参考:http://blog.csdn.net/yuwenruli/article/details/6612010):
casServerLoginUrl :定义CAS服务器的登录URL地址,例如: https://localhost:8443/cas/login
service or serverName:
service :发送到CAS服务器的service URL地址,例如https://localhost:8443/yourwebapp/index.html
serverName:CAS客户端的服务器名称,Service URL使用这个名称动态组装,例如:http://localhost:8080 (必须包括协议,如果端口是标准端口则可以不写,例如80端口)
言归正传,现在从第一个Filter开始,下面是这个Filter的逻辑过程。
我们发现这个Filter的职责只是判断是否已经登录,如果没有登录,则根据配置(gateway)来决定条状到什么地方。
我们来看看源代码中怎么做的,
public final void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException { // 转换参数 final HttpServletRequest request = (HttpServletRequest) servletRequest; final HttpServletResponse response = (HttpServletResponse) servletResponse; //从session中取得Assertion final HttpSession session = request.getSession(false); final Assertion assertion = session != null ? (Assertion) session.getAttribute(CONST_CAS_ASSERTION) : null; //如果存在,则说明已经登录,本过滤器处理完成,处理下个过滤器 if (assertion != null) { filterChain.doFilter(request, response); return; } //如果session中没有Assertion对象,组装serviceUrl并试着从参数中取得ticket属性。 final String serviceUrl = constructServiceUrl(request, response); final String ticket = CommonUtils.safeGetParameter(request,getArtifactParameterName()); final boolean wasGatewayed = this.gatewayStorage.hasGatewayedAlready(request, serviceUrl); //如果ticket不为空,或者wasGatewayed为true,则本过滤器处理完成,处理下个过滤器 if (CommonUtils.isNotBlank(ticket) || wasGatewayed) { filterChain.doFilter(request, response); return; } // 定义需要条状的url地址 final String modifiedServiceUrl; log.debug("no ticket and no assertion found"); //ticket 为空,并且wasGatewayed也为false,则根据初始化参数gateway的值来组装跳转url。 if (this.gateway) { log.debug("setting gateway attribute in session"); modifiedServiceUrl = this.gatewayStorage.storeGatewayInformation(request, serviceUrl); } else { modifiedServiceUrl = serviceUrl; } if (log.isDebugEnabled()) { log.debug("Constructed service url: " + modifiedServiceUrl); } //组装跳转url final String urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casServerLoginUrl, getServiceParameterName(), modifiedServiceUrl, this.renew, this.gateway, this.aspId); if (log.isDebugEnabled()) { log.debug("redirecting to \"" + urlToRedirectTo + "\""); } //跳转到urlToRedirectTo指定的url,如果没有配置gateway,则跳转到casServerLoginUrl参数指定的url。 response.sendRedirect(urlToRedirectTo); }