Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 还原登录 Helper 优化 #15

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
/**
* 登录助手
*
* @author Lion Li(RuoYi-Vue-Plus)
* @author Charles7c
* @author Lion Li(RuoYi-Vue-Plus)
* @since 2022/12/24 12:58
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
Expand All @@ -55,23 +55,18 @@ public class LoginHelper {
* 登录用户信息
*/
public static void login(LoginUser loginUser) {
if (null == loginUser) {
return;
}

// 记录登录信息
HttpServletRequest request = ServletUtils.getRequest();
loginUser.setClientIp(ServletUtil.getClientIP(request));
loginUser.setLocation(IpUtils.getCityInfo(loginUser.getClientIp()));
loginUser.setBrowser(ServletUtils.getBrowser(request));
LogContext logContext = LogContextHolder.get();
loginUser.setLoginTime(null != logContext ? logContext.getCreateTime() : LocalDateTime.now());

// 登录保存用户信息
// 登录并缓存用户信息
StpUtil.login(loginUser.getId());
SaHolder.getStorage().set(CacheConsts.LOGIN_USER_KEY, loginUser);
loginUser.setToken(StpUtil.getTokenValue());
StpUtil.getSession().set(CacheConsts.LOGIN_USER_KEY, loginUser);
StpUtil.getTokenSession().set(CacheConsts.LOGIN_USER_KEY, loginUser);
}

/**
Expand All @@ -84,11 +79,11 @@ public static LoginUser getLoginUser() {
if (null != loginUser) {
return loginUser;
}
SaSession session = StpUtil.getSession();
if (null == session) {
SaSession tokenSession = StpUtil.getTokenSession();
if (null == tokenSession) {
return null;
}
loginUser = (LoginUser)session.get(CacheConsts.LOGIN_USER_KEY);
loginUser = (LoginUser)tokenSession.get(CacheConsts.LOGIN_USER_KEY);
SaHolder.getStorage().set(CacheConsts.LOGIN_USER_KEY, loginUser);
return loginUser;
}
Expand All @@ -101,12 +96,11 @@ public static LoginUser getLoginUser() {
* @return 登录用户信息
*/
public static LoginUser getLoginUser(String token) {
Object loginId = StpUtil.getLoginIdByToken(token);
SaSession session = StpUtil.getSessionByLoginId(loginId);
if (null == session) {
SaSession tokenSession = StpUtil.getTokenSessionByToken(token);
if (null == tokenSession) {
return null;
}
return (LoginUser)session.get(CacheConsts.LOGIN_USER_KEY);
return (LoginUser)tokenSession.get(CacheConsts.LOGIN_USER_KEY);
}

/**
Expand Down