mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-10-31 10:18:42 +08:00 
			
		
		
		
	优化登录逻辑的实现,封装统一的登录成功方法,创建 Session + 记录登录日志
This commit is contained in:
		| @@ -87,7 +87,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { | |||||||
|         LoginUser loginUser = this.login0(reqVO.getMobile(), reqVO.getPassword()); |         LoginUser loginUser = this.login0(reqVO.getMobile(), reqVO.getPassword()); | ||||||
|  |  | ||||||
|         // 缓存登录用户到 Redis 中,返回 sessionId 编号 |         // 缓存登录用户到 Redis 中,返回 sessionId 编号 | ||||||
|         return userSessionApi.createUserSession(loginUser, userIp, userAgent); |         return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_USERNAME, userIp, userAgent); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -101,11 +101,10 @@ public class MemberAuthServiceImpl implements MemberAuthService { | |||||||
|         Assert.notNull(user, "获取用户失败,结果为空"); |         Assert.notNull(user, "获取用户失败,结果为空"); | ||||||
|  |  | ||||||
|         // 执行登陆 |         // 执行登陆 | ||||||
|         this.createLoginLog(user.getMobile(), LoginLogTypeEnum.LOGIN_SMS, LoginResultEnum.SUCCESS); |  | ||||||
|         LoginUser loginUser = AuthConvert.INSTANCE.convert(user); |         LoginUser loginUser = AuthConvert.INSTANCE.convert(user); | ||||||
|  |  | ||||||
|         // 缓存登录用户到 Redis 中,返回 sessionId 编号 |         // 缓存登录用户到 Redis 中,返回 sessionId 编号 | ||||||
|         return userSessionApi.createUserSession(loginUser, userIp, userAgent); |         return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_SMS, userIp, userAgent); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -122,7 +121,6 @@ public class MemberAuthServiceImpl implements MemberAuthService { | |||||||
|         if (user == null) { |         if (user == null) { | ||||||
|             throw exception(USER_NOT_EXISTS); |             throw exception(USER_NOT_EXISTS); | ||||||
|         } |         } | ||||||
|         this.createLoginLog(user.getMobile(), LoginLogTypeEnum.LOGIN_SOCIAL, LoginResultEnum.SUCCESS); |  | ||||||
|  |  | ||||||
|         // 创建 LoginUser 对象 |         // 创建 LoginUser 对象 | ||||||
|         LoginUser loginUser = AuthConvert.INSTANCE.convert(user); |         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)); |         socialUserApi.bindSocialUser(AuthConvert.INSTANCE.convert(loginUser.getId(), getUserType().getValue(), reqVO)); | ||||||
|  |  | ||||||
|         // 缓存登录用户到 Redis 中,返回 sessionId 编号 |         // 缓存登录用户到 Redis 中,返回 sessionId 编号 | ||||||
|         return userSessionApi.createUserSession(loginUser, userIp, userAgent); |         return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_SOCIAL, userIp, userAgent); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -150,6 +148,13 @@ public class MemberAuthServiceImpl implements MemberAuthService { | |||||||
|         return sessionId; |         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 |     @Override | ||||||
|     public void socialBind(Long userId, AppAuthSocialBindReqVO reqVO) { |     public void socialBind(Long userId, AppAuthSocialBindReqVO reqVO) { | ||||||
|         // 绑定社交用户(新增) |         // 绑定社交用户(新增) | ||||||
| @@ -186,9 +191,7 @@ public class MemberAuthServiceImpl implements MemberAuthService { | |||||||
|             this.createLoginLog(username, logType, LoginResultEnum.UNKNOWN_ERROR); |             this.createLoginLog(username, logType, LoginResultEnum.UNKNOWN_ERROR); | ||||||
|             throw exception(AUTH_LOGIN_FAIL_UNKNOWN); |             throw exception(AUTH_LOGIN_FAIL_UNKNOWN); | ||||||
|         } |         } | ||||||
|         // 登录成功的日志 |  | ||||||
|         Assert.notNull(authentication.getPrincipal(), "Principal 不会为空"); |         Assert.notNull(authentication.getPrincipal(), "Principal 不会为空"); | ||||||
|         this.createLoginLog(username, logType, LoginResultEnum.SUCCESS); |  | ||||||
|         return (LoginUser) authentication.getPrincipal(); |         return (LoginUser) authentication.getPrincipal(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -107,7 +107,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | |||||||
|         LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); |         LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); | ||||||
|  |  | ||||||
|         // 缓存登陆用户到 Redis 中,返回 sessionId 编号 |         // 缓存登陆用户到 Redis 中,返回 sessionId 编号 | ||||||
|         return userSessionService.createUserSession(loginUser, userIp, userAgent); |         return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_USERNAME, userIp, userAgent); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void verifyCaptcha(AuthLoginReqVO reqVO) { |     private void verifyCaptcha(AuthLoginReqVO reqVO) { | ||||||
| @@ -155,9 +155,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | |||||||
|             this.createLoginLog(username, logTypeEnum, LoginResultEnum.UNKNOWN_ERROR); |             this.createLoginLog(username, logTypeEnum, LoginResultEnum.UNKNOWN_ERROR); | ||||||
|             throw exception(AUTH_LOGIN_FAIL_UNKNOWN); |             throw exception(AUTH_LOGIN_FAIL_UNKNOWN); | ||||||
|         } |         } | ||||||
|         // 登录成功的日志 |  | ||||||
|         Assert.notNull(authentication.getPrincipal(), "Principal 不会为空"); |         Assert.notNull(authentication.getPrincipal(), "Principal 不会为空"); | ||||||
|         this.createLoginLog(username, logTypeEnum, LoginResultEnum.SUCCESS); |  | ||||||
|         return (LoginUser) authentication.getPrincipal(); |         return (LoginUser) authentication.getPrincipal(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -207,7 +205,6 @@ public class AdminAuthServiceImpl implements AdminAuthService { | |||||||
|         if (user == null) { |         if (user == null) { | ||||||
|             throw exception(USER_NOT_EXISTS); |             throw exception(USER_NOT_EXISTS); | ||||||
|         } |         } | ||||||
|         this.createLoginLog(user.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL, LoginResultEnum.SUCCESS); |  | ||||||
|  |  | ||||||
|         // 创建 LoginUser 对象 |         // 创建 LoginUser 对象 | ||||||
|         LoginUser loginUser = this.buildLoginUser(user); |         LoginUser loginUser = this.buildLoginUser(user); | ||||||
| @@ -216,7 +213,7 @@ public class AdminAuthServiceImpl implements AdminAuthService { | |||||||
|         socialUserService.bindSocialUser(AuthConvert.INSTANCE.convert(loginUser.getId(), getUserType().getValue(), reqVO)); |         socialUserService.bindSocialUser(AuthConvert.INSTANCE.convert(loginUser.getId(), getUserType().getValue(), reqVO)); | ||||||
|  |  | ||||||
|         // 缓存登录用户到 Redis 中,返回 sessionId 编号 |         // 缓存登录用户到 Redis 中,返回 sessionId 编号 | ||||||
|         return userSessionService.createUserSession(loginUser, userIp, userAgent); |         return createUserSessionAfterLoginSuccess(loginUser, LoginLogTypeEnum.LOGIN_SOCIAL, userIp, userAgent); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -231,6 +228,13 @@ public class AdminAuthServiceImpl implements AdminAuthService { | |||||||
|         // 绑定社交用户(新增) |         // 绑定社交用户(新增) | ||||||
|         socialUserService.bindSocialUser(AuthConvert.INSTANCE.convert(loginUser.getId(), getUserType().getValue(), reqVO)); |         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 编号 |         // 缓存登录用户到 Redis 中,返回 sessionId 编号 | ||||||
|         return userSessionService.createUserSession(loginUser, userIp, userAgent); |         return userSessionService.createUserSession(loginUser, userIp, userAgent); | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 YunaiV
					YunaiV