标签:
在做的项目需要用到cas,而使用cas的话,需要在client的webapp的web.xml中配置好多个filter,但是需要兼容到老的逻辑,如果满足某些条件,就不走cas的filter,满足另外一些条件,只走cas的其中几个filter,那该怎么进行filter的相关配置呢?
从网上搜到了一片blog:http://blog.csdn.net/tandaly/article/details/8687671,里面讲述了两种方式。
第一种是给filter设置标识。我目前考虑,在自己项目中采取这种为filter加标识的方式:
publicclass FirstFilter implementsFilter { //... publicvoid doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)throwsIOException, ServletException { servletRequest.setAttribute("executeSecondFilter",true); //... if(someReason) servletRequest.setAttribute("executeSecondFilter",false);
servletRequest.setAttribute("executeThirdFilter",false);
servletRequest.setAttribute("executeForthFilter",true); filterChain.doFilter(servletRequest, servletResponse); } } publicclass SecondFilter implementsFilter { //.. publicvoid doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)throwsIOException, ServletException { if(servletRequest.getAttribute("executeSecondFilter") == null|| !((Boolean) servletRequest.getAttribute("executeSecondFilter"))) { filterChain.doFilter(servletRequest, servletResponse); } //... } }
publicclass ThirdFilter implementsFilter {
//..
publicvoid doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)throwsIOException, ServletException {
if(servletRequest.getAttribute("executeThirdFilter") == null|| !((Boolean) servletRequest.getAttribute("executeThirdFilter"))) {
filterChain.doFilter(servletRequest, servletResponse);
}
//...
}
}
filterChain.doFilter()的作用是跳转到filter链的下一个filter。
标签:
原文地址:http://www.cnblogs.com/sonofelice/p/5143210.html