您的位置:首页 > 其它

CAS-Client客户端研究(一)-AuthenticationFilter

2011-07-12 14:38 363 查看
最近研究CAS,先从客户开始来说明CAS的逻辑,可能会结合源代码。

必要说明:/article/8634081.html

先来说说配置过滤器需要的参数吧(参考:/article/8634088.html):

必要参数

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端口)

可选参数:

renew : 指定renew是否为true,有效值为true和false,如果为true则每次请求都产生新的session。默认是false。

gateway - 指定是否使用防火墙,有效值是true和false,默认是false。

artifactParameterName - 指定request保存票据的参数名称,默认是ticket。

serviceParameterName - 指定request保存service的参数名称,默认是service。

言归正传,现在从第一个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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: