| 
							
							
							
						 |  |  | @@ -1,18 +1,12 @@ | 
		
	
		
			
				|  |  |  |  | package cn.iocoder.yudao.adminserver.modules.system.service.auth.impl; | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  | import cn.hutool.core.collection.CollUtil; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAuthLoginReqVO; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAuthSocialLogin2ReqVO; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAuthSocialLoginReqVO; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysSocialUserMapper; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.framework.security.core.LoginUser; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.controller.auth.vo.auth.SysAuthLoginReqVO; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.controller.logger.vo.loginlog.SysLoginLogCreateReqVO; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.convert.auth.SysAuthConvert; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.user.SysUserDO; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginLogTypeEnum; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.enums.logger.SysLoginResultEnum; | 
		
	
	
		
			
				
					
					|  |  |  | @@ -21,8 +15,13 @@ import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysUserSessionSe | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.service.common.SysCaptchaService; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.service.logger.SysLoginLogService; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialService; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; | 
		
	
		
			
				|  |  |  |  | import cn.iocoder.yudao.framework.security.core.LoginUser; | 
		
	
		
			
				|  |  |  |  | import lombok.extern.slf4j.Slf4j; | 
		
	
		
			
				|  |  |  |  | import me.zhyd.oauth.model.AuthUser; | 
		
	
		
			
				|  |  |  |  | import org.springframework.context.annotation.Lazy; | 
		
	
	
		
			
				
					
					|  |  |  | @@ -41,8 +40,8 @@ import javax.annotation.Resource; | 
		
	
		
			
				|  |  |  |  | import java.util.List; | 
		
	
		
			
				|  |  |  |  | import java.util.Set; | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 
		
	
		
			
				|  |  |  |  | import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.*; | 
		
	
		
			
				|  |  |  |  | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 
		
	
		
			
				|  |  |  |  | import static java.util.Collections.singleton; | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  | /** | 
		
	
	
		
			
				
					
					|  |  |  | @@ -71,9 +70,6 @@ public class SysAuthServiceImpl implements SysAuthService { | 
		
	
		
			
				|  |  |  |  |     @Resource | 
		
	
		
			
				|  |  |  |  |     private SysSocialService socialService; | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |     @Resource | 
		
	
		
			
				|  |  |  |  |     private SysSocialUserMapper socialUserMapper; | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |     @Override | 
		
	
		
			
				|  |  |  |  |     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { | 
		
	
		
			
				|  |  |  |  |         // 获取 username 对应的 SysUserDO | 
		
	
	
		
			
				
					
					|  |  |  | @@ -92,6 +88,8 @@ public class SysAuthServiceImpl implements SysAuthService { | 
		
	
		
			
				|  |  |  |  |         if (user == null) { | 
		
	
		
			
				|  |  |  |  |             throw new UsernameNotFoundException(String.valueOf(userId)); | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         this.createLoginLog(user.getUsername(), SysLoginLogTypeEnum.LOGIN_MOCK, SysLoginResultEnum.SUCCESS); | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |         // 创建 LoginUser 对象 | 
		
	
		
			
				|  |  |  |  |         LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user); | 
		
	
		
			
				|  |  |  |  |         loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 | 
		
	
	
		
			
				
					
					|  |  |  | @@ -112,24 +110,26 @@ public class SysAuthServiceImpl implements SysAuthService { | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |     private void verifyCaptcha(String username, String captchaUUID, String captchaCode) { | 
		
	
		
			
				|  |  |  |  |         final SysLoginLogTypeEnum logTypeEnum = SysLoginLogTypeEnum.LOGIN_USERNAME; | 
		
	
		
			
				|  |  |  |  |         String code = captchaService.getCaptchaCode(captchaUUID); | 
		
	
		
			
				|  |  |  |  |         // 验证码不存在 | 
		
	
		
			
				|  |  |  |  |         if (code == null) { | 
		
	
		
			
				|  |  |  |  |             // 创建登陆失败日志(验证码不存在) | 
		
	
		
			
				|  |  |  |  |             this.createLoginLog(username, SysLoginResultEnum.CAPTCHA_NOT_FOUND); | 
		
	
		
			
				|  |  |  |  |             throw ServiceExceptionUtil.exception(AUTH_LOGIN_CAPTCHA_NOT_FOUND); | 
		
	
		
			
				|  |  |  |  |             this.createLoginLog(username, logTypeEnum, SysLoginResultEnum.CAPTCHA_NOT_FOUND); | 
		
	
		
			
				|  |  |  |  |             throw exception(AUTH_LOGIN_CAPTCHA_NOT_FOUND); | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         // 验证码不正确 | 
		
	
		
			
				|  |  |  |  |         if (!code.equals(captchaCode)) { | 
		
	
		
			
				|  |  |  |  |             // 创建登陆失败日志(验证码不正确) | 
		
	
		
			
				|  |  |  |  |             this.createLoginLog(username, SysLoginResultEnum.CAPTCHA_CODE_ERROR); | 
		
	
		
			
				|  |  |  |  |             throw ServiceExceptionUtil.exception(AUTH_LOGIN_CAPTCHA_CODE_ERROR); | 
		
	
		
			
				|  |  |  |  |             this.createLoginLog(username, logTypeEnum, SysLoginResultEnum.CAPTCHA_CODE_ERROR); | 
		
	
		
			
				|  |  |  |  |             throw exception(AUTH_LOGIN_CAPTCHA_CODE_ERROR); | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         // 正确,所以要删除下验证码 | 
		
	
		
			
				|  |  |  |  |         captchaService.deleteCaptchaCode(captchaUUID); | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |     private LoginUser login0(String username, String password) { | 
		
	
		
			
				|  |  |  |  |         final SysLoginLogTypeEnum logTypeEnum = SysLoginLogTypeEnum.LOGIN_USERNAME; | 
		
	
		
			
				|  |  |  |  |         // 用户验证 | 
		
	
		
			
				|  |  |  |  |         Authentication authentication; | 
		
	
		
			
				|  |  |  |  |         try { | 
		
	
	
		
			
				
					
					|  |  |  | @@ -137,25 +137,25 @@ public class SysAuthServiceImpl implements SysAuthService { | 
		
	
		
			
				|  |  |  |  |             // 在其内部,会调用到 loadUserByUsername 方法,获取 User 信息 | 
		
	
		
			
				|  |  |  |  |             authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password)); | 
		
	
		
			
				|  |  |  |  |         } catch (BadCredentialsException badCredentialsException) { | 
		
	
		
			
				|  |  |  |  |             this.createLoginLog(username, SysLoginResultEnum.BAD_CREDENTIALS); | 
		
	
		
			
				|  |  |  |  |             this.createLoginLog(username, logTypeEnum, SysLoginResultEnum.BAD_CREDENTIALS); | 
		
	
		
			
				|  |  |  |  |             throw exception(AUTH_LOGIN_BAD_CREDENTIALS); | 
		
	
		
			
				|  |  |  |  |         } catch (DisabledException disabledException) { | 
		
	
		
			
				|  |  |  |  |             this.createLoginLog(username, SysLoginResultEnum.USER_DISABLED); | 
		
	
		
			
				|  |  |  |  |             this.createLoginLog(username, logTypeEnum, SysLoginResultEnum.USER_DISABLED); | 
		
	
		
			
				|  |  |  |  |             throw exception(AUTH_LOGIN_USER_DISABLED); | 
		
	
		
			
				|  |  |  |  |         } catch (AuthenticationException authenticationException) { | 
		
	
		
			
				|  |  |  |  |             log.error("[login0][username({}) 发生未知异常]", username, authenticationException); | 
		
	
		
			
				|  |  |  |  |             this.createLoginLog(username, SysLoginResultEnum.UNKNOWN_ERROR); | 
		
	
		
			
				|  |  |  |  |             this.createLoginLog(username, logTypeEnum, SysLoginResultEnum.UNKNOWN_ERROR); | 
		
	
		
			
				|  |  |  |  |             throw exception(AUTH_LOGIN_FAIL_UNKNOWN); | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         // 登陆成功 | 
		
	
		
			
				|  |  |  |  |         // 登陆成功的日志 | 
		
	
		
			
				|  |  |  |  |         Assert.notNull(authentication.getPrincipal(), "Principal 不会为空"); | 
		
	
		
			
				|  |  |  |  |         this.createLoginLog(username, SysLoginResultEnum.SUCCESS); | 
		
	
		
			
				|  |  |  |  |         this.createLoginLog(username, logTypeEnum, SysLoginResultEnum.SUCCESS); | 
		
	
		
			
				|  |  |  |  |         return (LoginUser) authentication.getPrincipal(); | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |     private void createLoginLog(String username, SysLoginResultEnum loginResult) { | 
		
	
		
			
				|  |  |  |  |     private void createLoginLog(String username, SysLoginLogTypeEnum logTypeEnum, SysLoginResultEnum loginResult) { | 
		
	
		
			
				|  |  |  |  |         SysLoginLogCreateReqVO reqVO = new SysLoginLogCreateReqVO(); | 
		
	
		
			
				|  |  |  |  |         reqVO.setLogType(SysLoginLogTypeEnum.LOGIN_USERNAME.getType()); | 
		
	
		
			
				|  |  |  |  |         reqVO.setLogType(logTypeEnum.getType()); | 
		
	
		
			
				|  |  |  |  |         reqVO.setTraceId(TracerUtils.getTraceId()); | 
		
	
		
			
				|  |  |  |  |         reqVO.setUsername(username); | 
		
	
		
			
				|  |  |  |  |         reqVO.setUserAgent(ServletUtils.getUserAgent()); | 
		
	
	
		
			
				
					
					|  |  |  | @@ -187,11 +187,14 @@ public class SysAuthServiceImpl implements SysAuthService { | 
		
	
		
			
				|  |  |  |  |             throw exception(AUTH_THIRD_LOGIN_NOT_BIND); | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |         // 使用账号密码,进行登陆。 | 
		
	
		
			
				|  |  |  |  |         // 自动登陆 | 
		
	
		
			
				|  |  |  |  |         SysUserDO user = userService.getUser(socialUsers.get(0).getUserId()); | 
		
	
		
			
				|  |  |  |  |         if (user == null) { | 
		
	
		
			
				|  |  |  |  |             throw exception(USER_NOT_EXISTS); | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |         this.createLoginLog(user.getUsername(), SysLoginLogTypeEnum.LOGIN_SOCIAL, SysLoginResultEnum.SUCCESS); | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |         // 创建 LoginUser 对象 | 
		
	
		
			
				|  |  |  |  |         LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user); | 
		
	
		
			
				|  |  |  |  |         // TODO 芋艿:需要改造下,增加各种登陆方式 | 
		
	
		
			
				|  |  |  |  |         loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 | 
		
	
	
		
			
				
					
					|  |  |  | @@ -210,7 +213,7 @@ public class SysAuthServiceImpl implements SysAuthService { | 
		
	
		
			
				|  |  |  |  |         Assert.notNull(authUser, "授权用户不为空"); | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |         // 使用账号密码,进行登陆。 | 
		
	
		
			
				|  |  |  |  |         LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); // TODO 芋艿:需要改造下,增加各种登陆方式 | 
		
	
		
			
				|  |  |  |  |         LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); | 
		
	
		
			
				|  |  |  |  |         loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 | 
		
	
		
			
				|  |  |  |  |  | 
		
	
		
			
				|  |  |  |  |         // 绑定社交用户(新增) | 
		
	
	
		
			
				
					
					|  |  |  |   |