用户登录加载角色到缓存;修改用户角色Key格式
This commit is contained in:
@@ -48,11 +48,11 @@ public class RedisKeyConstants {
|
||||
/**
|
||||
* 构建用户角色数据 KEY
|
||||
*
|
||||
* @param phone 用户手机号
|
||||
* @param userId 用户手机号
|
||||
* @return 用户角色数据 KEY
|
||||
*/
|
||||
public static String buildUserRolesKey(String phone) {
|
||||
return USER_ROLES_KEY_PREFIX + phone;
|
||||
public static String buildUserRolesKey(Long userId) {
|
||||
return USER_ROLES_KEY_PREFIX + userId;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+3
@@ -3,6 +3,8 @@ package top.crushtj.xiaoyi.auth.domain.mappers;
|
||||
import top.crushtj.xiaoyi.auth.domain.entity.UserRoleRelEntity;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ayi
|
||||
* @version V1.0
|
||||
@@ -12,5 +14,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
*/
|
||||
public interface UserRoleRelMapper extends BaseMapper<UserRoleRelEntity> {
|
||||
|
||||
List<Long> selectByUserId(Long userId);
|
||||
}
|
||||
|
||||
|
||||
+3
@@ -10,5 +10,8 @@
|
||||
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="isDeleted" column="is_deleted" jdbcType="BOOLEAN"/>
|
||||
</resultMap>
|
||||
<select id="selectByUserId" resultType="java.lang.Long">
|
||||
SELECT role_id FROM t_user_role_rel WHERE user_id = #{userId} AND is_deleted = 0
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
||||
+22
-2
@@ -4,7 +4,6 @@ import cn.dev33.satoken.stp.SaTokenInfo;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -103,6 +102,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
|
||||
} else {
|
||||
// 已注册,则获取其用户 ID
|
||||
userId = userEntity.getId();
|
||||
this.loadUserRole(userId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -115,6 +115,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
|
||||
return Response.success(tokenInfo.tokenValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
*
|
||||
* @param phone 手机号
|
||||
* @return 用户ID
|
||||
*/
|
||||
private Long registerUser(String phone) {
|
||||
return transactionTemplate.execute(status -> {
|
||||
try {
|
||||
@@ -150,7 +156,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
|
||||
// 将用户角色信息存储到 Redis 中
|
||||
List<Long> roles = new ArrayList<>();
|
||||
roles.add(COMMON_USER_ROLE_ID);
|
||||
String userRolesKey = RedisKeyConstants.buildUserRolesKey(phone);
|
||||
String userRolesKey = RedisKeyConstants.buildUserRolesKey(userId);
|
||||
redisTemplate.opsForValue()
|
||||
.set(userRolesKey, JsonUtils.toJsonString(roles));
|
||||
return userId;
|
||||
@@ -171,5 +177,19 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载用户角色信息到缓存
|
||||
*
|
||||
* @param userId 用户ID
|
||||
*/
|
||||
private void loadUserRole(Long userId) {
|
||||
String userRolesKey = RedisKeyConstants.buildUserRolesKey(userId);
|
||||
Boolean hasKey = redisTemplate.hasKey(userRolesKey);
|
||||
if (!hasKey) {
|
||||
List<Long> roles = userRoleRelMapper.selectByUserId(userId);
|
||||
redisTemplate.opsForValue().set(userRolesKey,JsonUtils.toJsonString(roles));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user