public String obtainCookieValue(final Cookie cookie, final HttpServletRequest request){ 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()); returnnull; }
final String[] cookieParts = cookieValue.split(String.valueOf(COOKIE_FIELD_SEPARATOR)); if (cookieParts.length != COOKIE_FIELDS_LENGTH) { thrownew 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)) { thrownew IllegalStateException("Invalid cookie. Required fields are empty"); }
if (!remoteAddr.equals(request.getRemoteAddr())) { thrownew IllegalStateException("Invalid cookie. Required remote address does not match " + request.getRemoteAddr()); }
final String agent = WebUtils.getHttpServletRequestUserAgent(request); if (!userAgent.equals(agent)) { thrownew IllegalStateException("Invalid cookie. Required user-agent does not match " + agent); } return value; }
public String buildCookieValue(final String givenCookieValue, final HttpServletRequest request){ final StringBuilder builder = new StringBuilder(givenCookieValue);
final ClientInfo clientInfo = ClientInfoHolder.getClientInfo(); builder.append(COOKIE_FIELD_SEPARATOR); builder.append(clientInfo.getClientIpAddress()); final String userAgent = WebUtils.getHttpServletRequestUserAgent(request); if (StringUtils.isBlank(userAgent)) { thrownew IllegalStateException("Request does not specify a user-agent"); } builder.append(COOKIE_FIELD_SEPARATOR); builder.append(userAgent);
final String res = builder.toString(); LOGGER.debug("Encoding cookie value [{}]", res); returnthis.cipherExecutor.encode(res); }
ticket的生成是从clientInfo.getClientIpAddress()获取客户端ip
我们再看org.apereo.inspektr.common.web.ClientInfo
publicClientInfo(final HttpServletRequest request, final String alternateServerAddrHeaderName, final String alternateLocalAddrHeaderName, finalboolean useServerHostAddress){
修改代码让验证cookie获取客户端ip保持一致,如果是cas server 5.0.4版本可以修改org.apereo.cas.audit.spi.config.CasCoreAuditConfiguration类中的org.apereo.inspektr.common.web.ClientInfoThreadLocalFilter增加初始化参数来自定义客户端ip获取headerName
@Bean public FilterRegistrationBean casClientInfoLoggingFilter() { final FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new ClientInfoThreadLocalFilter()); bean.setUrlPatterns(Collections.singleton("/*")); bean.setName("CAS Client Info Logging Filter"); bean.addInitParameter(ClientInfoThreadLocalFilter.CONST_IP_ADDRESS_HEADER,"X-Forwarded-For"); return bean; }