session动态更新
This commit is contained in:
@@ -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<String> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -106,7 +106,6 @@ public class OnlineUserService {
|
|||||||
* @param key /
|
* @param key /
|
||||||
*/
|
*/
|
||||||
public void kickOut(String key){
|
public void kickOut(String key){
|
||||||
// key = properties.getOnlineKey() + key;
|
|
||||||
redisUtils.del(key);
|
redisUtils.del(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +114,6 @@ public class OnlineUserService {
|
|||||||
* @param token /
|
* @param token /
|
||||||
*/
|
*/
|
||||||
public void logout(String token) {
|
public void logout(String token) {
|
||||||
// String key = properties.getOnlineKey() + token;
|
|
||||||
redisUtils.del(token);
|
redisUtils.del(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,49 +1,49 @@
|
|||||||
/*
|
///*
|
||||||
* Copyright 2019-2020 the original author or authors.
|
// * Copyright 2019-2020 the original author or authors.
|
||||||
*
|
// *
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
// * Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
// * you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
// * You may obtain a copy of the License at
|
||||||
*
|
// *
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
// * http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
// *
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
// * Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
// * distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
// * See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
// * limitations under the License.
|
||||||
*/
|
// */
|
||||||
|
//
|
||||||
package org.nl.modules.security.service;
|
//package org.nl.modules.security.service;
|
||||||
|
//
|
||||||
import cn.hutool.core.util.StrUtil;
|
//import cn.hutool.core.util.StrUtil;
|
||||||
import org.springframework.stereotype.Component;
|
//import org.springframework.stereotype.Component;
|
||||||
|
//
|
||||||
/**
|
///**
|
||||||
* @author: liaojinlong
|
// * @author: liaojinlong
|
||||||
* @date: 2020/6/11 18:01
|
// * @date: 2020/6/11 18:01
|
||||||
* @apiNote: 用于清理 用户登录信息缓存,为防止Spring循环依赖与安全考虑 ,单独构成工具类
|
// * @apiNote: 用于清理 用户登录信息缓存,为防止Spring循环依赖与安全考虑 ,单独构成工具类
|
||||||
*/
|
// */
|
||||||
@Component
|
//@Component
|
||||||
public class UserCacheClean {
|
//public class UserCacheClean {
|
||||||
|
//
|
||||||
/**
|
// /**
|
||||||
* 清理特定用户缓存信息<br>
|
// * 清理特定用户缓存信息<br>
|
||||||
* 用户信息变更时
|
// * 用户信息变更时
|
||||||
*
|
// *
|
||||||
* @param userName /
|
// * @param userName /
|
||||||
*/
|
// */
|
||||||
public void cleanUserCache(String userName) {
|
// public void cleanUserCache(String userName) {
|
||||||
if (StrUtil.isNotEmpty(userName)) {
|
// if (StrUtil.isNotEmpty(userName)) {
|
||||||
// UserDetailsServiceImpl.userDtoCache.remove(userName);
|
//// UserDetailsServiceImpl.userDtoCache.remove(userName);
|
||||||
}
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清理所有用户的缓存信息<br>
|
|
||||||
* ,如发生角色授权信息变化,可以简便的全部失效缓存
|
|
||||||
*/
|
|
||||||
// public void cleanAll() {
|
|
||||||
// UserDetailsServiceImpl.userDtoCache.clear();
|
|
||||||
// }
|
// }
|
||||||
}
|
//
|
||||||
|
// /**
|
||||||
|
// * 清理所有用户的缓存信息<br>
|
||||||
|
// * ,如发生角色授权信息变化,可以简便的全部失效缓存
|
||||||
|
// */
|
||||||
|
//// public void cleanAll() {
|
||||||
|
//// UserDetailsServiceImpl.userDtoCache.clear();
|
||||||
|
//// }
|
||||||
|
//}
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import com.alibaba.fastjson.JSONObject;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.nl.exception.BadRequestException;
|
import org.nl.exception.BadRequestException;
|
||||||
import org.nl.exception.EntityExistException;
|
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.Role;
|
||||||
import org.nl.modules.system.domain.User;
|
import org.nl.modules.system.domain.User;
|
||||||
import org.nl.modules.system.repository.RoleRepository;
|
import org.nl.modules.system.repository.RoleRepository;
|
||||||
@@ -63,7 +62,7 @@ public class RoleServiceImpl implements RoleService {
|
|||||||
private final RoleSmallMapper roleSmallMapper;
|
private final RoleSmallMapper roleSmallMapper;
|
||||||
private final RedisUtils redisUtils;
|
private final RedisUtils redisUtils;
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final UserCacheClean userCacheClean;
|
// private final UserCacheClean userCacheClean;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<RoleDto> queryAll() {
|
public List<RoleDto> queryAll() {
|
||||||
@@ -162,25 +161,6 @@ public class RoleServiceImpl implements RoleService {
|
|||||||
return Collections.min(roleDtos.stream().map(RoleDto::getLevel).collect(Collectors.toList()));
|
return Collections.min(roleDtos.stream().map(RoleDto::getLevel).collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Override
|
|
||||||
// @Cacheable(key = "'auth:' + #p0.id")
|
|
||||||
// public List<String> mapToGrantedAuthorities(UserDto user) {
|
|
||||||
// Set<String> permissions = new HashSet<>();
|
|
||||||
// // 如果是管理员直接返回
|
|
||||||
// if (user.getIsAdmin()) {
|
|
||||||
// permissions.add("admin");
|
|
||||||
//// return permissions.stream().map(SimpleGrantedAuthority::new)
|
|
||||||
//// .collect(Collectors.toList());
|
|
||||||
// }
|
|
||||||
// Set<Role> 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
|
@Override
|
||||||
@Cacheable(key = "'auth:' + #p0")
|
@Cacheable(key = "'auth:' + #p0")
|
||||||
public List<String> getPermissionList(String id) {
|
public List<String> getPermissionList(String id) {
|
||||||
@@ -234,7 +214,7 @@ public class RoleServiceImpl implements RoleService {
|
|||||||
public void delCaches(Long id, List<User> users) {
|
public void delCaches(Long id, List<User> users) {
|
||||||
users = CollectionUtil.isEmpty(users) ? userRepository.findByRoleId(id) : users;
|
users = CollectionUtil.isEmpty(users) ? userRepository.findByRoleId(id) : users;
|
||||||
if (CollectionUtil.isNotEmpty(users)) {
|
if (CollectionUtil.isNotEmpty(users)) {
|
||||||
users.forEach(item -> userCacheClean.cleanUserCache(item.getUsername()));
|
// users.forEach(item -> userCacheClean.cleanUserCache(item.getUsername()));
|
||||||
Set<Long> userIds = users.stream().map(User::getId).collect(Collectors.toSet());
|
Set<Long> userIds = users.stream().map(User::getId).collect(Collectors.toSet());
|
||||||
redisUtils.delByKeys(CacheKey.DATA_USER, userIds);
|
redisUtils.delByKeys(CacheKey.DATA_USER, userIds);
|
||||||
redisUtils.delByKeys(CacheKey.MENU_USER, userIds);
|
redisUtils.delByKeys(CacheKey.MENU_USER, userIds);
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import org.nl.config.FileProperties;
|
import org.nl.config.FileProperties;
|
||||||
import org.nl.exception.EntityExistException;
|
import org.nl.exception.EntityExistException;
|
||||||
import org.nl.exception.EntityNotFoundException;
|
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.OnlineUserService;
|
||||||
import org.nl.modules.security.service.UserCacheClean;
|
|
||||||
import org.nl.modules.system.domain.User;
|
import org.nl.modules.system.domain.User;
|
||||||
import org.nl.modules.system.repository.UserRepository;
|
import org.nl.modules.system.repository.UserRepository;
|
||||||
import org.nl.modules.system.service.UserService;
|
import org.nl.modules.system.service.UserService;
|
||||||
@@ -59,8 +59,9 @@ public class UserServiceImpl implements UserService {
|
|||||||
private final UserMapper userMapper;
|
private final UserMapper userMapper;
|
||||||
private final FileProperties properties;
|
private final FileProperties properties;
|
||||||
private final RedisUtils redisUtils;
|
private final RedisUtils redisUtils;
|
||||||
private final UserCacheClean userCacheClean;
|
|
||||||
private final OnlineUserService onlineUserService;
|
private final OnlineUserService onlineUserService;
|
||||||
|
private final FlushSessionUtil flushSessionUtil;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object queryAll(UserQueryCriteria criteria, Pageable pageable) {
|
public Object queryAll(UserQueryCriteria criteria, Pageable pageable) {
|
||||||
@@ -137,6 +138,8 @@ public class UserServiceImpl implements UserService {
|
|||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
// 清除缓存
|
// 清除缓存
|
||||||
delCaches(user.getId(), user.getUsername());
|
delCaches(user.getId(), user.getUsername());
|
||||||
|
// 修改session
|
||||||
|
flushSession(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -149,6 +152,8 @@ public class UserServiceImpl implements UserService {
|
|||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
// 清理缓存
|
// 清理缓存
|
||||||
delCaches(user.getId(), user.getUsername());
|
delCaches(user.getId(), user.getUsername());
|
||||||
|
// 修改session
|
||||||
|
flushSession(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -178,7 +183,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
public void updatePass(String username, String pass) {
|
public void updatePass(String username, String pass) {
|
||||||
userRepository.updatePass(username, pass, new Date());
|
userRepository.updatePass(username, pass, new Date());
|
||||||
redisUtils.del("user::username:" + username);
|
redisUtils.del("user::username:" + username);
|
||||||
flushCache(username);
|
flushSession(userRepository.findByUsername(username));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -194,7 +199,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
FileUtil.del(oldPath);
|
FileUtil.del(oldPath);
|
||||||
}
|
}
|
||||||
@NotBlank String username = user.getUsername();
|
@NotBlank String username = user.getUsername();
|
||||||
flushCache(username);
|
flushSession(user);
|
||||||
return new HashMap<String, String>(1) {{
|
return new HashMap<String, String>(1) {{
|
||||||
put("avatar", file.getName());
|
put("avatar", file.getName());
|
||||||
}};
|
}};
|
||||||
@@ -204,7 +209,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public void updateEmail(String username, String email) {
|
public void updateEmail(String username, String email) {
|
||||||
userRepository.updateEmail(username, email);
|
userRepository.updateEmail(username, email);
|
||||||
flushCache(username);
|
flushSession(userRepository.findByUsername(username));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -234,15 +239,15 @@ public class UserServiceImpl implements UserService {
|
|||||||
*/
|
*/
|
||||||
public void delCaches(Long id, String username) {
|
public void delCaches(Long id, String username) {
|
||||||
redisUtils.del(CacheKey.USER_ID + id);
|
redisUtils.del(CacheKey.USER_ID + id);
|
||||||
flushCache(username);
|
// flushCache(username);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清理 登陆时 用户缓存信息
|
* 清理 登陆时 用户缓存信息
|
||||||
*
|
*
|
||||||
* @param username /
|
* @param user /
|
||||||
*/
|
*/
|
||||||
private void flushCache(String username) {
|
private void flushSession(User user) {
|
||||||
userCacheClean.cleanUserCache(username);
|
flushSessionUtil.flushSessionInfo(user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user