Shrio主要用到两个核心的功能
- 认证
- 权限控制
Springboot不像SSM框架没有xml配置文件,使用Java代码+注解实现这个问题
1.在pom文件添加shrio依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.5.2</version>
</dependency>2.配置Shrio过滤器
这是配置一个shiro过滤器 将所有的请求都交给shiro处理
ShiroConfig应该是加了@Comfiguration注解的,加了这个注解Spring在加载的时候才会根据ShiroConfig启动shrio的配置,@Bean只是将bean交给spring管理,让spring可以使用到这些bean
ShiroFilterFactoryBean初始化这个方法,securityManager传入进去
配置系统需要的拦截器路径,使用filterChainDefinitionMap保存拦截器路径
然后把filterChainDefinitionMap设置到shiroFilterFactoryBean对象里面去
一般正常使用的就是两种过滤器
- anon 匿名可以访问,就是不需要登录
- authc 必须登录才可以访问
然后返回shiroFilterFactoryBean
编写SecurityManager方法初始化DefaultWebSecurityManager对象
然后把这个SecurityManager指定自定义的realm
然后编写自定义的Realm类CustomRealm继承AuthorizingRealm类重写两个方法
doGetAuthenticationInfo 认证的时候执行这个方法 也就是subject.login()这个方法的时候执行doGetAuthorizationInfo 授权的时候执行这个方法doGetAuthenticationInfo然后在这个方法里面查询数据库用户名和密码放到SimpleAuthenticationInfo对象里面,然后shrio去认证,如果出错会抛出异常doGetAuthorizationInfo
根据用户名查询当前用户拥有的角色和权限
然后把它们的角色和权限集合放到simpleAuthorizationInfo对象里面交给shrio去处理
这里shrio底层会和@RequiresRoles和@RequiresPermission注解标识符比较如果一致证明有这个权限就可以访问