为什么要用shiro

在介绍spring 集成shiro原理之前,我们首先应该想到的是为什么要用shiro?如果你有此问题,那与我想到一块了。简单来说,shiro是一个安全控制框架,它提供了认证、授权、会话管理、加密等功能,如果不用shiro,那么这些功能都需要自己开发,你肯定得写一个登陆过滤器/拦截器吧,肯定得写授权控制吧,肯定得写会话调度器吧等等,用了shiro就不用写这些了,shiro题你写了,并且规范了这些需求,你只需要扩展它的接口或者压根不需要扩展使用默认的,配置一下就可以实现权限控制功能。既然我们用shiro,就得弄懂它的原理。

概括

shiro是怎么在web项目中发挥的作用,原理是什么?简单来说,在web.xml中配置shiro filter,它会过滤所有请求,然后委派给内部filterchain并结合aop对annotated的方法进行权限控制。

shiroFilter原来是SpringShiroFilter

在web工程web.xml中,都会配置shiroFilter,如下

 <filter><filter-name>shiroFilter</filter-name><filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class></filter><filter-mapping><filter-name>shiroFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>

filter-class是DelegatingFilterProxyDelegatingFilterProxy 的作用,是从spring context中寻找名字为shiroFilter的bean。
再看一下spring配置文件,是ShiroFilterFactoryBean创建了shiroFilter

<!-- 安全认证过滤器 --><bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">//省略其它无关配置</bean>

ShiroFilterFactoryBean 是个FactoryBean,实现了FactoryBean接口,

public class ShiroFilterFactoryBean implements FactoryBean, BeanPostProcessor {public Object getObject() throws Exception {if (instance == null) {instance = createInstance();}return instance;}
}

那么最终是创建了什么对象呢?继续看createInstance()方法,最终返回了SpringShiroFilter对象

protected AbstractShiroFilter createInstance() throws Exception {log.debug("Creating Shiro Filter instance.");SecurityManager securityManager = getSecurityManager();if (securityManager == null) {String msg = "SecurityManager property must be set.";throw new BeanInitializationException(msg);}if (!(securityManager instanceof WebSecurityManager)) {String msg = "The security manager does not implement the WebSecurityManager interface.";throw new BeanInitializationException(msg);}FilterChainManager manager = createFilterChainManager();//Expose the constructed FilterChainManager by first wrapping it in a// FilterChainResolver implementation. The AbstractShiroFilter implementations// do not know about FilterChainManagers - only resolvers:PathMatchingFilterChainResolver chainResolver = new PathMatchingFilterChainResolver();chainResolver.setFilterChainManager(manager);//Now create a concrete ShiroFilter instance and apply the acquired SecurityManager and built//FilterChainResolver.  It doesn't matter that the instance is an anonymous inner class//here - we're just using it because it is a concrete AbstractShiroFilter instance that accepts//injection of the SecurityManager and FilterChainResolver:return new SpringShiroFilter((WebSecurityManager) securityManager, chainResolver);}

至此,在web项目中集成shiro的大致过程就清晰了。

  • web.xml中配置shiroFilter,拦截所有请求,DelegatingFilterProxy去spring配置文件中寻找shiroFilter
  • spring配置文件中ShiroFilterFactoryBean 创建shiroFilter

spring集成shiro原理相关推荐

  1. spring集成shiro详解

    最近项目中要用到shiro作为权限管理,以前都是用自定义的或者spring security,所以就开始看了一些网上的文章,但是感觉都写得很零散.而且大多数都只是给了几行代码,我们得项目相对比较复杂, ...

  2. Spring集成Shiro框架实战

    文章目录 一:什么是Shiro框架 二:Shiro框架简介 1.Shiro基础功能点介绍 2.Shiro的工作原理 3.Shiro的内部工作结构 4.Shiro的身份认证流程 三:Spring集成Sh ...

  3. Shiro学习总结(10)——Spring集成Shiro

    1.引入Shiro的Maven依赖 [html] view plaincopy <!-- Spring 整合Shiro需要的依赖 --> <dependency> <gr ...

  4. Spring Boot 极简集成 Shiro

    点击关注公众号,Java干货及时送达 1. 前言 Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证,授权,加密,和会话管理. Shiro有三大核心组件: Subject: ...

  5. spring boot 2.0 集成shiro注意事项

    2019独角兽企业重金招聘Python工程师标准>>> spring boot 2.0 全面拥抱java8,在安全验证上面 很大程度的简化了配置项,用shiro就要把security ...

  6. Spring Boot + Shiro 集成

    2019独角兽企业重金招聘Python工程师标准>>> Spring Boot + Shiro 集成 Shiro 是一个流行的 Java 安全框架. 其实 Spring 有一个 Sp ...

  7. Shiro与Spring集成时,Shiro权限注解@RequiresRoles等不生效的解决方案

    2019独角兽企业重金招聘Python工程师标准>>> Shiro与Spring集成时,Shiro权限注解@RequiresRoles等不生效, 这个问题着实整了好久,网上各种解决方 ...

  8. 解决Spring Boot集成Shiro,配置类使用Autowired无法注入Bean问题

    为什么80%的码农都做不了架构师?>>>    如题,最近使用spring boot集成shiro,在shiroFilter要使用数据库动态给URL赋权限的时候,发现 @Autowi ...

  9. Spring +mybatisplus+shiro权限管理集成整合

    一.Apache Shiro是一个功能强大.灵活的,开源的安全框架.它可以干净利落地处理身份验证.授权.企业会话管理和加密. Shiro能做什么呢? 验证用户身份 用户访问权限控制,比如:1.判断用户 ...

  10. 有手就行的 Spring Boot 集成 Shiro

    前言   Apache Shiro 是 Java 的一个安全框架.目前,使用 Apache Shiro 的人越来越多,因为它相当简单,对比 Spring Security,可能没有 Spring Se ...

最新文章

  1. linux i2c子系统看不懂啊,Linux 下的I2C子系统
  2. python好吗-Python现在就业前景好吗?
  3. PHP_Memcache函数详解
  4. 又踩.NET Core的坑:在同步方法中调用异步方法Wait时发生死锁(deadlock)
  5. Mac终端神器zsh
  6. R语言统计分布及模拟
  7. ★LeetCode(669)——修剪二叉搜索树(JavaScript)
  8. C# 根据空格数截取
  9. 光学相干断层成像术(OCT)
  10. Xmind模板文档分享——生活计划(3)
  11. BAT解密:互联网技术发展之路(2)- 业务如何驱动技术发展
  12. c语言-基本计算 pm2.5,pm2.5标准
  13. 为什么顶级程序员都有超强逆商?顶级程序员马化腾在艰难的时候,是如何度过的?
  14. 手写vue3源码——reactive, effect ,scheduler, stop 等
  15. 项目管理知识体系指南 (八)
  16. win10 更新Node JS和npm
  17. 一次性将多个网址收藏到谷歌书签的文件夹中
  18. VS工程文件常见后缀名文件含义
  19. 【树】二叉树的两种非递归遍历方法
  20. 传统商圈陷入困境,构建商圈新零售平台成转型趋势

热门文章

  1. Django1.9重写用户模型报错has no attribute 'USERNAME_FIELD'
  2. 微信获取openid的时候报40163错的原因
  3. 在word文档中添加上角标和下角标
  4. Python简单换脸程序
  5. WPS实现公式居中同时编号对齐
  6. html5中ul什么意思,css代码中的ul和li是什么意思
  7. 湖北武汉劳务员证书劳务人员实名制管理的策略建筑七大员培训
  8. STM32F4使用硬件SPI驱动ADS8322
  9. 大数据监控大屏系统,有这些demo就足够了!
  10. passing ‘const XXX’ as ‘this’ argument discards qualifiers