Skip to content

shiro中的过滤器

参考https://blog.csdn.net/zkcJava/article/details/120292476

一、类继承结构

当我们需要使用 spring 中的过滤器时,通常通过继承 filter 接口或者使用@Webfilter注解实现。shiro中过滤器实现也是通过实现filter接口实现的,shiro中整个filter类结构如下图所示,最顶层的AbstractFilter 实现了filter接口。 image-20230808173823690

二、过滤器分类

在DefaultFilter 枚举类中列出了shiro中常用的filter image-20230808164721715

三、shiro过滤器分析

1.OncePerRequestFilter

这个类重写了dofilter方法,用来保证每个请求只会被一个filter过滤一次,不会重复过滤

java
public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
    String alreadyFilteredAttributeName = this.getAlreadyFilteredAttributeName();
    if (request.getAttribute(alreadyFilteredAttributeName) != null) {
        log.trace("Filter '{}' already executed. Proceeding without invoking this filter.", this.getName());
        filterChain.doFilter(request, response);
    } else if (this.isEnabled(request, response) && !this.shouldNotFilter(request)) {
        log.trace("Filter '{}' not yet executed. Executing now.", this.getName());
        request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE);
        try {
            this.doFilterInternal(request, response, filterChain);
        } finally {
            request.removeAttribute(alreadyFilteredAttributeName);
        }
    } else {
        log.debug(
                "Filter '{}' is not enabled for the current request. Proceeding without invoking this filter.",
                this.getName());
        filterChain.doFilter(request, response);
    }
}

如果当前filter没有执行过过滤,默认会进入第二个if(那两个条件默认是成立的) (1)为request设置一个已经执行过滤器的属性名称在request中,默认为TRUE; (2)调用doFilterInternal方法,执行真正的过滤器处理逻辑 (3)这个过滤器处理完后,将过滤器的属性名称从request中移出。这样如果程序执行到第2步,过滤又被调用了,它将会走到第一个if中,直接略过这个过滤器的处理,这样就保证了,每个过滤器在处理一个请求的时候只会被执行一次 总结:OncePerRequestFilter提供了一个实际处理过滤业务的方法doFilterInternal,并且保证每个请求只会被该过滤器过滤一次。image-20230808170449362

2.AdviceFilter

java
public void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain)
        throws ServletException, IOException {
    Exception exception = null;
    try {
        boolean continueChain = this.preHandle(request, response);
        if (log.isTraceEnabled()) {
            log.trace("Invoked preHandle method. Continuing chain?: [" + continueChain + "]");
        }
        if (continueChain) {
            this.executeChain(request, response, chain);
        }
        this.postHandle(request, response);
        if (log.isTraceEnabled()) {
            log.trace("Successfully invoked postHandle method");
        }
    } catch (Exception var9) {
        exception = var9;
    } finally {
        this.cleanup(request, response, exception);
    }
}

相当于aop的环绕通知,重写doFilterInternal方法,添加preHandle和postHandle方法,可以进行前置处理和后置处理

3.PathMatchingFilter

PathMatchingFilter对配置了url请求拦截的地址进行过滤器过滤,对没有配置拦截的url请求直接放行,不进行拦截。如果请求需要过滤,则处理过滤的逻辑由子类实现onPreHandle完成 这个类重写了父类的prehandle方法,用来对特定的url进行过滤,如果用户的请求与配置中拦截的请求匹配,则会调用isFilterChainContinued方法进行下一步处理。如果拦截器是开启的,则调用方法onPreHandle进行拦截处理. image-20230808173907023 onPreHandle方法默认返回true,也不对请求进行拦截处理,所以如果需要有自定义处理拦截的逻辑,需要由子类覆盖这个方法来完成 image-20230808174345342

4.AccessControlFilter

AccessControlFilter中的onPreHandle处理真正的拦截逻辑,isAccessAllowed方法验证用户是否登录,onAccessDenied处理用户没登录后的逻辑,isAccessAllowed和onAccessDenied方法的实现在子类。如果用户已登录,那么过滤器将直接放行,如果用户没有登录,那么再由其子类中的onAccessDenied方法处理后续逻辑 image-20230808175302007

5.AuthenticationFilter

AuthenticationFilter实现了isAccessAllowed方法,如果用户已登录,那么过滤器将直接放行,如果用户没有登录,那么再由其子类中的onAccessDenied方法处理后续逻辑 image-20230808181111400

6.AuthenticatingFilter

AuthenticatingFilter提供了一些如登录,和重定向跳转的方法,并没有onAccessDenied方法的实现,那么这个实现就只能在最后一个子类FormAuthenticationFilter中完成了

7.FormAuthenticationFilter

该类实现了onAccessDenied方法,用来处理没有登录的逻辑,首先判断是否为登录请求,如果为登录请求,通过isLoginSubmission判断请求方式,如果是get说明用户直接通过浏览器访问登录页面,返回false,直接放行;如果用户是一个http的post请求,那么就执行executeLogin(request, response)方法做登录操作。 image-20230808181409249

总结

首先过滤器会调用doFilter(在OncePerRequestFilter中)方法,然后再调用doFilterInternal方法(在AdviceFilter中),然后再调用preHandle、executeChain、postHandle(在AdviceFilter中)这3个方法,实际拦截业务在preHandle方法中,然后再调用onPreHandle(在AccessControlFilter中)方法,然后再调用isAccessAllowed(在AuthenticationFilter中)方法和onAccessDenied(在FormAuthenticationFilter中)方法。

本文转自 https://blog.csdn.net/boker123/article/details/132173102,如有侵权,请联系删除。

Released under the MIT License.