用户登录加载角色到缓存;修改用户角色Key格式

This commit is contained in:
2026-02-02 18:28:33 +08:00
parent 02010bd9ae
commit 6da20bdac4
7 changed files with 42 additions and 7 deletions
@@ -48,11 +48,11 @@ public class RedisKeyConstants {
/** /**
* 构建用户角色数据 KEY * 构建用户角色数据 KEY
* *
* @param phone 用户手机号 * @param userId 用户手机号
* @return 用户角色数据 KEY * @return 用户角色数据 KEY
*/ */
public static String buildUserRolesKey(String phone) { public static String buildUserRolesKey(Long userId) {
return USER_ROLES_KEY_PREFIX + phone; return USER_ROLES_KEY_PREFIX + userId;
} }
/** /**
@@ -3,6 +3,8 @@ package top.crushtj.xiaoyi.auth.domain.mappers;
import top.crushtj.xiaoyi.auth.domain.entity.UserRoleRelEntity; import top.crushtj.xiaoyi.auth.domain.entity.UserRoleRelEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
/** /**
* @author ayi * @author ayi
* @version V1.0 * @version V1.0
@@ -12,5 +14,6 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
*/ */
public interface UserRoleRelMapper extends BaseMapper<UserRoleRelEntity> { public interface UserRoleRelMapper extends BaseMapper<UserRoleRelEntity> {
List<Long> selectByUserId(Long userId);
} }
@@ -10,5 +10,8 @@
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/> <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
<result property="isDeleted" column="is_deleted" jdbcType="BOOLEAN"/> <result property="isDeleted" column="is_deleted" jdbcType="BOOLEAN"/>
</resultMap> </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> </mapper>
@@ -4,7 +4,6 @@ import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@@ -103,6 +102,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
} else { } else {
// 已注册,则获取其用户 ID // 已注册,则获取其用户 ID
userId = userEntity.getId(); userId = userEntity.getId();
this.loadUserRole(userId);
} }
} }
} }
@@ -115,6 +115,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
return Response.success(tokenInfo.tokenValue); return Response.success(tokenInfo.tokenValue);
} }
/**
* 用户注册
*
* @param phone 手机号
* @return 用户ID
*/
private Long registerUser(String phone) { private Long registerUser(String phone) {
return transactionTemplate.execute(status -> { return transactionTemplate.execute(status -> {
try { try {
@@ -150,7 +156,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
// 将用户角色信息存储到 Redis 中 // 将用户角色信息存储到 Redis 中
List<Long> roles = new ArrayList<>(); List<Long> roles = new ArrayList<>();
roles.add(COMMON_USER_ROLE_ID); roles.add(COMMON_USER_ROLE_ID);
String userRolesKey = RedisKeyConstants.buildUserRolesKey(phone); String userRolesKey = RedisKeyConstants.buildUserRolesKey(userId);
redisTemplate.opsForValue() redisTemplate.opsForValue()
.set(userRolesKey, JsonUtils.toJsonString(roles)); .set(userRolesKey, JsonUtils.toJsonString(roles));
return userId; return userId;
@@ -171,5 +177,19 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
return null; 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));
}
}
} }
+4
View File
@@ -14,6 +14,10 @@
<description>网关服务</description> <description>网关服务</description>
<dependencies> <dependencies>
<dependency>
<groupId>top.crushtj</groupId>
<artifactId>xiaoyi-common</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId> <artifactId>spring-cloud-starter-bootstrap</artifactId>
@@ -32,6 +32,7 @@ public class SaTokenConfigure {
.check(r -> StpUtil.checkLogin()) // 校验是否登录 .check(r -> StpUtil.checkLogin()) // 校验是否登录
; ;
SaRouter.match("/auth/user/logout", r -> StpUtil.checkPermission("user"));
// 权限认证 -- 不同模块, 校验不同权限 // 权限认证 -- 不同模块, 校验不同权限
// SaRouter.match("/user/**", r -> StpUtil.checkPermission("user")); // SaRouter.match("/user/**", r -> StpUtil.checkPermission("user"));
// SaRouter.match("/admin/**", r -> StpUtil.checkPermission("admin")); // SaRouter.match("/admin/**", r -> StpUtil.checkPermission("admin"));
@@ -1,6 +1,7 @@
package top.crushtj.xiaoyi.gateway.auth; package top.crushtj.xiaoyi.gateway.auth;
import cn.dev33.satoken.stp.StpInterface; import cn.dev33.satoken.stp.StpInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List; import java.util.List;
@@ -13,15 +14,18 @@ import java.util.List;
* @description 自定义权限验证接口 * @description 自定义权限验证接口
*/ */
@Slf4j
@Component @Component
public class StpInterfaceImpl implements StpInterface { public class StpInterfaceImpl implements StpInterface {
@Override @Override
public List<String> getPermissionList(Object o, String s) { public List<String> getPermissionList(Object loginId, String loginType) {
log.info("## 获取用户权限列表, loginId: {}", loginId);
return List.of(); return List.of();
} }
@Override @Override
public List<String> getRoleList(Object o, String s) { public List<String> getRoleList(Object loginId, String loginType) {
log.info("## 获取用户角色列表, loginId: {}", loginId);
return List.of(); return List.of();
} }
} }