将几个Filter整合成一个Filter
2016-07-08 00:00
489 查看
CAS-Client的客户端配置还是多Filter,这个对于接入系统比较繁琐,我试着将这几个Filter合并成一个。
FilterChainProxy :其职责是给web.xml使用,接受其他需要合并的Filter的参数,初始化其他Filter.
FilterInvocation:doFilter的参数的封装,取得路径信息等。
VirtualFilterChain:实现FilterChain,
代码如下:
FilterChainProxy
VirtualFilterChain
FilterInvocation
FilterChainProxy :其职责是给web.xml使用,接受其他需要合并的Filter的参数,初始化其他Filter.
FilterInvocation:doFilter的参数的封装,取得路径信息等。
VirtualFilterChain:实现FilterChain,
代码如下:
FilterChainProxy
public class FilterChainProxy extends AbstractConfigurationFilter { Logger logger = Logger.getLogger(FilterChainProxy.class); private Filter[] filters; /* (non-Javadoc) * @see javax.servlet.Filter#destroy() */ public void destroy() { for (int i = 0; i < filters.length; i++) { if (filters[i] != null) { if (logger.isDebugEnabled()) { logger.debug("Destroying Filter defined in ApplicationContext: '" + filters[i].toString() + "'"); } filters[i].destroy(); } } } /* (non-Javadoc) * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { FilterInvocation fi = new FilterInvocation(request, response, chain); if (filters.length == 0) { if (logger.isDebugEnabled()) { logger.debug(fi.getRequestUrl()+" has an empty filter list"); } chain.doFilter(request, response); return; } VirtualFilterChain virtualFilterChain = new VirtualFilterChain(fi, filters); virtualFilterChain.doFilter(fi.getRequest(), fi.getResponse()); } /* (non-Javadoc) * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) */ public void init(FilterConfig filterConfig) throws ServletException { filters = obtainAllDefinedFilters(); for (int i = 0; i < filters.length; i++) { if (filters[i] != null) { if (logger.isDebugEnabled()) { logger.debug("Initializing Filter defined in ApplicationContext: '" + filters[i].toString() + "'"); } filters[i].init(filterConfig); } } } /** * 初始化单点登录需要的过滤器 * @return */ private Filter[] obtainAllDefinedFilters() { Set<Filter> list = new LinkedHashSet<Filter>(); // SingleSignOutFilter list.add(new SingleSignOutFilter()); // AuthenticationFilter list.add(new AuthenticationFilter()); // Cas20ProxyReceivingTicketValidationFilter list.add(new Cas20ProxyReceivingTicketValidationFilter()); // AssertionThreadLocalFilter list.add(new AssertionThreadLocalFilter()); // HttpServletRequestWrapperFilter list.add(new HttpServletRequestWrapperFilter()); return (Filter[]) list.toArray(new Filter[0]); }
VirtualFilterChain
private class VirtualFilterChain implements FilterChain { private FilterInvocation fi; private Filter[] additionalFilters; private int currentPosition = 0; public VirtualFilterChain(FilterInvocation filterInvocation, Filter[] additionalFilters) { this.fi = filterInvocation; this.additionalFilters = additionalFilters; } public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { if (currentPosition == additionalFilters.length) { if (logger.isDebugEnabled()) { logger.debug(fi.getRequestUrl() + " reached end of additional filter chain; proceeding with original chain"); } fi.getChain().doFilter(request, response); } else { currentPosition++; if (logger.isDebugEnabled()) { logger.debug(fi.getRequestUrl() + " at position " + currentPosition + " of " + additionalFilters.length + " in additional filter chain; firing Filter: '" + additionalFilters[currentPosition - 1] + "'"); } additionalFilters[currentPosition - 1].doFilter(request, response, this); } } }
FilterInvocation
public class FilterInvocation { private FilterChain chain; private ServletRequest request; private ServletResponse response; public FilterInvocation(ServletRequest request, ServletResponse response, FilterChain chain) { if ((request == null) || (response == null) || (chain == null)) { throw new IllegalArgumentException("Cannot pass null values to constructor"); } if (!(request instanceof HttpServletRequest)) { throw new IllegalArgumentException("Can only process HttpServletRequest"); } if (!(response instanceof HttpServletResponse)) { throw new IllegalArgumentException("Can only process HttpServletResponse"); } this.request = request; this.response = response; this.chain = chain; } public FilterChain getChain() { return chain; } /** * Indicates the URL that the user agent used for this request.<P>The returned URL does <b>not</b> reflect * the port number determined from a {@link org.acegisecurity.util.PortResolver}.</p> * * @return the full URL of this request */ public String getFullRequestUrl() { return UrlUtils.getFullRequestUrl(this); } public HttpServletRequest getHttpRequest() { return (HttpServletRequest) request; } public HttpServletResponse getHttpResponse() { return (HttpServletResponse) response; } public ServletRequest getRequest() { return request; } /** * Obtains the web application-specific fragment of the URL. * * @return the URL, excluding any server name, context path or servlet path */ public String getRequestUrl() { return UrlUtils.getRequestUrl(this); } public ServletResponse getResponse() { return response; } public String toString() { return "FilterInvocation: URL: " + getRequestUrl(); } }
相关文章推荐
- FastDFS的配置、部署与API使用解读(1)Get Started with FastDFS
- 更改MYSQL数据库不区分大小写表名
- Maximum execution time of 30 seconds exceeded故障解决办法
- JVM调优总结(二)-一些概念
- Eclipse中启动插件tomcat时出现Timeout waiting 问题
- Linux 下MongoDB的安装以及使用
- 搜索引擎-倒排索引基础知识 .
- JVM调优总结(十二)-参考资料
- 了解 Web 服务规范: 第 2 部分:Web 服务描述语言 (WSDL)
- 使用 Apache Solr 实现更加灵巧的搜索,第 1 部分: 基本特性和 Solr 模式
- FastDFS的配置、部署与API使用解读(6)FastDFS配置详解之Storage配置
- JVM调优总结(九)-新一代的垃圾回收算法
- 说说IO(四)- 文件系统
- 说说IO(七)- RAID
- MongoDB的备份(mongodump)与恢复(mongorestore)
- Java深度历险(一)——Java字节代码的操纵
- FastDFS的配置、部署与API使用解读(4)FastDFS配置详解之Client配置
- 测试用例的一些原则
- 博客消失历险记
- Axis2配置指南(翻译)