cas AuthenticationFilter

AuthenticationFilter

*** 这个类的作用:判断是否已经登录,如果没有登录则根据配置的信息来决定将跳转到什么地方 ***

    casServerLoginUrl:定义cas 服务器的登录URL地址 如:http://localhost:8443/cas/login
service/serviceName
service:发送到cas服务器的servic URL地址,例如 https://locahost:8443/yourwebapp/index.html
serviceName:cas客户端的服务器名称,service URL使用这个名称动态组装,
例如:http://localhost:8080(必须包括协议,如果端口是标准端口则可以不写)
    renew:指定renew是否为true,有效值为true和false,如果为true,则每次请求都产生新的session。默认是false
gateway: 指定是否使用防火墙,有效值为true或false,默认是false
artifactParameterName: 指定request保存票据的参数名称,默认是ticket
serviceParamterName: 指定request保存service的参数名词,默认是service
    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);
}
上一篇:spring-boot 多线程


下一篇:大型运输行业实战_day01_1_业务分析