feat: Spring Boot 整合 Spring Cache 和 Redis 缓存

This commit is contained in:
haoxr
2023-12-04 21:39:12 +08:00
parent 0fa99a61ae
commit e28b3e9490
12 changed files with 176 additions and 35 deletions

View File

@@ -0,0 +1,71 @@
package com.youlai.system.config;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* Redis 缓存配置
*
* @author haoxr
* @since 2023/12/4
*/
@EnableCaching
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
public class RedisCacheConfig {
/**
* 自定义 RedisCacheManager
* <p>
* 修改 Redis 序列化方式,默认 JdkSerializationRedisSerializer
*
* @param redisConnectionFactory {@link RedisConnectionFactory}
* @param cacheProperties {@link CacheProperties}
* @return {@link RedisCacheManager}
*/
@Bean
public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory, CacheProperties cacheProperties){
return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration(cacheProperties))
.build();
}
/**
* 自定义 RedisCacheConfiguration
*
* @param cacheProperties {@link CacheProperties}
* @return {@link RedisCacheConfiguration}
*/
@Bean
RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.string()));
config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
CacheProperties.Redis redisProperties = cacheProperties.getRedis();
if (redisProperties.getTimeToLive() != null) {
config = config.entryTtl(redisProperties.getTimeToLive());
}
if (!redisProperties.isCacheNullValues()) {
config = config.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
config = config.disableKeyPrefix();
}
config = config.computePrefixWith(name -> name + ":");//覆盖默认key双冒号 CacheKeyPrefix#prefixed
return config;
}
}

View File

@@ -19,6 +19,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.List;
@Tag(name = "03.角色接口")
@@ -29,7 +30,7 @@ public class SysRoleController {
private final SysRoleService roleService;
@Operation(summary = "角色分页列表" )
@Operation(summary = "角色分页列表")
@GetMapping("/page")
public PageResult<RolePageVO> getRolePage(
@ParameterObject RolePageQuery queryParams
@@ -44,7 +45,7 @@ public class SysRoleController {
List<Option> list = roleService.listRoleOptions();
return Result.success(list);
}
@Operation(summary = "新增角色")
@PostMapping
@PreAuthorize("@ss.hasPerm('sys:role:add')")
@@ -57,7 +58,7 @@ public class SysRoleController {
@Operation(summary = "角色表单数据")
@GetMapping("/{roleId}/form")
public Result<RoleForm> getRoleForm(
@Parameter(description ="角色ID") @PathVariable Long roleId
@Parameter(description = "角色ID") @PathVariable Long roleId
) {
RoleForm roleForm = roleService.getRoleForm(roleId);
return Result.success(roleForm);
@@ -75,7 +76,7 @@ public class SysRoleController {
@DeleteMapping("/{ids}")
@PreAuthorize("@ss.hasPerm('sys:role:delete')")
public Result deleteRoles(
@Parameter(description ="删除角色,多个以英文逗号(,)分割") @PathVariable String ids
@Parameter(description = "删除角色,多个以英文逗号(,)分割") @PathVariable String ids
) {
boolean result = roleService.deleteRoles(ids);
return Result.judge(result);
@@ -84,8 +85,8 @@ public class SysRoleController {
@Operation(summary = "修改角色状态")
@PutMapping(value = "/{roleId}/status")
public Result updateRoleStatus(
@Parameter(description ="角色ID") @PathVariable Long roleId,
@Parameter(description ="状态(1:启用;0:禁用)") @RequestParam Integer status
@Parameter(description = "角色ID") @PathVariable Long roleId,
@Parameter(description = "状态(1:启用;0:禁用)") @RequestParam Integer status
) {
boolean result = roleService.updateRoleStatus(roleId, status);
return Result.judge(result);
@@ -94,19 +95,19 @@ public class SysRoleController {
@Operation(summary = "获取角色的菜单ID集合")
@GetMapping("/{roleId}/menuIds")
public Result<List<Long>> getRoleMenuIds(
@Parameter(description ="角色ID") @PathVariable Long roleId
@Parameter(description = "角色ID") @PathVariable Long roleId
) {
List<Long> menuIds = roleService.getRoleMenuIds(roleId);
return Result.success(menuIds);
}
@Operation(summary = "分配菜单权限给角色")
@Operation(summary = "分配菜单(包括按钮权限)给角色")
@PutMapping("/{roleId}/menus")
public Result updateRoleMenus(
public Result assignMenusToRole(
@PathVariable Long roleId,
@RequestBody List<Long> menuIds
) {
boolean result = roleService.updateRoleMenus(roleId,menuIds);
boolean result = roleService.assignMenusToRole(roleId, menuIds);
return Result.judge(result);
}
}

View File

@@ -5,7 +5,7 @@ import com.youlai.system.model.entity.SysUserRole;
import org.apache.ibatis.annotations.Mapper;
/**
* 用户角色持久
* 用户角色访问
*
* @author haoxr
* @since 2022/1/15
@@ -13,4 +13,10 @@ import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {
/**
* 统计角色下绑定的用户数量
*
* @param roleId 角色ID
*/
int countUsersForRole(Long roleId);
}

View File

@@ -85,7 +85,7 @@ public interface SysRoleService extends IService<SysRole> {
* @param menuIds
* @return
*/
boolean updateRoleMenus(Long roleId, List<Long> menuIds);
boolean assignMenusToRole(Long roleId, List<Long> menuIds);
/**
* 获取最大范围的数据权限

View File

@@ -15,5 +15,13 @@ public interface SysUserRoleService extends IService<SysUserRole> {
* @param roleIds
* @return
*/
boolean saveUserRoles(Long userId, List<Long> roleIds);
boolean saveUserRoles(Long userId, List<Long> roleIds);
/**
* 判断角色是否存在绑定的用户
*
* @param roleId 角色ID
* @return true已分配 false未分配
*/
boolean isRoleAssignedToUser(Long roleId);
}

View File

@@ -10,10 +10,10 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.constant.SystemConstants;
import com.youlai.system.common.model.Option;
import com.youlai.system.converter.RoleConverter;
import com.youlai.system.core.security.service.PermissionService;
import com.youlai.system.mapper.SysRoleMapper;
import com.youlai.system.model.entity.SysRole;
import com.youlai.system.model.entity.SysRoleMenu;
import com.youlai.system.model.entity.SysUserRole;
import com.youlai.system.model.form.RoleForm;
import com.youlai.system.model.query.RolePageQuery;
import com.youlai.system.model.vo.RolePageVO;
@@ -39,9 +39,10 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
private final SysRoleMenuService sysRoleMenuService;
private final SysUserRoleService sysUserRoleService;
private final SysRoleMenuService roleMenuService;
private final SysUserRoleService userRoleService;
private final RoleConverter roleConverter;
private final PermissionService permissionService;
/**
* 角色分页列表
@@ -119,7 +120,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
* 获取角色表单数据
*
* @param roleId 角色ID
* @return {@link RoleForm} 角色表单数据
* @return {@link RoleForm} 角色表单数据
*/
@Override
public RoleForm getRoleForm(Long roleId) {
@@ -136,10 +137,9 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
*/
@Override
public boolean updateRoleStatus(Long roleId, Integer status) {
boolean result = this.update(new LambdaUpdateWrapper<SysRole>()
return this.update(new LambdaUpdateWrapper<SysRole>()
.eq(SysRole::getId, roleId)
.set(SysRole::getStatus, status));
return result;
}
/**
@@ -153,15 +153,23 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
Assert.isTrue(StrUtil.isNotBlank(ids), "删除的角色ID不能为空");
List<Long> roleIds = Arrays.stream(ids.split(","))
.map(Long::parseLong)
.collect(Collectors.toList());
.toList();
roleIds.forEach(id -> {
long count = sysUserRoleService.count(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRoleId, id));
Assert.isTrue(count <= 0, "角色已分配用户,无法删除");
sysRoleMenuService.remove(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, id));
});
for (Long roleId : roleIds) {
SysRole role = this.getById(roleId);
Assert.isTrue(role != null, "角色不存在");
return this.removeByIds(roleIds);
// 判断角色是否被用户关联
boolean isRoleAssigned = userRoleService.isRoleAssignedToUser(roleId);
Assert.isTrue(!isRoleAssigned, "角色【{}】已分配用户,请先解除关联后删除", role.getName());
boolean deleteResult = this.removeById(roleId);
if(deleteResult) {
// 删除成功,刷新权限缓存
permissionService.refreshPermissionCache(role.getCode());
}
}
return true;
}
/**
@@ -172,28 +180,29 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
*/
@Override
public List<Long> getRoleMenuIds(Long roleId) {
return sysRoleMenuService.listMenuIdsByRoleId(roleId);
return roleMenuService.listMenuIdsByRoleId(roleId);
}
/**
* 修改角色的资源权限
*
* @param roleId 角色ID
* @param roleId 角色ID
* @param menuIds 菜单ID集合
* @return {@link Boolean}
*/
@Override
@Transactional
@CacheEvict(cacheNames = "system", key = "'routes'")
public boolean updateRoleMenus(Long roleId, List<Long> menuIds) {
public boolean assignMenusToRole(Long roleId, List<Long> menuIds) {
// 删除角色菜单
sysRoleMenuService.remove(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
roleMenuService.remove(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
// 新增角色菜单
if (CollectionUtil.isNotEmpty(menuIds)) {
List<SysRoleMenu> roleMenus = menuIds.stream()
List<SysRoleMenu> roleMenus = menuIds
.stream()
.map(menuId -> new SysRoleMenu(roleId, menuId))
.collect(Collectors.toList());
sysRoleMenuService.saveBatch(roleMenus);
.toList();
roleMenuService.saveBatch(roleMenus);
}
return true;
}

View File

@@ -32,7 +32,7 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
List<Long> userRoleIds = this.list(new LambdaQueryWrapper<SysUserRole>()
.eq(SysUserRole::getUserId, userId))
.stream()
.map(item -> item.getRoleId())
.map(SysUserRole::getRoleId)
.collect(Collectors.toList());
// 新增用户角色
@@ -67,4 +67,16 @@ public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUs
return true;
}
/**
* 判断角色是否存在绑定的用户
*
* @param roleId 角色ID
* @return true已分配 false未分配
*/
@Override
public boolean isRoleAssignedToUser(Long roleId) {
int count = this.baseMapper.countUsersForRole(roleId);
return count > 0;
}
}