优化登录逻辑的实现,封装统一的登录成功方法,创建 Session + 记录登录日志

This commit is contained in:
YunaiV 2022-03-30 00:02:55 +08:00
parent b9693c3e06
commit 56ea65e53d
2 changed files with 19 additions and 12 deletions

View File

@ -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();
}

View File

@ -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);
}