Commit 56ea65e53d073b99e9d18bcde27a3158d7d546e9
1 parent
b9693c3e
优化登录逻辑的实现,封装统一的登录成功方法,创建 Session + 记录登录日志
Showing
2 changed files
with
19 additions
and
12 deletions
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 { |
| 87 | 87 | LoginUser loginUser = this.login0(reqVO.getMobile(), reqVO.getPassword()); |
| 88 | 88 | |
| 89 | 89 | // 缓存登录用户到 Redis 中,返回 sessionId 编号 |
| 90 | - return userSessionApi.createUserSession(loginUser, userIp, userAgent); | |
| 90 | + return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_USERNAME, userIp, userAgent); | |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | @Override |
| ... | ... | @@ -101,11 +101,10 @@ public class MemberAuthServiceImpl implements MemberAuthService { |
| 101 | 101 | Assert.notNull(user, "获取用户失败,结果为空"); |
| 102 | 102 | |
| 103 | 103 | // 执行登陆 |
| 104 | - this.createLoginLog(user.getMobile(), LoginLogTypeEnum.LOGIN_SMS, LoginResultEnum.SUCCESS); | |
| 105 | 104 | LoginUser loginUser = AuthConvert.INSTANCE.convert(user); |
| 106 | 105 | |
| 107 | 106 | // 缓存登录用户到 Redis 中,返回 sessionId 编号 |
| 108 | - return userSessionApi.createUserSession(loginUser, userIp, userAgent); | |
| 107 | + return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_SMS, userIp, userAgent); | |
| 109 | 108 | } |
| 110 | 109 | |
| 111 | 110 | @Override |
| ... | ... | @@ -122,7 +121,6 @@ public class MemberAuthServiceImpl implements MemberAuthService { |
| 122 | 121 | if (user == null) { |
| 123 | 122 | throw exception(USER_NOT_EXISTS); |
| 124 | 123 | } |
| 125 | - this.createLoginLog(user.getMobile(), LoginLogTypeEnum.LOGIN_SOCIAL, LoginResultEnum.SUCCESS); | |
| 126 | 124 | |
| 127 | 125 | // 创建 LoginUser 对象 |
| 128 | 126 | LoginUser loginUser = AuthConvert.INSTANCE.convert(user); |
| ... | ... | @@ -131,7 +129,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { |
| 131 | 129 | socialUserApi.bindSocialUser(AuthConvert.INSTANCE.convert(loginUser.getId(), getUserType().getValue(), reqVO)); |
| 132 | 130 | |
| 133 | 131 | // 缓存登录用户到 Redis 中,返回 sessionId 编号 |
| 134 | - return userSessionApi.createUserSession(loginUser, userIp, userAgent); | |
| 132 | + return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_SOCIAL, userIp, userAgent); | |
| 135 | 133 | } |
| 136 | 134 | |
| 137 | 135 | @Override |
| ... | ... | @@ -150,6 +148,13 @@ public class MemberAuthServiceImpl implements MemberAuthService { |
| 150 | 148 | return sessionId; |
| 151 | 149 | } |
| 152 | 150 | |
| 151 | + private String createUserSessionAfterLoginSuccess(LoginUser loginUser, LoginLogTypeEnum logType, String userIp, String userAgent) { | |
| 152 | + // 插入登陆日志 | |
| 153 | + createLoginLog(loginUser.getUsername(), logType, LoginResultEnum.SUCCESS); | |
| 154 | + // 缓存登录用户到 Redis 中,返回 sessionId 编号 | |
| 155 | + return userSessionApi.createUserSession(loginUser, userIp, userAgent); | |
| 156 | + } | |
| 157 | + | |
| 153 | 158 | @Override |
| 154 | 159 | public void socialBind(Long userId, AppAuthSocialBindReqVO reqVO) { |
| 155 | 160 | // 绑定社交用户(新增) |
| ... | ... | @@ -186,9 +191,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { |
| 186 | 191 | this.createLoginLog(username, logType, LoginResultEnum.UNKNOWN_ERROR); |
| 187 | 192 | throw exception(AUTH_LOGIN_FAIL_UNKNOWN); |
| 188 | 193 | } |
| 189 | - // 登录成功的日志 | |
| 190 | 194 | Assert.notNull(authentication.getPrincipal(), "Principal 不会为空"); |
| 191 | - this.createLoginLog(username, logType, LoginResultEnum.SUCCESS); | |
| 192 | 195 | return (LoginUser) authentication.getPrincipal(); |
| 193 | 196 | } |
| 194 | 197 | ... | ... |
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 { |
| 107 | 107 | LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); |
| 108 | 108 | |
| 109 | 109 | // 缓存登陆用户到 Redis 中,返回 sessionId 编号 |
| 110 | - return userSessionService.createUserSession(loginUser, userIp, userAgent); | |
| 110 | + return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_USERNAME, userIp, userAgent); | |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | private void verifyCaptcha(AuthLoginReqVO reqVO) { |
| ... | ... | @@ -155,9 +155,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { |
| 155 | 155 | this.createLoginLog(username, logTypeEnum, LoginResultEnum.UNKNOWN_ERROR); |
| 156 | 156 | throw exception(AUTH_LOGIN_FAIL_UNKNOWN); |
| 157 | 157 | } |
| 158 | - // 登录成功的日志 | |
| 159 | 158 | Assert.notNull(authentication.getPrincipal(), "Principal 不会为空"); |
| 160 | - this.createLoginLog(username, logTypeEnum, LoginResultEnum.SUCCESS); | |
| 161 | 159 | return (LoginUser) authentication.getPrincipal(); |
| 162 | 160 | } |
| 163 | 161 | |
| ... | ... | @@ -207,7 +205,6 @@ public class AdminAuthServiceImpl implements AdminAuthService { |
| 207 | 205 | if (user == null) { |
| 208 | 206 | throw exception(USER_NOT_EXISTS); |
| 209 | 207 | } |
| 210 | - this.createLoginLog(user.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL, LoginResultEnum.SUCCESS); | |
| 211 | 208 | |
| 212 | 209 | // 创建 LoginUser 对象 |
| 213 | 210 | LoginUser loginUser = this.buildLoginUser(user); |
| ... | ... | @@ -216,7 +213,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { |
| 216 | 213 | socialUserService.bindSocialUser(AuthConvert.INSTANCE.convert(loginUser.getId(), getUserType().getValue(), reqVO)); |
| 217 | 214 | |
| 218 | 215 | // 缓存登录用户到 Redis 中,返回 sessionId 编号 |
| 219 | - return userSessionService.createUserSession(loginUser, userIp, userAgent); | |
| 216 | + return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_SOCIAL, userIp, userAgent); | |
| 220 | 217 | } |
| 221 | 218 | |
| 222 | 219 | @Override |
| ... | ... | @@ -232,6 +229,13 @@ public class AdminAuthServiceImpl implements AdminAuthService { |
| 232 | 229 | socialUserService.bindSocialUser(AuthConvert.INSTANCE.convert(loginUser.getId(), getUserType().getValue(), reqVO)); |
| 233 | 230 | |
| 234 | 231 | // 缓存登录用户到 Redis 中,返回 sessionId 编号 |
| 232 | + return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_SOCIAL, userIp, userAgent); | |
| 233 | + } | |
| 234 | + | |
| 235 | + private String createUserSessionAfterLoginSuccess(LoginUser loginUser, LoginLogTypeEnum logType, String userIp, String userAgent) { | |
| 236 | + // 插入登陆日志 | |
| 237 | + createLoginLog(loginUser.getUsername(), logType, LoginResultEnum.SUCCESS); | |
| 238 | + // 缓存登录用户到 Redis 中,返回 sessionId 编号 | |
| 235 | 239 | return userSessionService.createUserSession(loginUser, userIp, userAgent); |
| 236 | 240 | } |
| 237 | 241 | ... | ... |