ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

shiro相关Filter

2021-05-20 21:01:17  阅读:110  来源: 互联网

标签:filter 拦截器 request Filter 相关 response shiro


0前言

shiro中的filer是使用servlet中的filter接口扩展的,但是shiro代理了servlet 原声的filter,请求到了后会先执行shiro的filter,再执行原声的filter。

前面文章项目启动时shiro加载过程中介绍过,shiro只配置了shiroFilter一个拦截器入口,那么shiro是怎样执行内部那么多filter的?shiro内部的filter又是如何实现的呢?

带着上述两个问题,我们梳理下shiro的filter实现和工作流程

 

1shiro的filter实现

这里照搬一下zhangkaitao博客中的图。

 

 (1)Filter

servlet的filter,不熟悉的可以看一下servlet规范。

(2)AbstractFilter

shiro实现,主要用来配置FilterConfig,init()方法里提供了一个模板方法onFilterConfigSet()供子类实现。

(3)NameableFilter

给filter起名字的,如shiro自带的anon、authc,名字要求唯一,不然会有问题。

(4)OncePerRequestFilter

用于防止多次执行 Filter 的;也就是说一次请求只会走一次拦截器链;另外提供 enabled 属性,表示是否开启该拦截器实例,默认 enabled=true 表示开启,如果不想让某个拦截器工作,可以设置为 false 即可。可以简单看一下dofilter实现,源码107行

    public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName();
//首先检查一下有没有执行过这个filter,通过在request里面设置attribute实现 if ( request.getAttribute(alreadyFilteredAttributeName) != null ) { log.trace("Filter '{}' already executed. Proceeding without invoking this filter.", getName()); filterChain.doFilter(request, response); } else //noinspection deprecation
//在判断一下这个拦截器需不需要执行enable属性为true才需要执行 if (/* added in 1.2: */ !isEnabled(request, response) || /* 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 { // Do invoke this filter... log.trace("Filter '{}' not yet executed. Executing now.", getName()); request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE); try {
//重点,shiro的拦截器都走这个逻辑,这是一个模板方法,具体实现交给子类实现 doFilterInternal(request, response, filterChain); } finally { // Once the request has finished, we're done and we don't // need to mark as 'already filtered' any more. request.removeAttribute(alreadyFilteredAttributeName); } } }

OncePerRequestFilter算是一个比较重要的Filter实现,shiro的所有filter只有这一次dofilter实现。OncePerRequestFilter子类分成了两个分支:

一个是AdviceFilter,该类提供了Filter的Aop风格支持,并且shiro内部授权和验证的filter实现都是在这一分支。

一个是AbstractShiroFilter,也就是web.xml中配置的shiro入口filter。

 

todo

 

标签:filter,拦截器,request,Filter,相关,response,shiro
来源: https://www.cnblogs.com/ouym/p/14791473.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有