From 56ea65e53d073b99e9d18bcde27a3158d7d546e9 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Wed, 30 Mar 2022 00:02:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=99=BB=E5=BD=95=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E7=9A=84=E5=AE=9E=E7=8E=B0=EF=BC=8C=E5=B0=81=E8=A3=85?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E7=9A=84=E7=99=BB=E5=BD=95=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E5=88=9B=E5=BB=BA=20Session=20+=20?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E7=99=BB=E5=BD=95=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/auth/MemberAuthServiceImpl.java | 17 ++++++++++------- .../service/auth/AdminAuthServiceImpl.java | 14 +++++++++----- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java index 107c23b63..f28af5081 100644 --- a/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java +++ b/yudao-module-member/yudao-module-member-impl/src/main/java/cn/iocoder/yudao/module/member/service/auth/MemberAuthServiceImpl.java @@ -87,7 +87,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { LoginUser loginUser = this.login0(reqVO.getMobile(), reqVO.getPassword()); // 缓存登录用户到 Redis 中,返回 sessionId 编号 - return userSessionApi.createUserSession(loginUser, userIp, userAgent); + return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_USERNAME, userIp, userAgent); } @Override @@ -101,11 +101,10 @@ public class MemberAuthServiceImpl implements MemberAuthService { Assert.notNull(user, "获取用户失败,结果为空"); // 执行登陆 - this.createLoginLog(user.getMobile(), LoginLogTypeEnum.LOGIN_SMS, LoginResultEnum.SUCCESS); LoginUser loginUser = AuthConvert.INSTANCE.convert(user); // 缓存登录用户到 Redis 中,返回 sessionId 编号 - return userSessionApi.createUserSession(loginUser, userIp, userAgent); + return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_SMS, userIp, userAgent); } @Override @@ -122,7 +121,6 @@ public class MemberAuthServiceImpl implements MemberAuthService { if (user == null) { throw exception(USER_NOT_EXISTS); } - this.createLoginLog(user.getMobile(), LoginLogTypeEnum.LOGIN_SOCIAL, LoginResultEnum.SUCCESS); // 创建 LoginUser 对象 LoginUser loginUser = AuthConvert.INSTANCE.convert(user); @@ -131,7 +129,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { socialUserApi.bindSocialUser(AuthConvert.INSTANCE.convert(loginUser.getId(), getUserType().getValue(), reqVO)); // 缓存登录用户到 Redis 中,返回 sessionId 编号 - return userSessionApi.createUserSession(loginUser, userIp, userAgent); + return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_SOCIAL, userIp, userAgent); } @Override @@ -150,6 +148,13 @@ public class MemberAuthServiceImpl implements MemberAuthService { return sessionId; } + private String createUserSessionAfterLoginSuccess(LoginUser loginUser, LoginLogTypeEnum logType, String userIp, String userAgent) { + // 插入登陆日志 + createLoginLog(loginUser.getUsername(), logType, LoginResultEnum.SUCCESS); + // 缓存登录用户到 Redis 中,返回 sessionId 编号 + return userSessionApi.createUserSession(loginUser, userIp, userAgent); + } + @Override public void socialBind(Long userId, AppAuthSocialBindReqVO reqVO) { // 绑定社交用户(新增) @@ -186,9 +191,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { this.createLoginLog(username, logType, LoginResultEnum.UNKNOWN_ERROR); throw exception(AUTH_LOGIN_FAIL_UNKNOWN); } - // 登录成功的日志 Assert.notNull(authentication.getPrincipal(), "Principal 不会为空"); - this.createLoginLog(username, logType, LoginResultEnum.SUCCESS); return (LoginUser) authentication.getPrincipal(); } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java index 38018c460..0d3303d8d 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java @@ -107,7 +107,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); // 缓存登陆用户到 Redis 中,返回 sessionId 编号 - return userSessionService.createUserSession(loginUser, userIp, userAgent); + return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_USERNAME, userIp, userAgent); } private void verifyCaptcha(AuthLoginReqVO reqVO) { @@ -155,9 +155,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { this.createLoginLog(username, logTypeEnum, LoginResultEnum.UNKNOWN_ERROR); throw exception(AUTH_LOGIN_FAIL_UNKNOWN); } - // 登录成功的日志 Assert.notNull(authentication.getPrincipal(), "Principal 不会为空"); - this.createLoginLog(username, logTypeEnum, LoginResultEnum.SUCCESS); return (LoginUser) authentication.getPrincipal(); } @@ -207,7 +205,6 @@ public class AdminAuthServiceImpl implements AdminAuthService { if (user == null) { throw exception(USER_NOT_EXISTS); } - this.createLoginLog(user.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL, LoginResultEnum.SUCCESS); // 创建 LoginUser 对象 LoginUser loginUser = this.buildLoginUser(user); @@ -216,7 +213,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { socialUserService.bindSocialUser(AuthConvert.INSTANCE.convert(loginUser.getId(), getUserType().getValue(), reqVO)); // 缓存登录用户到 Redis 中,返回 sessionId 编号 - return userSessionService.createUserSession(loginUser, userIp, userAgent); + return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_SOCIAL, userIp, userAgent); } @Override @@ -231,6 +228,13 @@ public class AdminAuthServiceImpl implements AdminAuthService { // 绑定社交用户(新增) socialUserService.bindSocialUser(AuthConvert.INSTANCE.convert(loginUser.getId(), getUserType().getValue(), reqVO)); + // 缓存登录用户到 Redis 中,返回 sessionId 编号 + return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_SOCIAL, userIp, userAgent); + } + + private String createUserSessionAfterLoginSuccess(LoginUser loginUser, LoginLogTypeEnum logType, String userIp, String userAgent) { + // 插入登陆日志 + createLoginLog(loginUser.getUsername(), logType, LoginResultEnum.SUCCESS); // 缓存登录用户到 Redis 中,返回 sessionId 编号 return userSessionService.createUserSession(loginUser, userIp, userAgent); }