删除了用户信息缓存功能。
This commit is contained in:
parent
f0ba4c0d31
commit
82054ed718
@ -27,12 +27,7 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
// 创建定时缓存,缓存时间与JWT过期时间一致
|
// 创建定时缓存,缓存时间与JWT过期时间一致
|
||||||
return CacheUtil.newTimedCache(jwtExpirationMs);
|
return CacheUtil.newTimedCache(jwtExpirationMs);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
@Bean
|
|
||||||
public Cache<String, String> loginuserCache() {
|
|
||||||
return CacheUtil.newLRUCache(200);//用户登录缓存数 缺省200
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 静态资源处理
|
* 静态资源处理
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
|
@ -64,10 +64,6 @@ public class OrganizationController {
|
|||||||
if (StrUtil.isEmpty(organization.getId())) {
|
if (StrUtil.isEmpty(organization.getId())) {
|
||||||
return ResponseResult.error("组织信息id不能为空");
|
return ResponseResult.error("组织信息id不能为空");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (organization == null) {
|
|
||||||
return ResponseResult.error("组织信息不能为空");
|
|
||||||
}
|
|
||||||
//填写 最近修改者
|
//填写 最近修改者
|
||||||
organization.setLastmodifier(AuthUtils.getUsername());
|
organization.setLastmodifier(AuthUtils.getUsername());
|
||||||
//填写 最近修改时间
|
//填写 最近修改时间
|
||||||
|
@ -222,6 +222,7 @@ private Long jwtExpirationMs;
|
|||||||
String userId = loginUser.getUser().getId();
|
String userId = loginUser.getUser().getId();
|
||||||
long expireTime = System.currentTimeMillis() + jwtExpirationMs;
|
long expireTime = System.currentTimeMillis() + jwtExpirationMs;
|
||||||
Map<String, Object> map = new HashMap();
|
Map<String, Object> map = new HashMap();
|
||||||
|
map.put("appid", loginUser.getUser().getAppId());
|
||||||
map.put("userid", userId);
|
map.put("userid", userId);
|
||||||
map.put("username", loginUser.getUsername());
|
map.put("username", loginUser.getUsername());
|
||||||
map.put("nickname", loginUser.getUser().getNickname());
|
map.put("nickname", loginUser.getUser().getNickname());
|
||||||
@ -229,9 +230,6 @@ private Long jwtExpirationMs;
|
|||||||
String token = jwtUtils.generateToken(loginUser.getUsername(), userId);
|
String token = jwtUtils.generateToken(loginUser.getUsername(), userId);
|
||||||
map.put("token", token);
|
map.put("token", token);
|
||||||
map.put("permissions", loginUser.getPermissions());
|
map.put("permissions", loginUser.getPermissions());
|
||||||
//把完整的用户信息存入到HuTool缓存中,userId作为key
|
|
||||||
String jsonStr = JSONUtil.toJsonStr(loginUser);
|
|
||||||
webConfig.loginuserCache().put("login:" + userId, jsonStr);
|
|
||||||
return ResponseResult.successData(map);
|
return ResponseResult.successData(map);
|
||||||
}
|
}
|
||||||
catch (BadCredentialsException e) {
|
catch (BadCredentialsException e) {
|
||||||
@ -262,10 +260,7 @@ private Long jwtExpirationMs;
|
|||||||
UsernamePasswordAuthenticationToken authentication =
|
UsernamePasswordAuthenticationToken authentication =
|
||||||
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
||||||
LoginUser loginuser = (LoginUser) authentication.getPrincipal();
|
LoginUser loginuser = (LoginUser) authentication.getPrincipal();
|
||||||
String userId = loginuser.getUser().getId();
|
//记录退出日志
|
||||||
//删除redis中的登陆用户信息
|
|
||||||
webConfig.loginuserCache().remove("login:" + userId);
|
|
||||||
//记录退出日志
|
|
||||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
HttpServletRequest request = attributes.getRequest();
|
HttpServletRequest request = attributes.getRequest();
|
||||||
AppOptLog sysLog = new AppOptLog();
|
AppOptLog sysLog = new AppOptLog();
|
||||||
|
69
backend/src/main/java/com/stdproject/utils/AuthUtils.java
Normal file
69
backend/src/main/java/com/stdproject/utils/AuthUtils.java
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package com.stdproject.utils;
|
||||||
|
|
||||||
|
import com.stdproject.entity.LoginUser;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 认证工具类
|
||||||
|
* 用于获取当前登录用户信息
|
||||||
|
*
|
||||||
|
* @author system
|
||||||
|
* @since 2025-01-27
|
||||||
|
*/
|
||||||
|
public class AuthUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前登录用户
|
||||||
|
*
|
||||||
|
* @return LoginUser 当前登录用户信息
|
||||||
|
*/
|
||||||
|
public static LoginUser getUser() {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
if (authentication != null && authentication.getPrincipal() instanceof UserDetails) {
|
||||||
|
return (LoginUser) authentication.getPrincipal();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 获取当前登录用户名
|
||||||
|
*
|
||||||
|
* @return String 用户名
|
||||||
|
*/
|
||||||
|
public static String getUsername() {
|
||||||
|
LoginUser user = getUser();
|
||||||
|
return user != null ? user.getUsername() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前登录用户昵称
|
||||||
|
*
|
||||||
|
* @return String 用户昵称
|
||||||
|
*/
|
||||||
|
public static String getNickname() {
|
||||||
|
LoginUser user = getUser();
|
||||||
|
return user != null ? user.geNickname() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前登录用户ID
|
||||||
|
*
|
||||||
|
* @return Long 用户ID
|
||||||
|
*/
|
||||||
|
public static String getUserId() {
|
||||||
|
LoginUser user = getUser();
|
||||||
|
return user != null && user.getUser() != null ? user.getUser().getId() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查是否已登录
|
||||||
|
*
|
||||||
|
* @return boolean 是否已登录
|
||||||
|
*/
|
||||||
|
public static boolean isAuthenticated() {
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
return authentication != null && authentication.isAuthenticated()
|
||||||
|
&& authentication.getPrincipal() instanceof UserDetails;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user