|
@@ -1,5 +1,6 @@
|
|
|
package cn.iocoder.yudao.module.system.service.auth;
|
|
package cn.iocoder.yudao.module.system.service.auth;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.ObjUtil;
|
|
import cn.hutool.core.util.ObjUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
@@ -15,10 +16,13 @@ import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO;
|
|
|
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
|
|
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
|
|
|
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
|
|
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*;
|
|
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.authentication.vo.AuthenticationPageReqVO;
|
|
|
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
|
|
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.authentication.AuthenticationDO;
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserBindDO;
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserBindDO;
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
|
|
|
|
+import cn.iocoder.yudao.module.system.dal.mysql.authentication.AuthenticationMapper;
|
|
|
import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserBindMapper;
|
|
import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserBindMapper;
|
|
|
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
|
|
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
|
|
|
import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
|
|
import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
|
|
@@ -45,7 +49,11 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
import javax.validation.Validator;
|
|
import javax.validation.Validator;
|
|
|
|
|
+import java.time.Duration;
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
|
|
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
|
|
@@ -87,6 +95,8 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
@Setter // 为了单测:开启或者关闭验证码
|
|
@Setter // 为了单测:开启或者关闭验证码
|
|
|
private Boolean captchaEnable;
|
|
private Boolean captchaEnable;
|
|
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private AuthenticationMapper authenticationMapper;
|
|
|
@Resource
|
|
@Resource
|
|
|
private AdminUserMapper userMapper;
|
|
private AdminUserMapper userMapper;
|
|
|
|
|
|
|
@@ -313,6 +323,32 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public AuthLoginRespVO portalSsoLogin(AuthPortalSsoLoginReqVO reqVO) {
|
|
|
|
|
+ if (StrUtil.isBlank(reqVO.getCode())) {
|
|
|
|
|
+ // 查询不到用户详情 返回 登录失败
|
|
|
|
|
+ throw exception(OAUTH2_CODE_NOT_EXISTS);
|
|
|
|
|
+ }
|
|
|
|
|
+ AuthenticationPageReqVO authReqVO = new AuthenticationPageReqVO();
|
|
|
|
|
+ authReqVO.setRandomCode(reqVO.getCode());
|
|
|
|
|
+ List<AuthenticationDO> authentications = authenticationMapper.selectList(authReqVO);
|
|
|
|
|
+ if (CollUtil.isNotEmpty(authentications)) {
|
|
|
|
|
+ AuthenticationDO auth = authentications.get(0);
|
|
|
|
|
+ // 如果随机码的创建时间 距离现在已经超过5分钟 则失效
|
|
|
|
|
+ LocalDateTime createTime = auth.getCreateTime();
|
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
+ // 计算时间差,如果超过5分钟则返回null
|
|
|
|
|
+ Duration duration = Duration.between(createTime, now);
|
|
|
|
|
+ if (duration.toMinutes() > 5) {
|
|
|
|
|
+ // 随机码已失效,不再生成token
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 创建 Token 令牌,记录登录日志 portal
|
|
|
|
|
+ return createTokenAfterLoginSuccess(auth.getUserid(), auth.getJobNumber(), LoginLogTypeEnum.LOGIN_PORTAL);
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@VisibleForTesting
|
|
@VisibleForTesting
|
|
|
void validateCaptcha(AuthLoginReqVO reqVO) {
|
|
void validateCaptcha(AuthLoginReqVO reqVO) {
|
|
|
ResponseModel response = doValidateCaptcha(reqVO);
|
|
ResponseModel response = doValidateCaptcha(reqVO);
|
|
@@ -429,4 +465,22 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
|
|
|
|
|
userService.updateUserPassword(userByMobile.getId(), reqVO.getPassword());
|
|
userService.updateUserPassword(userByMobile.getId(), reqVO.getPassword());
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String ssoGetToken(AuthPortalLoginReqVO reqVO) {
|
|
|
|
|
+ // 如果 当前用户表中存在 username 则返回随机 uuid 并存储到表 system_authentication
|
|
|
|
|
+ // 查询当前用户表中 用户 是否存在
|
|
|
|
|
+ AdminUserDO user = userMapper.selectByUsername(reqVO.getUsername());
|
|
|
|
|
+ if (ObjUtil.isEmpty(user)) {
|
|
|
|
|
+ throw exception(USER_NOT_EXISTS);
|
|
|
|
|
+ }
|
|
|
|
|
+ // 这里不生成pms令牌 先返回随机code
|
|
|
|
|
+ String randomCode = UUID.randomUUID().toString();
|
|
|
|
|
+ AuthenticationDO authentication = new AuthenticationDO();
|
|
|
|
|
+ authentication.setUserid(user.getId());
|
|
|
|
|
+ authentication.setJobNumber(reqVO.getUsername());
|
|
|
|
|
+ authentication.setRandomCode(randomCode);
|
|
|
|
|
+ authenticationMapper.insert(authentication);
|
|
|
|
|
+ return randomCode;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|