mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-13 21:32:07 +08:00
1、认证auth service/controller单测完善;2、新增mes系统jwt token创建接口和相关配置;
This commit is contained in:
+5
-48
@@ -3,7 +3,6 @@ package com.cf.imes.module.system.controller.admin.auth;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.cf.imes.framework.common.enums.CommonStatusEnum;
|
||||
import com.cf.imes.framework.common.enums.UserTypeEnum;
|
||||
import com.cf.imes.framework.common.exception.ServiceException;
|
||||
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
|
||||
import com.cf.imes.framework.common.pojo.CommonResult;
|
||||
@@ -18,7 +17,6 @@ import com.cf.imes.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginSmsCheckReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthPermissionInfoRespVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSocialLoginReqVO;
|
||||
import com.cf.imes.module.system.convert.auth.AuthConvert;
|
||||
import com.cf.imes.module.system.dal.dataobject.organ.OrganizationDO;
|
||||
import com.cf.imes.module.system.dal.dataobject.permission.MenuDO;
|
||||
@@ -31,14 +29,11 @@ import com.cf.imes.module.system.service.permission.MenuService;
|
||||
import com.cf.imes.module.system.service.permission.PermissionService;
|
||||
import com.cf.imes.module.system.service.permission.RoleService;
|
||||
import com.cf.imes.module.system.service.sms.SmsCodeService;
|
||||
import com.cf.imes.module.system.service.social.SocialClientService;
|
||||
import com.cf.imes.module.system.service.user.AdminUserService;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginRespVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSmsLoginReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSmsSendReqVO;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.Parameters;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -76,8 +71,6 @@ public class AuthController {
|
||||
private MenuService menuService;
|
||||
@Resource
|
||||
private PermissionService permissionService;
|
||||
@Resource
|
||||
private SocialClientService socialClientService;
|
||||
|
||||
@Resource
|
||||
private SecurityProperties securityProperties;
|
||||
@@ -138,15 +131,6 @@ public class AuthController {
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@PostMapping("/refresh-token")
|
||||
@PermitAll
|
||||
@Operation(summary = "刷新令牌")
|
||||
@Parameter(name = "refreshToken", description = "刷新令牌", required = true)
|
||||
@OperateLog(enable = false) // 避免 Post 请求被记录操作日志
|
||||
public CommonResult<AuthLoginRespVO> refreshToken(@RequestParam("refreshToken") String refreshToken) {
|
||||
return success(authService.refreshToken(refreshToken));
|
||||
}
|
||||
|
||||
@GetMapping("/get-permission-info")
|
||||
@Operation(summary = "获取登录用户的权限信息")
|
||||
public CommonResult<AuthPermissionInfoRespVO> getPermissionInfo(HttpServletRequest request) {
|
||||
@@ -158,7 +142,7 @@ public class AuthController {
|
||||
// 1.1 获得用户信息
|
||||
AdminUserDO user = userService.getUser(userId);
|
||||
if (user == null) {
|
||||
return null;
|
||||
return success(null);
|
||||
}
|
||||
// 获取机构的有效期
|
||||
OrganizationDO organizationDO = organService.validOrgan(user.getOrganId());
|
||||
@@ -192,14 +176,6 @@ public class AuthController {
|
||||
|
||||
// ========== 短信登录相关 ==========
|
||||
|
||||
@PostMapping("/sms-login")
|
||||
@PermitAll
|
||||
@Operation(summary = "使用短信验证码登录")
|
||||
@OperateLog(enable = false) // 避免 Post 请求被记录操作日志
|
||||
public CommonResult<AuthLoginRespVO> smsLogin(@RequestBody @Valid AuthSmsLoginReqVO reqVO) {
|
||||
return success(authService.smsLogin(reqVO));
|
||||
}
|
||||
|
||||
@PostMapping("/send-sms-code")
|
||||
@PermitAll
|
||||
@Operation(summary = "发送手机验证码")
|
||||
@@ -221,28 +197,9 @@ public class AuthController {
|
||||
return success(true);
|
||||
}
|
||||
|
||||
// ========== 社交登录相关 ==========
|
||||
|
||||
@GetMapping("/social-auth-redirect")
|
||||
@PermitAll
|
||||
@Operation(summary = "社交授权的跳转")
|
||||
@Parameters({
|
||||
@Parameter(name = "type", description = "社交类型", required = true),
|
||||
@Parameter(name = "redirectUri", description = "回调路径")
|
||||
})
|
||||
public CommonResult<String> socialLogin(@RequestParam("type") Integer type,
|
||||
@RequestParam("redirectUri") String redirectUri) {
|
||||
return success(socialClientService.getAuthorizeUrl(
|
||||
type, UserTypeEnum.ADMIN.getValue(), redirectUri));
|
||||
@GetMapping("/mes/token")
|
||||
@Operation(summary = "创建mes系统jwt token")
|
||||
public CommonResult<String> getMesJwtToken() {
|
||||
return success(authService.getMesJwtToken());
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/social-login")
|
||||
@PermitAll
|
||||
@Operation(summary = "社交快捷登录,使用 code 授权码", description = "适合未登录的用户,但是社交账号已绑定用户")
|
||||
@OperateLog(enable = false) // 避免 Post 请求被记录操作日志
|
||||
public CommonResult<AuthLoginRespVO> socialQuickLogin(@RequestBody @Valid AuthSocialLoginReqVO reqVO) {
|
||||
return success(authService.socialLogin(reqVO));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-27
@@ -1,20 +1,16 @@
|
||||
package com.cf.imes.module.system.controller.admin.auth.vo;
|
||||
|
||||
import com.cf.imes.framework.common.validation.InEnum;
|
||||
import com.cf.imes.module.system.enums.social.SocialTypeEnum;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import jakarta.validation.constraints.AssertTrue;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
|
||||
@Schema(description = "管理后台 - 账号密码登录 Request VO,如果登录并绑定社交用户,需要传递 social 开头的参数")
|
||||
@Schema(description = "管理后台 - 账号密码登录 Request VO")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@@ -39,30 +35,8 @@ public class AuthLoginReqVO {
|
||||
@NotEmpty(message = "验证码不能为空", groups = CodeEnableGroup.class)
|
||||
private String captchaVerification;
|
||||
|
||||
// ========== 绑定社交登录时,需要传递如下参数 ==========
|
||||
|
||||
@Schema(description = "社交平台的类型,参见 SocialTypeEnum 枚举值", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
|
||||
@InEnum(SocialTypeEnum.class)
|
||||
private Integer socialType;
|
||||
|
||||
@Schema(description = "授权码", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||
private String socialCode;
|
||||
|
||||
@Schema(description = "state", requiredMode = Schema.RequiredMode.REQUIRED, example = "9b2ffbc1-7425-4155-9894-9d5c08541d62")
|
||||
private String socialState;
|
||||
|
||||
/**
|
||||
* 开启验证码的 Group
|
||||
*/
|
||||
public interface CodeEnableGroup {}
|
||||
|
||||
@AssertTrue(message = "授权码不能为空")
|
||||
public boolean isSocialCodeValid() {
|
||||
return socialType == null || StringUtils.isNotEmpty(socialCode);
|
||||
}
|
||||
|
||||
@AssertTrue(message = "授权 state 不能为空")
|
||||
public boolean isSocialState() {
|
||||
return socialType == null || StringUtils.isNotEmpty(socialState);
|
||||
}
|
||||
}
|
||||
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
package com.cf.imes.module.system.controller.admin.tokenconfig.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Getter
|
||||
@Data
|
||||
public class JwtConfig{
|
||||
|
||||
@Value("${jwt.secret}")
|
||||
private String secret;
|
||||
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.cf.imes.module.system.framework.chenfeng.config;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* chenfeng基础配置注册
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2026/1/9 11:53
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableConfigurationProperties(ChenfengProperties.class)
|
||||
public class ChenfengConfiguration {
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.cf.imes.module.system.framework.chenfeng.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
/**
|
||||
* chenfeng属性
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2026/1/9 11:50
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "chenfeng")
|
||||
@Validated
|
||||
@Data
|
||||
public class ChenfengProperties {
|
||||
private Captcha captcha = new Captcha();
|
||||
private Gray gray = new Gray();
|
||||
|
||||
@Data
|
||||
public static class Captcha {
|
||||
/**
|
||||
* 是否开启验证码
|
||||
*/
|
||||
private Boolean enable = true;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Gray {
|
||||
/**
|
||||
* 当前灰度版本
|
||||
*/
|
||||
private String version = "1.0.0";
|
||||
|
||||
/**
|
||||
* 默认版本
|
||||
*/
|
||||
private String defaultVersion = "1.0.0";
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
package com.cf.imes.module.system.framework.chenfeng;
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.cf.imes.module.system.framework.jwt.config;
|
||||
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* jwt配置注册
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2026/1/8 14:47
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableConfigurationProperties(JwtProperties.class)
|
||||
public class JwtConfiguration {
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.cf.imes.module.system.framework.jwt.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* jwt属性
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2026/1/8 14:24
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "jwt")
|
||||
@Validated
|
||||
@Data
|
||||
public class JwtProperties {
|
||||
|
||||
/**
|
||||
* imes密钥
|
||||
*/
|
||||
private String secret;
|
||||
|
||||
/**
|
||||
* mes密钥
|
||||
*/
|
||||
private String mesSecret;
|
||||
|
||||
/**
|
||||
* mes aud:jwt接受方
|
||||
*/
|
||||
private String mesAud;
|
||||
|
||||
/**
|
||||
* 令牌有效时长
|
||||
*/
|
||||
private Duration mesTokenTtl;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
package com.cf.imes.module.system.framework.jwt;
|
||||
+3
-23
@@ -3,9 +3,7 @@ package com.cf.imes.module.system.service.auth;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginRespVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginSmsCheckReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSmsLoginReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSmsSendReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSocialLoginReqVO;
|
||||
import com.cf.imes.module.system.dal.dataobject.user.AdminUserDO;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
@@ -66,27 +64,9 @@ public interface AdminAuthService {
|
||||
void sendSmsCode(AuthSmsSendReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 短信登录
|
||||
* 获取 mes系统 JWT 令牌
|
||||
*
|
||||
* @param reqVO 登录信息
|
||||
* @return 登录结果
|
||||
* @return 令牌
|
||||
*/
|
||||
AuthLoginRespVO smsLogin(AuthSmsLoginReqVO reqVO) ;
|
||||
|
||||
/**
|
||||
* 社交快捷登录,使用 code 授权码
|
||||
*
|
||||
* @param reqVO 登录信息
|
||||
* @return 登录结果
|
||||
*/
|
||||
AuthLoginRespVO socialLogin(@Valid AuthSocialLoginReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 刷新访问令牌
|
||||
*
|
||||
* @param refreshToken 刷新令牌
|
||||
* @return 登录结果
|
||||
*/
|
||||
AuthLoginRespVO refreshToken(String refreshToken);
|
||||
|
||||
String getMesJwtToken();
|
||||
}
|
||||
|
||||
+60
-94
@@ -1,11 +1,13 @@
|
||||
package com.cf.imes.module.system.service.auth;
|
||||
|
||||
import cn.hutool.core.util.BooleanUtil;
|
||||
import com.anji.captcha.model.common.ResponseModel;
|
||||
import com.anji.captcha.model.vo.CaptchaVO;
|
||||
import com.anji.captcha.service.CaptchaService;
|
||||
import com.cf.imes.framework.common.enums.CommonStatusEnum;
|
||||
import com.cf.imes.framework.common.enums.UserTypeEnum;
|
||||
import com.cf.imes.framework.common.exception.ServiceException;
|
||||
import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants;
|
||||
import com.cf.imes.framework.common.util.monitor.TracerUtils;
|
||||
import com.cf.imes.framework.common.util.servlet.ServletUtils;
|
||||
import com.cf.imes.framework.common.util.validation.ValidationUtils;
|
||||
@@ -13,17 +15,15 @@ import com.cf.imes.framework.ip.core.service.IPQueryService;
|
||||
import com.cf.imes.framework.ip.core.service.dto.IPQueryDataRespDTO;
|
||||
import com.cf.imes.framework.organ.core.context.OrganContextHolder;
|
||||
import com.cf.imes.framework.organ.core.service.OrganFrameworkService;
|
||||
import com.cf.imes.framework.security.core.LoginUser;
|
||||
import com.cf.imes.framework.security.core.service.SecurityFrameworkService;
|
||||
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
|
||||
import com.cf.imes.module.system.api.logger.dto.LoginLogCreateReqDTO;
|
||||
import com.cf.imes.module.system.api.sms.SmsCodeApi;
|
||||
import com.cf.imes.module.system.api.social.dto.SocialUserRespDTO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginRespVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginSmsCheckReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSmsLoginReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSmsSendReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSocialLoginReqVO;
|
||||
import com.cf.imes.module.system.convert.auth.AuthConvert;
|
||||
import com.cf.imes.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
||||
import com.cf.imes.module.system.dal.dataobject.organ.OrganizationDO;
|
||||
@@ -31,29 +31,35 @@ import com.cf.imes.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import com.cf.imes.module.system.enums.logger.LoginLogTypeEnum;
|
||||
import com.cf.imes.module.system.enums.logger.LoginResultEnum;
|
||||
import com.cf.imes.module.system.enums.oauth2.OAuth2ClientConstants;
|
||||
import com.cf.imes.module.system.enums.sms.SmsSceneEnum;
|
||||
import com.cf.imes.module.system.framework.chenfeng.config.ChenfengProperties;
|
||||
import com.cf.imes.module.system.framework.jwt.config.JwtProperties;
|
||||
import com.cf.imes.module.system.framework.product.ProductProperties;
|
||||
import com.cf.imes.module.system.service.dept.DeptService;
|
||||
import com.cf.imes.module.system.service.logger.LoginLogService;
|
||||
import com.cf.imes.module.system.service.member.MemberService;
|
||||
import com.cf.imes.module.system.service.oauth2.OAuth2TokenService;
|
||||
import com.cf.imes.module.system.service.sms.SmsCodeService;
|
||||
import com.cf.imes.module.system.service.social.SocialUserService;
|
||||
import com.cf.imes.module.system.service.organ.OrganService;
|
||||
import com.cf.imes.module.system.service.user.AdminUserService;
|
||||
import com.cf.imes.module.system.enums.ErrorCodeConstants;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.Header;
|
||||
import io.jsonwebtoken.JwsHeader;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.Validator;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
|
||||
import static com.cf.imes.framework.common.util.servlet.ServletUtils.getClientIP;
|
||||
@@ -66,7 +72,6 @@ import static com.cf.imes.module.system.enums.ErrorCodeConstants.ORGAN_DATA_CODE
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
@RefreshScope
|
||||
public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
|
||||
@Resource
|
||||
@@ -76,10 +81,6 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
@Resource
|
||||
private OAuth2TokenService oauth2TokenService;
|
||||
@Resource
|
||||
private SocialUserService socialUserService;
|
||||
@Resource
|
||||
private MemberService memberService;
|
||||
@Resource
|
||||
private Validator validator;
|
||||
@Resource
|
||||
private CaptchaService captchaService;
|
||||
@@ -108,17 +109,11 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
@Resource
|
||||
private ProductProperties productProperties;
|
||||
|
||||
/**
|
||||
* 验证码的开关,默认为 true
|
||||
*/
|
||||
@Value("${chenfeng.captcha.enable:true}")
|
||||
private Boolean captchaEnable;
|
||||
@Resource
|
||||
private JwtProperties jwtProperties;
|
||||
|
||||
@Value("${chenfeng.gray.version:1.0.0}")
|
||||
private String grayVersion;
|
||||
|
||||
@Value("${chenfeng.gray.default-version:1.0.0}")
|
||||
private String grayDefaultVersion;
|
||||
@Resource
|
||||
private ChenfengProperties chenfengProperties;
|
||||
|
||||
@Override
|
||||
public AdminUserDO authenticate(AuthLoginReqVO reqVO) {
|
||||
@@ -170,10 +165,10 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
if (isSuperAdmin || SecurityFrameworkUtils.isCfOrg(user.getOrganId())) {
|
||||
user.setUserType(isManageEndPoint ? UserTypeEnum.ADMIN : UserTypeEnum.MEMBER);
|
||||
} else {
|
||||
user.setUserType(UserTypeEnum.MEMBER);
|
||||
if (isManageEndPoint) {
|
||||
throw new ServiceException(ErrorCodeConstants.AUTH_MANAGEENDPOINT_LOGIN_PERMISSION_ERROR);
|
||||
}
|
||||
user.setUserType(UserTypeEnum.MEMBER);
|
||||
}
|
||||
|
||||
Long organId = user.getOrganId();
|
||||
@@ -185,10 +180,10 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
}
|
||||
if (CommonStatusEnum.ENABLE.getStatus().equals(organ.getGrayStatus())) {
|
||||
// 机构启用灰度、版本赋值到user
|
||||
user.setGrayVersion(grayVersion);
|
||||
user.setGrayVersion(chenfengProperties.getGray().getVersion());
|
||||
} else {
|
||||
// 未启用灰度的机构需要默认版本号来做网关实例选择
|
||||
user.setGrayDefaultVersion(grayDefaultVersion);
|
||||
user.setGrayDefaultVersion(chenfengProperties.getGray().getDefaultVersion());
|
||||
}
|
||||
// 创建 Token 令牌,记录登录日志
|
||||
return createTokenAfterLoginSuccess(user, reqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME, dataSourceCode);
|
||||
@@ -234,28 +229,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
smsCodeApi.sendSmsCode(AuthConvert.INSTANCE.convert(reqVO).setCreateIp(getClientIP()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthLoginRespVO smsLogin(AuthSmsLoginReqVO reqVO) {
|
||||
// 校验验证码
|
||||
smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.ADMIN_MEMBER_LOGIN.getScene(), getClientIP()));
|
||||
|
||||
// 获得用户信息
|
||||
AdminUserDO user = userService.getUserByMobile(reqVO.getMobile());
|
||||
if (user == null) {
|
||||
throw new ServiceException(ErrorCodeConstants.USER_NOT_EXISTS);
|
||||
}
|
||||
Long organId = user.getOrganId();
|
||||
OrganizationDO organ = organService.getOrgan(organId);
|
||||
String dataSourceCode = organ.getDataSourceCode();
|
||||
if(StringUtils.isBlank(dataSourceCode)) {
|
||||
throw new ServiceException(ORGAN_DATA_CODE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
// 创建 Token 令牌,记录登录日志
|
||||
return createTokenAfterLoginSuccess(user, reqVO.getMobile(), LoginLogTypeEnum.LOGIN_MOBILE, dataSourceCode);
|
||||
}
|
||||
|
||||
private void createLoginLog(Long userId, String username, UserTypeEnum userTypeEnum, Long organId,
|
||||
protected void createLoginLog(Long userId, String username, UserTypeEnum userTypeEnum, Long organId,
|
||||
LoginLogTypeEnum logTypeEnum, LoginResultEnum loginResult) {
|
||||
// 手动控制事务
|
||||
transactionTemplate.executeWithoutResult(status -> {
|
||||
@@ -279,7 +253,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
}
|
||||
loginLogService.createLoginLog(reqDTO);
|
||||
// 更新最后登录时间
|
||||
if (userId != null && Objects.equals(LoginResultEnum.SUCCESS.getResult(), loginResult.getResult())) {
|
||||
if (BooleanUtil.and(userId != null, Objects.equals(LoginResultEnum.SUCCESS.getResult(), loginResult.getResult()))) {
|
||||
userService.updateUserLogin(userId, clientIP);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -289,36 +263,10 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthLoginRespVO socialLogin(AuthSocialLoginReqVO reqVO) {
|
||||
// 使用 code 授权码,进行登录。然后,获得到绑定的用户编号
|
||||
SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(UserTypeEnum.ADMIN.getValue(), reqVO.getType(),
|
||||
reqVO.getCode(), reqVO.getState());
|
||||
if (socialUser == null || socialUser.getUserId() == null) {
|
||||
throw new ServiceException(ErrorCodeConstants.AUTH_THIRD_LOGIN_NOT_BIND);
|
||||
}
|
||||
|
||||
// 获得用户
|
||||
AdminUserDO user = userService.getUser(socialUser.getUserId());
|
||||
if (user == null) {
|
||||
throw new ServiceException(ErrorCodeConstants.USER_NOT_EXISTS);
|
||||
}
|
||||
|
||||
Long organId = user.getOrganId();
|
||||
OrganizationDO organ = organService.getOrgan(organId);
|
||||
String dataSourceCode = organ.getDataSourceCode();
|
||||
if(StringUtils.isBlank(dataSourceCode)) {
|
||||
throw new ServiceException(ORGAN_DATA_CODE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
// 创建 Token 令牌,记录登录日志
|
||||
return createTokenAfterLoginSuccess(user, user.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL, dataSourceCode);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void validateCaptcha(AuthLoginReqVO reqVO) {
|
||||
// 如果验证码关闭,则不进行校验
|
||||
if (!captchaEnable) {
|
||||
if (!chenfengProperties.getCaptcha().getEnable()) {
|
||||
return;
|
||||
}
|
||||
// 校验验证码
|
||||
@@ -352,12 +300,6 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
return AuthConvert.INSTANCE.convert(accessTokenDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthLoginRespVO refreshToken(String refreshToken) {
|
||||
OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.refreshAccessToken(refreshToken, OAuth2ClientConstants.CLIENT_ID_DEFAULT);
|
||||
return AuthConvert.INSTANCE.convert(accessTokenDO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void logout(String token, Integer logType) {
|
||||
// 删除访问令牌
|
||||
@@ -366,18 +308,18 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
return;
|
||||
}
|
||||
// 删除成功,则记录登出日志
|
||||
createLogoutLog(accessTokenDO.getUserId(), accessTokenDO.getUserType(), logType);
|
||||
createLogoutLog(accessTokenDO.getUserId(), accessTokenDO.getNickname(), accessTokenDO.getUserType(), logType);
|
||||
}
|
||||
|
||||
private void createLogoutLog(Long userId, Integer userType, Integer logType) {
|
||||
protected void createLogoutLog(Long userId, String userName, Integer userType, Integer logType) {
|
||||
String clientIP = getClientIP();
|
||||
LoginLogCreateReqDTO reqDTO = new LoginLogCreateReqDTO();
|
||||
reqDTO.setLogType(logType);
|
||||
reqDTO.setTraceId(TracerUtils.getTraceId());
|
||||
reqDTO.setUserId(userId);
|
||||
reqDTO.setUserType(userType);
|
||||
reqDTO.setUsername(getUsername(userId));
|
||||
// 设置登录时通过ip云服务查询到的地域信息
|
||||
reqDTO.setUsername(userName);
|
||||
// 设置登出时通过ip云服务查询到的地域信息
|
||||
if (ipQueryService.serviceEnable()) {
|
||||
IPQueryDataRespDTO ipQueryDataRespDTO = ipQueryService.querySource(clientIP);
|
||||
reqDTO.setRegion(StringUtils.join(ipQueryDataRespDTO.getProv(), ipQueryDataRespDTO.getCity(), ipQueryDataRespDTO.getArea()));
|
||||
@@ -388,16 +330,40 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
loginLogService.createLoginLog(reqDTO);
|
||||
}
|
||||
|
||||
private String getUsername(Long userId) {
|
||||
if (userId == null) {
|
||||
return null;
|
||||
}
|
||||
AdminUserDO user = userService.getUser(userId);
|
||||
return user != null ? user.getUsername() : null;
|
||||
}
|
||||
|
||||
private UserTypeEnum getUserType() {
|
||||
return UserTypeEnum.ADMIN;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getMesJwtToken() {
|
||||
// 当前登录用户
|
||||
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
|
||||
if (loginUser == null) {
|
||||
throw new ServiceException(GlobalErrorCodeConstants.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
// 计算超时时间
|
||||
Instant now = Instant.now();
|
||||
long nbf = now.getEpochSecond();
|
||||
long exp = now.plus(jwtProperties.getMesTokenTtl()).getEpochSecond();
|
||||
|
||||
// 生产token
|
||||
SecretKey secretKey = Keys.hmacShaKeyFor(jwtProperties.getMesSecret().getBytes(StandardCharsets.UTF_8));
|
||||
String aud = jwtProperties.getMesAud();
|
||||
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
|
||||
|
||||
return Jwts.builder()
|
||||
.setHeaderParam(JwsHeader.ALGORITHM, signatureAlgorithm.getValue())
|
||||
.setHeaderParam(JwsHeader.KEY_ID, aud)
|
||||
.setHeaderParam(Header.TYPE, Header.JWT_TYPE)
|
||||
.claim("userId",loginUser.getId())
|
||||
.claim("companyId", loginUser.getOrganId())
|
||||
.claim("role","")
|
||||
.claim(Claims.AUDIENCE, aud)
|
||||
.claim(Claims.NOT_BEFORE, nbf)
|
||||
.claim(Claims.EXPIRATION, exp)
|
||||
.signWith(secretKey, signatureAlgorithm)
|
||||
.compact();
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -33,7 +33,7 @@ import com.cf.imes.module.system.controller.admin.organ.vo.organ.OrganSimpleResp
|
||||
import com.cf.imes.module.system.controller.admin.statistics.vo.ManageOrgTotalStatisticRespVO;
|
||||
import com.cf.imes.module.system.controller.admin.statistics.vo.OrgStatusGroupStatisticsReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.systemconfig.vo.ProcessSchemeConfig;
|
||||
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.JwtConfig;
|
||||
import com.cf.imes.module.system.framework.jwt.config.JwtProperties;
|
||||
import com.cf.imes.module.system.controller.admin.user.vo.user.UserSaveReqVO;
|
||||
import com.cf.imes.module.system.convert.organ.OrganConvert;
|
||||
import com.cf.imes.module.system.dal.dataobject.dict.DictDataDO;
|
||||
@@ -127,7 +127,7 @@ public class OrganServiceImpl implements OrganService {
|
||||
private TokenConfigMapper tokenConfigMapper;
|
||||
|
||||
@Resource
|
||||
private JwtConfig jwtConfig;
|
||||
private JwtProperties jwtProperties;
|
||||
|
||||
@Resource
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
@@ -290,7 +290,7 @@ public class OrganServiceImpl implements OrganService {
|
||||
List<TokenConfigDO> tokenConfigDOS = tokenConfigMapper.selectConfigByOrganId(organId);
|
||||
if(CollUtil.isNotEmpty(tokenConfigDOS)) {
|
||||
tokenConfigDOS.forEach(f -> {
|
||||
f.setAppToken(generateBaseToken(organId, updateReqVO.getOrgAdminMobile(), f.getAppType(), jwtConfig.getSecret()));
|
||||
f.setAppToken(generateBaseToken(organId, updateReqVO.getOrgAdminMobile(), f.getAppType(), jwtProperties.getSecret()));
|
||||
});
|
||||
tokenConfigMapper.updateBatch(tokenConfigDOS);
|
||||
}
|
||||
|
||||
+3
-3
@@ -10,7 +10,7 @@ import com.cf.imes.framework.common.util.Assert.AssertUtils;
|
||||
import com.cf.imes.framework.common.util.date.DateUtils;
|
||||
import com.cf.imes.framework.common.util.date.LocalDateTimeUtils;
|
||||
import com.cf.imes.framework.common.util.object.BeanUtils;
|
||||
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.JwtConfig;
|
||||
import com.cf.imes.module.system.framework.jwt.config.JwtProperties;
|
||||
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.TokenConfigPageReqVO;
|
||||
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.TokenConfigRespVO;
|
||||
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.TokenConfigSaveReqVO;
|
||||
@@ -58,7 +58,7 @@ public class TokenConfigServiceImpl implements TokenConfigService{
|
||||
|
||||
|
||||
@Resource
|
||||
private JwtConfig jwtConfig;
|
||||
private JwtProperties jwtProperties;
|
||||
|
||||
private static final String FIELD_IMES = "imes-";
|
||||
|
||||
@@ -123,7 +123,7 @@ public class TokenConfigServiceImpl implements TokenConfigService{
|
||||
throw new ServiceException(TOKEN_APP_TYPE_EXIST);
|
||||
}
|
||||
|
||||
String token = generateBaseToken(organId, organizationDO.getContactMobile(), reqVO.getAppType(), jwtConfig.getSecret());
|
||||
String token = generateBaseToken(organId, organizationDO.getContactMobile(), reqVO.getAppType(), jwtProperties.getSecret());
|
||||
|
||||
LocalDateTime adjustedTime;
|
||||
|
||||
|
||||
@@ -333,6 +333,9 @@ justauth:
|
||||
# JWT 密钥配置
|
||||
jwt:
|
||||
secret: BjEM0tnL3W5zYLQ6pllol49uYVXe+f66pHyOM/tkGWg=
|
||||
mes-secret: 0d118b98602a332bd966bffa47b294c1
|
||||
mes-aud: iMES
|
||||
mes-token-ttl: 10m
|
||||
|
||||
--- #################### 验证码相关配置 ####################
|
||||
|
||||
|
||||
@@ -354,6 +354,9 @@ justauth:
|
||||
# JWT 密钥配置
|
||||
jwt:
|
||||
secret: BjEM0tnL3W5zYLQ6pllol49uYVXe+f66pHyOM/tkGWg=
|
||||
mes-secret: 0d118b98602a332bd966bffa47b294c1
|
||||
mes-aud: iMES
|
||||
mes-token-ttl: 10m
|
||||
|
||||
--- #################### 验证码相关配置 ####################
|
||||
|
||||
|
||||
Reference in New Issue
Block a user