From 4fe14aedddff719c26c6393757886abd36ec193c Mon Sep 17 00:00:00 2001 From: lyd <1419499670@qq.com> Date: Fri, 23 Sep 2022 10:27:18 +0800 Subject: [PATCH] =?UTF-8?q?session=E5=8A=A8=E6=80=81=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../satoken/utils/FlushSessionUtil.java | 42 ++++++++ .../security/service/OnlineUserService.java | 2 - .../security/service/UserCacheClean.java | 96 +++++++++---------- .../system/service/impl/RoleServiceImpl.java | 24 +---- .../system/service/impl/UserServiceImpl.java | 23 +++-- 5 files changed, 106 insertions(+), 81 deletions(-) create mode 100644 nladmin-system/src/main/java/org/nl/modules/security/satoken/utils/FlushSessionUtil.java diff --git a/nladmin-system/src/main/java/org/nl/modules/security/satoken/utils/FlushSessionUtil.java b/nladmin-system/src/main/java/org/nl/modules/security/satoken/utils/FlushSessionUtil.java new file mode 100644 index 000000000..0ae198b8d --- /dev/null +++ b/nladmin-system/src/main/java/org/nl/modules/security/satoken/utils/FlushSessionUtil.java @@ -0,0 +1,42 @@ +package org.nl.modules.security.satoken.utils; + +import cn.dev33.satoken.stp.StpUtil; +import lombok.RequiredArgsConstructor; +import org.nl.modules.system.domain.User; +import org.nl.modules.system.service.RoleService; +import org.nl.modules.system.service.UserService; +import org.nl.modules.system.service.dto.UserDto; +import org.nl.utils.dto.CurrentUser; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @author: lyd + * @description: 当用户信息改变时候,需要刷新session + * @Date: 2022/9/23 + */ +@Component +@RequiredArgsConstructor +public class FlushSessionUtil { + + private final UserService userService; + private final RoleService roleService; + + /** + * 更新session数据 - Session , TokenSession + * @param user + */ + public void flushSessionInfo(User user) { + UserDto userDto = userService.findByName(user.getUsername()); + List permissionList = roleService.getPermissionList(userDto.getId().toString()); + StpUtil.getSession().set("UserDto", userDto); + // 获取权限列表 - 登录查找权限 + CurrentUser user2 = new CurrentUser(); + user2.setId(userDto.getId()); + user2.setUsername(userDto.getUsername()); + user2.setUser(userDto); + user2.setPermissions(permissionList); + StpUtil.getTokenSession().set("userInfo", user); + } +} diff --git a/nladmin-system/src/main/java/org/nl/modules/security/service/OnlineUserService.java b/nladmin-system/src/main/java/org/nl/modules/security/service/OnlineUserService.java index fc749be50..b4441e8fc 100644 --- a/nladmin-system/src/main/java/org/nl/modules/security/service/OnlineUserService.java +++ b/nladmin-system/src/main/java/org/nl/modules/security/service/OnlineUserService.java @@ -106,7 +106,6 @@ public class OnlineUserService { * @param key / */ public void kickOut(String key){ -// key = properties.getOnlineKey() + key; redisUtils.del(key); } @@ -115,7 +114,6 @@ public class OnlineUserService { * @param token / */ public void logout(String token) { -// String key = properties.getOnlineKey() + token; redisUtils.del(token); } diff --git a/nladmin-system/src/main/java/org/nl/modules/security/service/UserCacheClean.java b/nladmin-system/src/main/java/org/nl/modules/security/service/UserCacheClean.java index f5c610e94..28a0d0ce4 100644 --- a/nladmin-system/src/main/java/org/nl/modules/security/service/UserCacheClean.java +++ b/nladmin-system/src/main/java/org/nl/modules/security/service/UserCacheClean.java @@ -1,49 +1,49 @@ -/* - * Copyright 2019-2020 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.nl.modules.security.service; - -import cn.hutool.core.util.StrUtil; -import org.springframework.stereotype.Component; - -/** - * @author: liaojinlong - * @date: 2020/6/11 18:01 - * @apiNote: 用于清理 用户登录信息缓存,为防止Spring循环依赖与安全考虑 ,单独构成工具类 - */ -@Component -public class UserCacheClean { - - /** - * 清理特定用户缓存信息
- * 用户信息变更时 - * - * @param userName / - */ - public void cleanUserCache(String userName) { - if (StrUtil.isNotEmpty(userName)) { -// UserDetailsServiceImpl.userDtoCache.remove(userName); - } - } - - /** - * 清理所有用户的缓存信息
- * ,如发生角色授权信息变化,可以简便的全部失效缓存 - */ -// public void cleanAll() { -// UserDetailsServiceImpl.userDtoCache.clear(); +///* +// * Copyright 2019-2020 the original author or authors. +// * +// * Licensed under the Apache License, Version 2.0 (the "License"); +// * you may not use this file except in compliance with the License. +// * You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0 +// * +// * Unless required by applicable law or agreed to in writing, software +// * distributed under the License is distributed on an "AS IS" BASIS, +// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// * See the License for the specific language governing permissions and +// * limitations under the License. +// */ +// +//package org.nl.modules.security.service; +// +//import cn.hutool.core.util.StrUtil; +//import org.springframework.stereotype.Component; +// +///** +// * @author: liaojinlong +// * @date: 2020/6/11 18:01 +// * @apiNote: 用于清理 用户登录信息缓存,为防止Spring循环依赖与安全考虑 ,单独构成工具类 +// */ +//@Component +//public class UserCacheClean { +// +// /** +// * 清理特定用户缓存信息
+// * 用户信息变更时 +// * +// * @param userName / +// */ +// public void cleanUserCache(String userName) { +// if (StrUtil.isNotEmpty(userName)) { +//// UserDetailsServiceImpl.userDtoCache.remove(userName); +// } // } -} +// +// /** +// * 清理所有用户的缓存信息
+// * ,如发生角色授权信息变化,可以简便的全部失效缓存 +// */ +//// public void cleanAll() { +//// UserDetailsServiceImpl.userDtoCache.clear(); +//// } +//} diff --git a/nladmin-system/src/main/java/org/nl/modules/system/service/impl/RoleServiceImpl.java b/nladmin-system/src/main/java/org/nl/modules/system/service/impl/RoleServiceImpl.java index 7758ae5c7..22e335927 100644 --- a/nladmin-system/src/main/java/org/nl/modules/system/service/impl/RoleServiceImpl.java +++ b/nladmin-system/src/main/java/org/nl/modules/system/service/impl/RoleServiceImpl.java @@ -22,7 +22,6 @@ import com.alibaba.fastjson.JSONObject; import lombok.RequiredArgsConstructor; import org.nl.exception.BadRequestException; import org.nl.exception.EntityExistException; -import org.nl.modules.security.service.UserCacheClean; import org.nl.modules.system.domain.Role; import org.nl.modules.system.domain.User; import org.nl.modules.system.repository.RoleRepository; @@ -63,7 +62,7 @@ public class RoleServiceImpl implements RoleService { private final RoleSmallMapper roleSmallMapper; private final RedisUtils redisUtils; private final UserRepository userRepository; - private final UserCacheClean userCacheClean; +// private final UserCacheClean userCacheClean; @Override public List queryAll() { @@ -162,25 +161,6 @@ public class RoleServiceImpl implements RoleService { return Collections.min(roleDtos.stream().map(RoleDto::getLevel).collect(Collectors.toList())); } -// @Override -// @Cacheable(key = "'auth:' + #p0.id") -// public List mapToGrantedAuthorities(UserDto user) { -// Set permissions = new HashSet<>(); -// // 如果是管理员直接返回 -// if (user.getIsAdmin()) { -// permissions.add("admin"); -//// return permissions.stream().map(SimpleGrantedAuthority::new) -//// .collect(Collectors.toList()); -// } -// Set roles = roleRepository.findByUserId(user.getId()); -// permissions = roles.stream().flatMap(role -> role.getMenus().stream()) -// .filter(menu -> StrUtil.isNotEmpty(menu.getPermission())) -// .map(Menu::getPermission).collect(Collectors.toSet()); -//// return permissions.stream().map(SimpleGrantedAuthority::new) -//// .collect(Collectors.toList()); -// return null; -// } - @Override @Cacheable(key = "'auth:' + #p0") public List getPermissionList(String id) { @@ -234,7 +214,7 @@ public class RoleServiceImpl implements RoleService { public void delCaches(Long id, List users) { users = CollectionUtil.isEmpty(users) ? userRepository.findByRoleId(id) : users; if (CollectionUtil.isNotEmpty(users)) { - users.forEach(item -> userCacheClean.cleanUserCache(item.getUsername())); +// users.forEach(item -> userCacheClean.cleanUserCache(item.getUsername())); Set userIds = users.stream().map(User::getId).collect(Collectors.toSet()); redisUtils.delByKeys(CacheKey.DATA_USER, userIds); redisUtils.delByKeys(CacheKey.MENU_USER, userIds); diff --git a/nladmin-system/src/main/java/org/nl/modules/system/service/impl/UserServiceImpl.java b/nladmin-system/src/main/java/org/nl/modules/system/service/impl/UserServiceImpl.java index 3cc25208e..6435f7af9 100644 --- a/nladmin-system/src/main/java/org/nl/modules/system/service/impl/UserServiceImpl.java +++ b/nladmin-system/src/main/java/org/nl/modules/system/service/impl/UserServiceImpl.java @@ -20,8 +20,8 @@ import lombok.RequiredArgsConstructor; import org.nl.config.FileProperties; import org.nl.exception.EntityExistException; import org.nl.exception.EntityNotFoundException; +import org.nl.modules.security.satoken.utils.FlushSessionUtil; import org.nl.modules.security.service.OnlineUserService; -import org.nl.modules.security.service.UserCacheClean; import org.nl.modules.system.domain.User; import org.nl.modules.system.repository.UserRepository; import org.nl.modules.system.service.UserService; @@ -59,8 +59,9 @@ public class UserServiceImpl implements UserService { private final UserMapper userMapper; private final FileProperties properties; private final RedisUtils redisUtils; - private final UserCacheClean userCacheClean; private final OnlineUserService onlineUserService; + private final FlushSessionUtil flushSessionUtil; + @Override public Object queryAll(UserQueryCriteria criteria, Pageable pageable) { @@ -137,6 +138,8 @@ public class UserServiceImpl implements UserService { userRepository.save(user); // 清除缓存 delCaches(user.getId(), user.getUsername()); + // 修改session + flushSession(user); } @Override @@ -149,6 +152,8 @@ public class UserServiceImpl implements UserService { userRepository.save(user); // 清理缓存 delCaches(user.getId(), user.getUsername()); + // 修改session + flushSession(user); } @Override @@ -178,7 +183,7 @@ public class UserServiceImpl implements UserService { public void updatePass(String username, String pass) { userRepository.updatePass(username, pass, new Date()); redisUtils.del("user::username:" + username); - flushCache(username); + flushSession(userRepository.findByUsername(username)); } @Override @@ -194,7 +199,7 @@ public class UserServiceImpl implements UserService { FileUtil.del(oldPath); } @NotBlank String username = user.getUsername(); - flushCache(username); + flushSession(user); return new HashMap(1) {{ put("avatar", file.getName()); }}; @@ -204,7 +209,7 @@ public class UserServiceImpl implements UserService { @Transactional(rollbackFor = Exception.class) public void updateEmail(String username, String email) { userRepository.updateEmail(username, email); - flushCache(username); + flushSession(userRepository.findByUsername(username)); } @Override @@ -234,15 +239,15 @@ public class UserServiceImpl implements UserService { */ public void delCaches(Long id, String username) { redisUtils.del(CacheKey.USER_ID + id); - flushCache(username); +// flushCache(username); } /** * 清理 登陆时 用户缓存信息 * - * @param username / + * @param user / */ - private void flushCache(String username) { - userCacheClean.cleanUserCache(username); + private void flushSession(User user) { + flushSessionUtil.flushSessionInfo(user); } }