*** 這個類的做用:判斷是否已經登陸,若是沒有登陸則根據配置的信息來決定將跳轉到什麼地方 ***html
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); }