shiro中的过滤器

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过滤一次,不会重复过滤

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

    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中)方法。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在使用Shiro进行权限控制时,可以通过添加Shiro内置过滤器来实现对不同请求进行权限验证和访问控制。在配置Shiro的过程,我们可以通过配置ShiroFilterFactoryBean来添加内置过滤器。 在Shiro的内置过滤器,常用的有以下几种: 1. `anon`:匿名访问,允许所有用户访问,一般用于静态资源的放行。 2. `authc`:身份认证,要求用户已经登录才能访问,如果未登录,则跳转到登录页面进行认证。 3. `user`:用户认证通过或者记住我登录的都可以访问,一般用于一些需要登录后才能进行的操作。 4. `perms`:需要指定权限才能访问,例如`/admin`接口需要拥有`admin`权限才能访问。 5. `roles`:需要指定角色才能访问,例如`/user`接口需要拥有`user`角色才能访问。 6. `logout`:退出登录,执行`logout`操作后会跳转到登录页面。 配置Shiro内置过滤器的方法是在ShiroFilterFactoryBean的配置使用`filterChainDefinitionMap`属性,通过键值对的方式将请求路径和对应的过滤器进行配置。例如: ```java filterChainDefinitionMap.put("/static/**", "anon"); filterChainDefinitionMap.put("/login", "anon"); filterChainDefinitionMap.put("/admin/**", "authc, perms<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span> #### 引用[.reference_title] - *1* [springmvc+shiro自定义过滤器的实现代码](https://download.csdn.net/download/weixin_38559866/12755515)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* *4* [SpringBoot整合Shiro环境搭建与配置拦截器](https://blog.csdn.net/qq_43880100/article/details/127395422)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值