`
suichangkele
  • 浏览: 192806 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

shiro学习16-shiro提供的filter-OncePerRequestFilter

阅读更多

从这一节开始我们介绍shiro提供的filter,记住尽管这是shiro提供的,但是不一定适合你的项目,我们看他们源码的目的是理解其中的运行逻辑,在开发中不一定强制要求使用他们提供的filter。

我们从OnecPerRequestFilter,虽然这个类不是一个最顶层的类,他还有父类NameableFilter,但是NameableFilter我在开发中没有使用它提供的名字的便利性,而是根据在spring中配置的benaName决定filter的名字,所以直接跳过NameableFilter。OnecPerRequestFilter这个类是所有的其他的filter的父类,所有的filter的doFilter方法都是调用的这个类中的doFIlter方法,所以必须好好看这个类的doFilter方法,

 

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);//如果当前的filter已经执行过则直接放行不再重复执行。
        } else //noinspection deprecation
            if (/* added in 1.2: */ !isEnabled(request, response) ||
                /* retain backwards compatibility: */ shouldNotFilter(request) ) {//根据enabled判断是否要执行这个filter,如果不执行,则直接放行
            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 {
 
                doFilterInternal(request, response, filterChain);//执行doFilterInternal方法,所以实现类只要复写这个方法即可,这个方法才是这个filter里面真正要实现的业务逻辑。  
         } finally {
                // Once the request has finished, we're done and we don't
                // need to mark as 'already filtered' any more.
                request.removeAttribute(alreadyFilteredAttributeName);
            }
        }
}

  

从源码中可以看出,我们自己的业务逻辑要在doFilterInternal这个方法中实现,所以实现类只要实现这个方法即可,在这个类中这个方法也是一个抽象方法。

 

 

 

分享到:
评论

相关推荐

    shiro(shiro-all-1.8.0.jar)

    shiro(shiro-all-1.8.0.jar)

    shiro-core-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-core-1.4.0.jar; 赠送原API文档:shiro-core-1.4.0-javadoc.jar; 赠送源代码:shiro-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-core-1.4.0.pom; 包含翻译后的API文档:shiro-core...

    shiro-config-core-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-config-core-1.4.0.jar; 赠送原API文档:shiro-config-core-1.4.0-javadoc.jar; 赠送源代码:shiro-config-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-config-core-1.4.0.pom; ...

    shiro-ehcache-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-ehcache-1.4.0.jar; 赠送原API文档:shiro-ehcache-1.4.0-javadoc.jar; 赠送源代码:shiro-ehcache-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-ehcache-1.4.0.pom; 包含翻译后的API文档...

    shiro-jar-1.7.0.zip相关资源包

    解决:升級1.7后附件...shiro-cas-1.7.0.jar shiro-core-1.7.0.jar shiro-ehcache-1.7.0.jar shiro-spring-1.7.0.jar shiro-web-1.7.0.jar CustomShiroFilterFactoryBean.java spring-context-shiro.xml 修改说明.txt

    shiro-crypto-cipher-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-crypto-cipher-1.4.0.jar; 赠送原API文档:shiro-crypto-cipher-1.4.0-javadoc.jar; 赠送源代码:shiro-crypto-cipher-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-crypto-cipher-1.4.0....

    shiro-crypto-core-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-crypto-core-1.4.0.jar; 赠送原API文档:shiro-crypto-core-1.4.0-javadoc.jar; 赠送源代码:shiro-crypto-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-crypto-core-1.4.0.pom; ...

    shiro-crypto-core-1.4.0-API文档-中英对照版.zip

    赠送jar包:shiro-crypto-core-1.4.0.jar; 赠送原API文档:shiro-crypto-core-1.4.0-javadoc.jar; 赠送源代码:shiro-crypto-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-crypto-core-1.4.0.pom; ...

    shiro-config-core-1.4.0-API文档-中英对照版.zip

    赠送jar包:shiro-config-core-1.4.0.jar; 赠送原API文档:shiro-config-core-1.4.0-javadoc.jar; 赠送源代码:shiro-config-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-config-core-1.4.0.pom; ...

    shiro-core-1.7.1 jar

    shiro shiro-core-1.7.1 jar shiro漏洞

    shiro-config-ogdl-1.4.0-API文档-中英对照版.zip

    赠送jar包:shiro-config-ogdl-1.4.0.jar; 赠送原API文档:shiro-config-ogdl-1.4.0-javadoc.jar; 赠送源代码:shiro-config-ogdl-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-config-ogdl-1.4.0.pom; ...

    shiro-cas-1.2.3-API文档-中文版.zip

    赠送jar包:shiro-cas-1.2.3.jar; 赠送原API文档:shiro-cas-1.2.3-javadoc.jar; 赠送源代码:shiro-cas-1.2.3-sources.jar; 赠送Maven依赖信息文件:shiro-cas-1.2.3.pom; 包含翻译后的API文档:shiro-cas-...

    shiro-cas-1.2.3-API文档-中英对照版.zip

    赠送jar包:shiro-cas-1.2.3.jar; 赠送原API文档:shiro-cas-1.2.3-javadoc.jar; 赠送源代码:shiro-cas-1.2.3-sources.jar; 赠送Maven依赖信息文件:shiro-cas-1.2.3.pom; 包含翻译后的API文档:shiro-cas-...

    shiro-attack-4.7.0-SNAPSHOT-all.zip

    shiro_attack-4.7.0-SNAPSHOT-all.zip 序列化验证工具

    shiro-cache-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-cache-1.4.0.jar; 赠送原API文档:shiro-cache-1.4.0-javadoc.jar; 赠送源代码:shiro-cache-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-cache-1.4.0.pom; 包含翻译后的API文档:shiro-...

    shiro-cache-1.4.0-API文档-中英对照版.zip

    赠送jar包:shiro-cache-1.4.0.jar; 赠送原API文档:shiro-cache-1.4.0-javadoc.jar; 赠送源代码:shiro-cache-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-cache-1.4.0.pom; 包含翻译后的API文档:shiro-...

    shiro-all-1.3.2.jar

    shiro-all-1.3.2.jar

    shiro-event-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-event-1.4.0.jar; 赠送原API文档:shiro-event-1.4.0-javadoc.jar; 赠送源代码:shiro-event-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-event-1.4.0.pom; 包含翻译后的API文档:shiro-...

    shiro-root-1.4.1-source-release.zip

    给广大开发爱好者一个开发框架:shiro-root-1.4.1-source-release.zip

    shiro-jar.zip

    shiro-all-1.7.1.jar,shiro-aspectj-1.7.1.jar,shiro-cache-1.7.1.jar,shiro-config-core-1.7.1.jar,shiro-config-ogdl-1.7.1.jar,shiro-core-1.7.1.jar,shiro-crypto-cipher-1.7.1.jar,shiro-crypto-core-1.7.1.jar...

Global site tag (gtag.js) - Google Analytics