使用@WebFilter过滤器中设置例外URL的一种方式

9 篇文章 0 订阅

解决方式:

@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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值