Shiro框架简介
Apache Shiro是一个强大且易用的Java安全框架,执行身份认证丶授权丶密码和会话管理。
shiro有6大作用:
- Authentcation 认证===用户登录
- Authorization 授权===用户具有哪些权限
- Cryptography 安全数据加密
- Session Management 会话管理
- Web integration web 系统集成
- Interations 集成其他应用,spring,缓存框架
主要是 认证 和 授权
以用户登录为例-多图参考↓
Spring Boot整合shiro环境搭建
1.创建Spring Boot应用,集成Shiro及相关组件,pom.xml
pom.xml
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2.准备一个sql表
3.配置yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/shirodbtest?account
username: root
password: guohui
thymeleaf:
prefix: classpath:/templates
4.自定义realm
自定义realm首先我们就要写一个realm,而这个realm我们一般要继承AuthorizingRealm类,
因为这个类里面就有实现接收用户认证信息和接收用户权限信息的两个方法,而realm就是用来从数据库查询这些数据的。
下面是我自定义的realm:
//。1 自定义的Realm
public class UserRealm extends AuthorizingRealm {
@Override//授权
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("执行了授权===》doGetAuthorizationInfo"