springboot整合Shiro安全框架实现权限控制
当使用shiro安全框架验证是否有权限时,需要创建5张表,分别为:
数据库表:
user
:用户表
user_role
:用户角色中间表
role
:角色表
role_permission
:角色权限中间表
permission
:权限表
实体类:user
permission
import lombok.Data;
@Data
public class User {
private Integer id;
private String userName;
private String password;
}
import lombok.Data;
@Data
public class Permission {
private Integer id;
private String permissionName;//权限名
}
List<Permission> permissionList=findPermissionByUserName(username)
五表联查,通过用户名查出该用户所具有的权限。
1.引入依赖
<!--安全框架shiro-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
2.自定义验证规则 MyRealm
package com.qf.realm;
import com.qf.dao.PermissionMapper;
import com.qf.dao.UserMapper;
import com.qf.pojo.Permission;
import com.qf.pojo.User;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework