使用@WebFilter过滤器中设置例外URL的一种方式
解决方式:
@WebFilter(filterName="/CodeFilter",urlPatterns="/*"
,initParams={@WebInitParam(name ="EXCLUDED_PAGES" , value = "login.jsp;loginAction")})
public class AuthorizationCodeFilter implements Filter {
private String excludedPages;
private String[] excludedPageArray;
public void init(FilterConfig fConfig) throws ServletException {
excludedPages = fConfig.getInitParameter("EXCLUDED_PAGES");
if (null!=excludedPages && excludedPages.length()!=0) { // 例外页面不为空
excludedPageArray = excludedPages.split(String.valueOf(';'));
}
}
public void destroy() {
this.excludedPages = null;
this.excludedPageArray = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
boolean isExcludedPage = false;
for (String page : excludedPageArray) {// 遍历例外url数组
// 判断当前URL是否与例外页面相同
if(req.getServletPath().substring(1).equals(page)){ // 从第2个字符开始取(把前面的/去掉)
System.out.println(page + ", you're excluded.");
isExcludedPage = true;
break;
}
}
if (isExcludedPage) {//在过滤url之外
chain.doFilter(request, response);
}
else {// 不在过滤url之外
}
已有 0 人发表留言,猛击->> 这里<<-参与讨论
ITeye推荐