SaToken接口鉴权
This commit is contained in:
@@ -58,11 +58,11 @@ public class RedisKeyConstants {
|
||||
/**
|
||||
* 构建角色权限数据 KEY
|
||||
*
|
||||
* @param roleId 角色 ID
|
||||
* @param roleKey 角色 ID
|
||||
* @return 角色权限数据 KEY
|
||||
*/
|
||||
public static String buildRolePermissionsKey(Long roleId) {
|
||||
return ROLE_PERMISSIONS_KEY_PREFIX + roleId;
|
||||
public static String buildRolePermissionsKey(String roleKey) {
|
||||
return ROLE_PERMISSIONS_KEY_PREFIX + roleKey;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,5 +21,7 @@ public interface RoleMapper extends BaseMapper<RoleEntity> {
|
||||
*/
|
||||
List<RoleEntity> selectEnabledRoleList();
|
||||
|
||||
List<String> selectRoleKeyByUserId(Long userId);
|
||||
|
||||
}
|
||||
|
||||
|
||||
+1
-4
@@ -1,9 +1,7 @@
|
||||
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;
|
||||
import top.crushtj.xiaoyi.auth.domain.entity.UserRoleRelEntity;
|
||||
|
||||
/**
|
||||
* @author ayi
|
||||
@@ -14,6 +12,5 @@ import java.util.List;
|
||||
*/
|
||||
public interface UserRoleRelMapper extends BaseMapper<UserRoleRelEntity> {
|
||||
|
||||
List<Long> selectByUserId(Long userId);
|
||||
}
|
||||
|
||||
|
||||
@@ -20,4 +20,10 @@
|
||||
WHERE status = 1
|
||||
AND is_deleted = 0
|
||||
</select>
|
||||
<select id="selectRoleKeyByUserId" resultType="java.lang.String">
|
||||
SELECT r.role_key
|
||||
FROM t_role r
|
||||
LEFT JOIN t_user_role_rel urr ON r.id = urr.role_id
|
||||
WHERE urr.user_id = #{userId}
|
||||
</select>
|
||||
</mapper>
|
||||
|
||||
-3
@@ -10,8 +10,5 @@
|
||||
<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>
|
||||
|
||||
+6
-6
@@ -88,27 +88,27 @@ public class PushRolePermissions2RedisRunner implements ApplicationRunner {
|
||||
.collect(Collectors.toMap(PermissionEntity::getId, permissionEntity -> permissionEntity));
|
||||
|
||||
// 构建角色 ID 和权限 DO 列表的映射关系
|
||||
Map<Long, List<PermissionEntity>> roleIdPermissionMap = new HashMap<>();
|
||||
Map<String, List<String>> roleIdPermissionMap = new HashMap<>();
|
||||
roleList.forEach(role -> {
|
||||
Long roleId = role.getId();
|
||||
//获取角色对应的权限id列表
|
||||
List<Long> permissionIds = roleIdPermissionIdsMap.get(roleId);
|
||||
if (CollUtil.isNotEmpty(permissionIds)) {
|
||||
List<PermissionEntity> perDOS = Lists.newArrayList();
|
||||
List<String> permissionKeys = Lists.newArrayList();
|
||||
permissionIds.forEach(permissionId -> {
|
||||
// 根据权限 ID 获取具体的权限 DO 对象
|
||||
PermissionEntity permissionDO = permissionIdEntityMap.get(permissionId);
|
||||
if (Objects.nonNull(permissionDO)) {
|
||||
perDOS.add(permissionDO);
|
||||
permissionKeys.add(permissionDO.getPermissionKey());
|
||||
}
|
||||
});
|
||||
roleIdPermissionMap.put(roleId, perDOS);
|
||||
roleIdPermissionMap.put(role.getRoleKey(), permissionKeys);
|
||||
}
|
||||
});
|
||||
|
||||
// 将角色 ID 和权限 DO 列表的映射关系写入 Redis
|
||||
roleIdPermissionMap.forEach((roleId, permission) -> {
|
||||
String key = RedisKeyConstants.buildRolePermissionsKey(roleId);
|
||||
roleIdPermissionMap.forEach((roleKey, permission) -> {
|
||||
String key = RedisKeyConstants.buildRolePermissionsKey(roleKey);
|
||||
redisTemplate.opsForValue()
|
||||
.set(key, JsonUtils.toJsonString(permission));
|
||||
});
|
||||
|
||||
+13
-5
@@ -18,8 +18,10 @@ import top.crushtj.framework.common.utils.IdGenerator;
|
||||
import top.crushtj.framework.common.utils.JsonUtils;
|
||||
import top.crushtj.framework.common.utils.MaskUtils;
|
||||
import top.crushtj.xiaoyi.auth.constant.RedisKeyConstants;
|
||||
import top.crushtj.xiaoyi.auth.domain.entity.RoleEntity;
|
||||
import top.crushtj.xiaoyi.auth.domain.entity.UserEntity;
|
||||
import top.crushtj.xiaoyi.auth.domain.entity.UserRoleRelEntity;
|
||||
import top.crushtj.xiaoyi.auth.domain.mappers.RoleMapper;
|
||||
import top.crushtj.xiaoyi.auth.domain.mappers.UserMapper;
|
||||
import top.crushtj.xiaoyi.auth.domain.mappers.UserRoleRelMapper;
|
||||
import top.crushtj.xiaoyi.auth.enums.LoginTypeEnum;
|
||||
@@ -55,6 +57,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
|
||||
@Resource
|
||||
private UserRoleRelMapper userRoleRelMapper;
|
||||
|
||||
@Resource
|
||||
private RoleMapper roleMapper;
|
||||
|
||||
@Resource
|
||||
private TransactionTemplate transactionTemplate;
|
||||
|
||||
@@ -154,8 +159,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
|
||||
userRoleRelMapper.insert(userRoleRel);
|
||||
|
||||
// 将用户角色信息存储到 Redis 中
|
||||
List<Long> roles = new ArrayList<>();
|
||||
roles.add(COMMON_USER_ROLE_ID);
|
||||
RoleEntity role = roleMapper.selectById(COMMON_USER_ROLE_ID);
|
||||
List<String> roles = new ArrayList<>(1);
|
||||
roles.add(role.getRoleKey());
|
||||
String userRolesKey = RedisKeyConstants.buildUserRolesKey(userId);
|
||||
redisTemplate.opsForValue()
|
||||
.set(userRolesKey, JsonUtils.toJsonString(roles));
|
||||
@@ -167,7 +173,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
|
||||
// 回滚 redis 自增ID
|
||||
Long decrement = Long.valueOf(Objects.requireNonNull(redisTemplate.opsForValue()
|
||||
.get(XIAOYI_ID_GENERATOR_KEY))
|
||||
.toString().trim());
|
||||
.toString()
|
||||
.trim());
|
||||
if (decrement.compareTo(XIAOYI_ID_INITIAL_VALUE) > 0) {
|
||||
log.error("==> 用户注册异常,回滚redis自增ID: {}", decrement);
|
||||
redisTemplate.opsForValue()
|
||||
@@ -187,8 +194,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> impleme
|
||||
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));
|
||||
List<String> roles = roleMapper.selectRoleKeyByUserId(userId);
|
||||
redisTemplate.opsForValue()
|
||||
.set(userRolesKey, JsonUtils.toJsonString(roles));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user