CAS-Client客户端研究--AuthenticationFilter

必要参数

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);  
       }  

gatewayStorage:

public final class DefaultGatewayResolverImpl implements GatewayResolver {
	
    public static final String CONST_CAS_GATEWAY = "_const_cas_gateway_";

	public boolean hasGatewayedAlready(final HttpServletRequest request,
			final String serviceUrl) {
		final HttpSession session = request.getSession(false);
		
		if (session == null) {
			return false;
		}
		
		final boolean result = session.getAttribute(CONST_CAS_GATEWAY) != null;
		session.removeAttribute(CONST_CAS_GATEWAY);
		return result;
	}

	public String storeGatewayInformation(final HttpServletRequest request,
			final String serviceUrl) {
		request.getSession(true).setAttribute(CONST_CAS_GATEWAY, "yes");
		return serviceUrl;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值