博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cas-client-core-3.2.1配置路径不走server过滤器
阅读量:5153 次
发布时间:2019-06-13

本文共 7130 字,大约阅读时间需要 23 分钟。

重写默认的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修改

CAS Filter
com.genilex.utils.ReAuthenticationFilter
casServerLoginUrl
http://ip:8081/CAS/login
serverName
http://ip:8080
cas not filter url
exceptPaths
/test/*
CAS Filter
/*

 

转载于:https://www.cnblogs.com/jason123/p/8618187.html

你可能感兴趣的文章
小技巧——直接在目录中输入cmd然后就打开cmd命令窗口
查看>>
深浅拷贝(十四)
查看>>
HDU 6370(并查集)
查看>>
BZOJ 1207(dp)
查看>>
HDU 2076 夹角有多大(题目已修改,注意读题)
查看>>
洛谷P3676 小清新数据结构题(动态点分治)
查看>>
九校联考-DL24凉心模拟Day2T1 锻造(forging)
查看>>
Attributes.Add用途与用法
查看>>
L2-001 紧急救援 (dijkstra+dfs回溯路径)
查看>>
javascript 无限分类
查看>>
spring IOC装配Bean(注解方式)
查看>>
[面试算法题]有序列表删除节点-leetcode学习之旅(4)
查看>>
SpringBoot系列五:SpringBoot错误处理(数据验证、处理错误页、全局异常)
查看>>
kubernetes_book
查看>>
侧边栏广告和回到顶部
查看>>
https://blog.csdn.net/u012106306/article/details/80760744
查看>>
海上孤独的帆
查看>>
处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“Manag
查看>>
01: socket模块
查看>>
mysql触发器
查看>>