重写默认的AuthenticationFilter类中部分方法
1 import java.io.IOException; 2 3 import javax.servlet.FilterChain; 4 import javax.servlet.FilterConfig; 5 import javax.servlet.ServletException; 6 import javax.servlet.ServletRequest; 7 import javax.servlet.ServletResponse; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import javax.servlet.http.HttpSession; 11 12 import org.jasig.cas.client.authentication.DefaultGatewayResolverImpl; 13 import org.jasig.cas.client.authentication.GatewayResolver; 14 import org.jasig.cas.client.util.AbstractCasFilter; 15 import org.jasig.cas.client.util.CommonUtils; 16 import org.jasig.cas.client.validation.Assertion; 17 18 public class ReAuthenticationFilter extends AbstractCasFilter { 19 private String casServerLoginUrl; 20 private boolean renew = false; 21 22 private boolean gateway = false; 23 24 private GatewayResolver gatewayStorage = new DefaultGatewayResolverImpl(); 25 26 // 不拦截的路径 27 private String[] excludePaths; 28 29 protected void initInternal(FilterConfig filterConfig)throws ServletException { 30 if (!isIgnoreInitConfiguration()) { 31 super.initInternal(filterConfig); 32 setCasServerLoginUrl(getPropertyFromInitParams(filterConfig, 33 "casServerLoginUrl", null)); 34 this.log.trace("Loaded CasServerLoginUrl parameter: " 35 + this.casServerLoginUrl); 36 setRenew(parseBoolean(getPropertyFromInitParams(filterConfig, 37 "renew", "false"))); 38 this.log.trace("Loaded renew parameter: " + this.renew); 39 setGateway(parseBoolean(getPropertyFromInitParams(filterConfig, 40 "gateway", "false"))); 41 this.log.trace("Loaded gateway parameter: " + this.gateway); 42 43 String gatewayStorageClass = getPropertyFromInitParams( 44 filterConfig, "gatewayStorageClass", null); 45 46 if (gatewayStorageClass != null) { 47 try { 48 this.gatewayStorage = ((GatewayResolver) Class.forName( 49 gatewayStorageClass).newInstance()); 50 } catch (Exception e) { 51 this.log.error(e, e); 52 throw new ServletException(e); 53 } 54 } 55 } 56 //拦截器过滤修改************begin************************* 57 String _excludePaths = getPropertyFromInitParams(filterConfig, "exceptPaths", null); 58 if(CommonUtils.isNotBlank(_excludePaths)){ 59 excludePaths = _excludePaths.trim().split(","); 60 } 61 //拦截器过滤修改************end************************ 62 } 63 64 public void init() { 65 super.init(); 66 CommonUtils.assertNotNull(this.casServerLoginUrl,"casServerLoginUrl cannot be null."); 67 } 68 69 public final void doFilter(ServletRequest servletRequest, 70 ServletResponse servletResponse, FilterChain filterChain) 71 throws IOException, ServletException { 72 HttpServletRequest request = (HttpServletRequest) servletRequest; 73 HttpServletResponse response = (HttpServletResponse) servletResponse; 74 HttpSession session = request.getSession(false); 75 Assertion assertion = session != null ? (Assertion) session.getAttribute("_const_cas_assertion_") : null; 76 77 // 拦截器过滤修改************begin******************** 78 String uri = request.getRequestURI(); 79 if (excludePaths != null && excludePaths.length > 0 && uri != null) { 80 for (String path : excludePaths) { 81 if (CommonUtils.isNotBlank(path)) { 82 if (uri.contains(path)) { 83 filterChain.doFilter(request, response); 84 return; 85 } 86 } 87 } 88 } 89 // 拦截器过滤修改************end******************************** 90 91 if (assertion != null) { 92 filterChain.doFilter(request, response); 93 return; 94 } 95 96 String serviceUrl = constructServiceUrl(request, response); 97 String ticket = CommonUtils.safeGetParameter(request, 98 getArtifactParameterName()); 99 boolean wasGatewayed = this.gatewayStorage.hasGatewayedAlready(request, 100 serviceUrl); 101 102 if ((CommonUtils.isNotBlank(ticket)) || (wasGatewayed)) { 103 filterChain.doFilter(request, response); 104 return; 105 } 106 107 this.log.debug("no ticket and no assertion found"); 108 String modifiedServiceUrl; 109 if (this.gateway) { 110 this.log.debug("setting gateway attribute in session"); 111 modifiedServiceUrl = this.gatewayStorage.storeGatewayInformation(request, serviceUrl); 112 } else { 113 modifiedServiceUrl = serviceUrl; 114 } 115 116 if (this.log.isDebugEnabled()) { 117 this.log.debug("Constructed service url: " + modifiedServiceUrl); 118 } 119 120 String urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casServerLoginUrl, getServiceParameterName(), 121 modifiedServiceUrl, this.renew, this.gateway); 122 123 if (this.log.isDebugEnabled()) { 124 this.log.debug("redirecting to "" + urlToRedirectTo + """); 125 } 126 127 response.sendRedirect(urlToRedirectTo); 128 } 129 130 public final void setRenew(boolean renew) { 131 this.renew = renew; 132 } 133 134 public final void setGateway(boolean gateway) { 135 this.gateway = gateway; 136 } 137 138 public final void setCasServerLoginUrl(String casServerLoginUrl) { 139 this.casServerLoginUrl = casServerLoginUrl; 140 } 141 142 public final void setGatewayStorage(GatewayResolver gatewayStorage) { 143 this.gatewayStorage = gatewayStorage; 144 } 145 146 public String[] getExcludePaths() { 147 return excludePaths; 148 } 149 150 public void setExcludePaths(String[] excludePaths) { 151 this.excludePaths = excludePaths; 152 } 153 }
web.xml修改
<filter> <filter-name>CAS Filter</filter-name> <filter-class>com.genilex.utils.ReAuthenticationFilter</filter-class> <init-param> <param-name>casServerLoginUrl</param-name> <param-value>http://ip:8081/CAS/login</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>http://ip:8080</param-value> </init-param> <init-param> <description>cas not filter url</description> <param-name>exceptPaths</param-name> <param-value>/test/*</param-value> </init-param> </filter> <filter-mapping> <filter-name>CAS Filter</filter-name> <!-- 拦截路径 --> <url-pattern>/*</url-pattern> </filter-mapping>