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。
cas client AuthenticationFilter_第1张图片

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

你可能感兴趣的:(cas client AuthenticationFilter)