<on-start> <evaluate expression="initialFlowSetupAction"/> </on-start>
WebUtils.putTicketGrantingTicketInScopes(context,this.ticketGrantingTicketCookieGenerator.retrieveCookieValue(request));
try { final Cookie cookie = org.springframework.web.util.WebUtils.getCookie( request, getCookieName()); return cookie == null ? null : this.casCookieValueManager.obtainCookieValue(cookie, request); } catch (final Exception e) { logger.debug(e.getMessage(), e); } return null;
代码说明:逻辑比较简单,直接从cookie中获取TGC(getCookieName()方法有兴趣能够追踪下看看),若是TGC不为空,调用this.casCookieValueManager.obtainCookieValue(cookie, request)方法解析TGC获得TGT,问题逐渐明朗了,TGT是根据TGC获取到的。html
final String cookieValue = this.cipherExecutor.decode(cookie.getValue()); LOGGER.debug("Decoded cookie value is [{}]", cookieValue); if (StringUtils.isBlank(cookieValue)) { LOGGER.debug("Retrieved decoded cookie value is blank. Failed to decode cookie [{}]", cookie.getName()); return null; } final String[] cookieParts = cookieValue.split(String.valueOf(COOKIE_FIELD_SEPARATOR)); if (cookieParts.length != COOKIE_FIELDS_LENGTH) { throw new IllegalStateException("Invalid cookie. Required fields are missing"); } final String value = cookieParts[0]; final String remoteAddr = cookieParts[1]; final String userAgent = cookieParts[2]; if (StringUtils.isBlank(value) || StringUtils.isBlank(remoteAddr) || StringUtils.isBlank(userAgent)) { throw new IllegalStateException("Invalid cookie. Required fields are empty"); } if (!remoteAddr.equals(request.getRemoteAddr())) { throw new IllegalStateException("Invalid cookie. Required remote address does not match " + request.getRemoteAddr()); } if (!userAgent.equals(request.getHeader("user-agent"))) { throw new IllegalStateException("Invalid cookie. Required user-agent does not match " + request.getHeader("user-agent")); } return value;
代码说明:首先解密TGC后获得一个由@符号分隔的字符串,分隔后获取到TGT、客户端IP、客户端代理信息。并将从TGC中解密的客户端IP信息和客户端代理信息与当前请求的客户端IP信息和客户端代理信息进行比较,若不相等就抛出异常(Cas的安全策略)。web
WebUtils.putTicketGrantingTicketInScopes(context, this.ticketGrantingTicketCookieGenerator.retrieveCookieValue(request));
String cookie = this.ticketGrantingTicketCookieGenerator.retrieveCookieValue(request); //增长从request中获取tgt if (cookie == null && request.getRequestURI().contains("/login")) { String tgt = request.getParameter("tgt"); if (StringUtils.isNotBlank(tgt)) { HttpServletResponse response = WebUtils.getHttpServletResponse(context); //TGT生成为TGC并添加客户端cookie中 this.ticketGrantingTicketCookieGenerator.addCookie(request, response, tgt); //tgt直接付值给cookie cookie = tgt; } } WebUtils.putTicketGrantingTicketInScopes(context,cookie);
<on-start> <evaluate expression="initialFlowSetupExAction"/> </on-start>