Shiro源码学习(二)应用拦截器链

本文深入探讨了Shiro如何将配置的拦截器链整合到服务器Filter链中,以实现权限控制。从SpringShiroFilter出发,分析了doFilterInternal方法及其内部的拦截器链构建过程,包括如何匹配URL、组合拦截器以及最终执行拦截的逻辑,揭示了Shiro动态构建和应用拦截器链的机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将配置的拦截器链加入到FilterChain中

我们可能会在shiro中配置上图这样的拦截器链,但是像Tomcat这样的服务器中的Filter都是需要配置在web.xml中才会生效。而在上篇文章中,我们在web.xml中只配置了一个Filter,所以Shiro需要做的就是把配置的拦截器链“嫁接”到服务器原来的Filter链中。

在上篇中,我们提到,最后发挥拦截功能的是SpringShiroFilter这个拦截器,所以也从它开始分析

SpringShiroFilter的继承体系


其中OncePerRequestFilter对Filter接口的doFilter方法进行了重载:

	//从类的名字可以看出,该Filter对一次请求只会应用一次拦截
    public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
        if ( request.getAttribute(alreadyFilteredAttributeName) != null ) {
            log.trace("Filter '{}' already executed.  Proceeding without invoking this filter.", getName());
            filterChain.doFilter(request, response);
        } else //noinspection deprecation
            if (/* added in 1.2: */ !isEnabled(request, response) ||		//isEnabled方法默认返回true,shouldNotFilter默认返回false
                /* retain backwards compatibility: */ shouldNotFilter(request) ) {
            log.debug("Filter '{}' is not enabled for the current request.  Proceeding without invoking this filter.",
                    getName());
            filterChain.doFilter(request, response);
        } else {
			//将当前Filter的name添加到Request的属性中,在上面第一、二行代码中可以看到,如果请求再次遇到该Filter不会出现二次拦截
            request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);	

            try {
           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值