一、创建springboot工程,导入pom文件依赖
<?xml version="1.0" encoding="UTF-8"?>
< project xmlns = " http://maven.apache.org/POM/4.0.0" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0.0</ modelVersion>
< parent>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-parent</ artifactId>
< version> 2.5.4</ version>
< relativePath/>
</ parent>
< groupId> com.zjhc</ groupId>
< artifactId> shiro-all</ artifactId>
< version> 0.0.1-SNAPSHOT</ version>
< packaging> war</ packaging>
< name> shiro-all</ name>
< description> Demo project for Spring Boot</ description>
< properties>
< java.version> 1.8</ java.version>
</ properties>
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-data-redis</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-tomcat</ artifactId>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
< scope> test</ scope>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< optional> true</ optional>
</ dependency>
< dependency>
< groupId> org.apache.shiro</ groupId>
< artifactId> shiro-spring</ artifactId>
< version> 1.7.1</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< scope> 5.1.47</ scope>
</ dependency>
< dependency>
< groupId> com.baomidou</ groupId>
< artifactId> mybatis-plus-boot-starter</ artifactId>
< version> 3.2.0</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.1.23</ version>
</ dependency>
< dependency>
< groupId> cn.hutool</ groupId>
< artifactId> hutool-all</ artifactId>
< version> 5.7.7</ version>
</ dependency>
< dependency>
< groupId> org.thymeleaf.extras</ groupId>
< artifactId> thymeleaf-extras-java8time</ artifactId>
</ dependency>
< dependency>
< groupId> org.thymeleaf</ groupId>
< artifactId> thymeleaf-spring5</ artifactId>
</ dependency>
< dependency>
< groupId> com.github.theborakompanioni</ groupId>
< artifactId> thymeleaf-extras-shiro</ artifactId>
< version> 2.0.0</ version>
</ dependency>
< dependency>
< groupId> org.crazycake</ groupId>
< artifactId> shiro-redis</ artifactId>
< version> 3.3.1</ version>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-validation</ artifactId>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< scope> test</ scope>
</ dependency>
</ dependencies>
< build>
< plugins>
< plugin>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-maven-plugin</ artifactId>
< configuration>
< excludes>
< exclude>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
</ exclude>
</ excludes>
</ configuration>
</ plugin>
</ plugins>
</ build>
</ project>
二、yml文件配置
server:
port: 6677
spring:
datasource:
url: jdbc: mysql: / / 12.41 .106 .140 : 43306 / shiro_db? useUnicode= true & useSSL= false & characterEncoding= utf8& serverTimezone= UTC
username: root
password: rnny@123456
driver- class - name: com. mysql. cj. jdbc. Driver
thymeleaf:
cache: false
redis:
host: 12.41 .106 .140
port: 16379
password: rnny@123456
jedis:
pool:
min- idle: 8
max- wait: 10000
max- active: 2000
max- idle: 500
mybatis- plus:
mapper- locations: classpath* : / mapper
三、根据MP插件生成mapper,entity,service,impl,mapper.xml
3.1 User类
1. User实体
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName ( value = "user_info" )
public class User implements Serializable {
@TableId ( value = "uid" , type = IdType . AUTO)
private Integer uid;
@TableField ( value = "username" )
private String username;
@TableField ( value = "password" )
private String password;
@TableField ( value = "name" )
private String name;
@TableField ( value = "phone" )
private String phone;
@TableField ( value = "idcard" )
private String idcard;
@TableField ( value = "state" )
private String state;
}
2. UserService
public interface UserService extends IService < User > {
}
3. UserServiceImpl
@Service
public class UserServiceImpl extends ServiceImpl < UserMapper , User > implements UserService {
}
4. UserMapper
public interface UserMapper extends BaseMapper < User > {
User getUserByUsername ( String username) ;
int insert ( User user) ;
int del ( @Param ( "username" ) String username) ;
}
5. UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.zjhc.mapper.UserMapper" >
< resultMap id = " BaseResultMap" type = " com.zjhc.entity.User" >
< id column = " uid" jdbcType = " INTEGER" property = " uid" />
< result column = " username" jdbcType = " VARCHAR" property = " username" />
< result column = " password" jdbcType = " VARCHAR" property = " password" />
< result column = " name" jdbcType = " VARCHAR" property = " name" />
< result column = " phone" jdbcType = " VARCHAR" property = " phone" />
< result column = " idcard" jdbcType = " VARCHAR" property = " idcard" />
< result column = " state" jdbcType = " CHAR" property = " state" />
</ resultMap>
< sql id = " Base_Column_List" >
`uid`, username, `password`, `name`, phone, idcard, `state`
</ sql>
< select id = " getUserByUsername" resultMap = " BaseResultMap" >
select * from user_info where username = #{username}
</ select>
< insert id = " insert" parameterType = " com.zjhc.entity.User" >
< selectKey resultType = " java.lang.Integer" keyProperty = " uid" order = " AFTER" >
select last_insert_id()
</ selectKey>
insert into user_info
< trim prefix = " (" suffix = " )" suffixOverrides = " ," >
< if test = " uid != null" >
uid,
</ if>
< if test = " username != null and username != ' ' " >
username,
</ if>
< if test = " password != null and password != ' ' " >
password,
</ if>
< if test = " name != null and name != ' ' " >
name,
</ if>
< if test = " phone != null and phone != ' ' " >
phone,
</ if>
< if test = " idcard != null and idcard != ' ' " >
idcard,
</ if>
< if test = " state != null and state != ' ' " >
state
</ if>
</ trim>
< trim prefix = " values(" suffix = " )" suffixOverrides = " ," >
< if test = " uid != null" >
#{uid},
</ if>
< if test = " username != null and username != ' ' " >
#{username},
</ if>
< if test = " password != null and password != ' ' " >
#{password},
</ if>
< if test = " name != null and name != ' ' " >
#{name},
</ if>
< if test = " phone != null and phone != ' ' " >
#{phone},
</ if>
< if test = " idcard != null and idcard != ' ' " >
#{idcard},
</ if>
< if test = " state != null and state != ' ' " >
#{state}
</ if>
</ trim>
</ insert>
< delete id = " del" >
delete from user_info where username=#{username}
</ delete>
</ mapper>
3.2 Role类
1. Role实体
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName ( value = "sys_role" )
public class Role {
@TableId ( value = "role_id" , type = IdType . AUTO)
private Integer roleId;
@TableField ( value = "role_name" )
private String roleName;
@TableField ( value = "avaliable" )
private String avaliable;
@TableField ( value = "description" )
private String description;
}
2. RoleService
public interface RoleService extends IService < Role > {
}
3. RoleServiceImpl
@Service
public class RoleServiceImpl extends ServiceImpl < RoleMapper , Role > implements RoleService {
}
4. RoleMapper
public interface RoleMapper extends BaseMapper < Role > {
Set < Role > findRolesByUserId ( @Param ( "uid" ) Integer uid) ;
void delPermission ( int i, int i1) ;
void addPermission ( int i, int i1) ;
}
5. RoleMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.zjhc.mapper.RoleMapper" >
< resultMap id = " BaseResultMap" type = " com.zjhc.entity.Role" >
< id column = " role_id" jdbcType = " INTEGER" property = " roleId" />
< result column = " role_name" jdbcType = " VARCHAR" property = " roleName" />
< result column = " avaliable" jdbcType = " CHAR" property = " avaliable" />
< result column = " description" jdbcType = " VARCHAR" property = " description" />
</ resultMap>
< sql id = " Base_Column_List" >
role_id, role_name, avaliable, description
</ sql>
< select id = " findRolesByUserId" resultMap = " BaseResultMap" resultSets = " java.util.Set" resultType = " com.zjhc.entity.Role" >
SELECT r.* from sys_role r LEFT JOIN sys_user_role ur on r.role_id = ur.rid where ur.uid = #{uid}
</ select>
< delete id = " delPermission" >
delete from sys_role_permission where role_id=1 and permission_id=3
</ delete>
< insert id = " addPermission" >
insert into sys_role_permission(role_id, permission_id) values(1,3)
</ insert>
</ mapper>
3.3 Permissionlei
1. Permission实体
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName ( value = "sys_permission" )
public class Permission {
@TableId ( value = "id" , type = IdType . AUTO)
private Integer id;
@TableField ( value = "parent_id" )
private Integer parentId;
@TableField ( value = "parent_ids" )
private String parentIds;
@TableField ( value = "permission_Code" )
private String permissionCode;
@TableField ( value = "permission_Name" )
private String permissionName;
@TableField ( value = "resource_type" )
private String resourceType;
@TableField ( value = "url" )
private String url;
@TableField ( value = "avaliable" )
private String avaliable;
}
2.PermissionService
public interface PermissionService extends IService < Permission > {
}
3.PermissionServiceImpl
@Service
public class PermissionServiceImpl extends ServiceImpl < PermissionMapper , Permission > implements PermissionService {
}
4.PermissionMapper
public interface PermissionMapper extends BaseMapper < Permission > {
Set < Permission > findPermissionsByRoleId ( @Param ( "roles" ) Set < Role > roles) ;
}
5.PermissionMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<! DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace = " com.zjhc.mapper.PermissionMapper" >
< resultMap id = " BaseResultMap" type = " com.zjhc.entity.Permission" >
< id column = " id" jdbcType = " INTEGER" property = " id" />
< result column = " parent_id" jdbcType = " INTEGER" property = " parentId" />
< result column = " parent_ids" jdbcType = " VARCHAR" property = " parentIds" />
< result column = " permission_Code" jdbcType = " VARCHAR" property = " permissionCode" />
< result column = " permission_Name" jdbcType = " VARCHAR" property = " permissionName" />
< result column = " resource_type" jdbcType = " VARCHAR" property = " resourceType" />
< result column = " url" jdbcType = " VARCHAR" property = " url" />
< result column = " avaliable" jdbcType = " CHAR" property = " avaliable" />
</ resultMap>
< sql id = " Base_Column_List" >
id, parent_id, parent_ids, permission_Code, permission_Name, resource_type, url,
avaliable
</ sql>
< select id = " findPermissionsByRoleId" resultMap = " BaseResultMap" resultSets = " java.util.Set" resultType = " com.zjhc.entity.Permission" >
SELECT p.* from sys_permission p LEFT JOIN sys_role_permission rp on p.id = rp.permission_id WHERE rp.role_id IN
< foreach collection = " roles" index = " index" item = " item" open = " (" close = " )" separator = " ," >
#{item.roleId}
</ foreach>
</ select>
</ mapper>
四、创建UserRealm
4.1 UserRealm
1. UserRealm实现
@Slf4j
public class UserRealm extends AuthorizingRealm {
@Lazy
@Autowired
UserMapper userMapper;
@Lazy
@Autowired
RoleMapper roleMapper;
@Lazy
@Autowired
PermissionMapper permissionMapper;
public String getName ( ) {
return "UserRealm" ;
}
@Override
protected AuthorizationInfo doGetAuthorizationInfo ( PrincipalCollection principalCollection) {
log. info ( "=====================enter method UserRealm-doGetAuthorizationInfo获取角色权限" ) ;
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo ( ) ;
Subject subject = SecurityUtils . getSubject ( ) ;
User user = ( User ) subject. getPrincipal ( ) ;
Set < Role > roles = this . roleMapper. findRolesByUserId ( user. getUid ( ) ) ;
for ( Role role : roles) {
info. addRole ( role. getRoleName ( ) ) ;
}
Set < Permission > permissions = this . permissionMapper. findPermissionsByRoleId ( roles) ;
for ( Permission permission : permissions) {
info. addStringPermission ( permission. getPermissionCode ( ) ) ;
}
return info;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo ( AuthenticationToken authenticationToken) throws AuthenticationException {
log. info ( "=====================enter method UserRealm-doGetAuthenticationInfo进行登陆验证" ) ;
UsernamePasswordToken userToken = ( UsernamePasswordToken ) authenticationToken;
User user = userMapper. getUserByUsername ( userToken. getUsername ( ) ) ;
if ( null == user) {
throw new UnknownAccountException ( "账号不存在,请重试" ) ;
}
if ( "1" . equals ( user. getState ( ) ) ) {
throw new LockedAccountException ( "账户已被锁定" ) ;
}
return new SimpleAuthenticationInfo ( user, user. getPassword ( ) , new MyByteSource ( "!QAZ@WSX$RFV" ) , getName ( ) ) ;
}
}
五、整合Redis重写Cache,CacheManager,SessionDAO