1. 新增测试环境的登录测试接口
This commit is contained in:
13
pom.xml
13
pom.xml
@@ -16,20 +16,15 @@
|
||||
<!-- 各种 module 拓展 -->
|
||||
<module>yudao-module-system</module>
|
||||
<module>yudao-module-infra</module>
|
||||
<!-- <module>yudao-module-member</module>-->
|
||||
<module>yudao-module-bpm</module>
|
||||
<!-- <module>yudao-module-pay</module>-->
|
||||
<module>yudao-module-report</module>
|
||||
<module>yudao-module-mp</module>
|
||||
<!-- <module>yudao-module-mall</module>-->
|
||||
<!-- <module>yudao-module-crm</module>-->
|
||||
<!-- <module>yudao-module-erp</module>-->
|
||||
<!-- <module>yudao-module-mp</module>-->
|
||||
<!-- <module>yudao-module-ai</module>-->
|
||||
<module>yudao-module-template</module>
|
||||
<!-- <module>yudao-module-iot</module>-->
|
||||
<module>yudao-module-databus</module>
|
||||
<module>yudao-module-rule</module>
|
||||
<module>yudao-module-html2pdf</module>
|
||||
<!-- <module>yudao-module-databus</module>-->
|
||||
<!-- <module>yudao-module-rule</module>-->
|
||||
<!-- <module>yudao-module-html2pdf</module>-->
|
||||
</modules>
|
||||
|
||||
<name>${project.artifactId}</name>
|
||||
|
||||
@@ -17,6 +17,7 @@ public interface ErrorCodeConstants {
|
||||
ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1_002_000_005, "未绑定账号,需要进行绑定");
|
||||
ErrorCode AUTH_MOBILE_NOT_EXISTS = new ErrorCode(1_002_000_007, "手机号不存在");
|
||||
ErrorCode AUTH_REGISTER_CAPTCHA_CODE_ERROR = new ErrorCode(1_002_000_008, "验证码不正确,原因:{}");
|
||||
ErrorCode AUTH_TEST_LOGIN_NOT_ALLOWED = new ErrorCode(1_002_000_009, "测试登录接口仅在测试环境和本地开发环境下可用");
|
||||
|
||||
// ========== 菜单模块 1-002-001-000 ==========
|
||||
ErrorCode MENU_NAME_DUPLICATE = new ErrorCode(1_002_001_000, "已经存在该名字的菜单");
|
||||
|
||||
@@ -71,6 +71,14 @@ public class AuthController {
|
||||
return success(authService.login(reqVO));
|
||||
}
|
||||
|
||||
@PostMapping("/test-login")
|
||||
@PermitAll
|
||||
@Operation(summary = "测试登录(无验证码校验)", description = "仅用于测试环境和本地开发,生产环境不可用")
|
||||
@TenantIgnore
|
||||
public CommonResult<AuthLoginRespVO> testLogin(@RequestBody @Valid AuthTestLoginReqVO reqVO) {
|
||||
return success(authService.testLogin(reqVO));
|
||||
}
|
||||
|
||||
@PostMapping("/logout")
|
||||
@PermitAll
|
||||
@Operation(summary = "登出系统")
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package cn.iocoder.yudao.module.system.controller.admin.auth.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
@Schema(description = "管理后台 - 测试登录 Request VO(无验证码校验,仅测试环境和本地开发可用)")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class AuthTestLoginReqVO {
|
||||
|
||||
@Schema(description = "账号", requiredMode = Schema.RequiredMode.REQUIRED, example = "yudaoyuanma")
|
||||
@NotEmpty(message = "登录账号不能为空")
|
||||
@Length(min = 4, max = 16, message = "账号长度为 4-16 位")
|
||||
@Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母")
|
||||
private String username;
|
||||
|
||||
@Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "buzhidao")
|
||||
@NotEmpty(message = "密码不能为空")
|
||||
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
|
||||
private String password;
|
||||
|
||||
}
|
||||
@@ -30,6 +30,15 @@ public interface AdminAuthService {
|
||||
*/
|
||||
AuthLoginRespVO login(@Valid AuthLoginReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 测试登录(无验证码校验)
|
||||
* 仅用于测试环境和本地开发,生产环境不可用
|
||||
*
|
||||
* @param reqVO 登录信息
|
||||
* @return 登录结果
|
||||
*/
|
||||
AuthLoginRespVO testLogin(@Valid AuthTestLoginReqVO reqVO);
|
||||
|
||||
/**
|
||||
* 基于 token 退出登录
|
||||
*
|
||||
|
||||
@@ -78,6 +78,13 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
@Setter // 为了单测:开启或者关闭验证码
|
||||
private Boolean captchaEnable;
|
||||
|
||||
/**
|
||||
* 测试登录的开关,默认为 false
|
||||
* 仅在测试环境(test)和本地开发环境(local/dev)下允许
|
||||
*/
|
||||
@Value("${spring.profiles.active:}")
|
||||
private String activeProfile;
|
||||
|
||||
@Override
|
||||
public AdminUserDO authenticate(String username, String password) {
|
||||
final LoginLogTypeEnum logTypeEnum = LoginLogTypeEnum.LOGIN_USERNAME;
|
||||
@@ -116,6 +123,18 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
return createTokenAfterLoginSuccess(user.getId(), reqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthLoginRespVO testLogin(AuthTestLoginReqVO reqVO) {
|
||||
// 检查是否为测试环境或本地开发环境
|
||||
validateTestEnvironment();
|
||||
|
||||
// 使用账号密码,进行登录(跳过验证码校验)
|
||||
AdminUserDO user = authenticate(reqVO.getUsername(), reqVO.getPassword());
|
||||
|
||||
// 创建 Token 令牌,记录登录日志
|
||||
return createTokenAfterLoginSuccess(user.getId(), reqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSmsCode(AuthSmsSendReqVO reqVO) {
|
||||
// 如果是重置密码场景,需要校验图形验证码是否正确
|
||||
@@ -307,4 +326,23 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
|
||||
userService.updateUserPassword(userByMobile.getId(), reqVO.getPassword());
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证是否为测试环境或本地开发环境
|
||||
* 仅在 test、local、dev 环境下允许使用测试登录接口
|
||||
*/
|
||||
private void validateTestEnvironment() {
|
||||
if (StringUtils.isBlank(activeProfile)) {
|
||||
throw exception(AUTH_TEST_LOGIN_NOT_ALLOWED);
|
||||
}
|
||||
|
||||
String profile = activeProfile.toLowerCase();
|
||||
boolean isTestEnvironment = profile.contains("test") ||
|
||||
profile.contains("local") ||
|
||||
profile.contains("dev");
|
||||
|
||||
if (!isTestEnvironment) {
|
||||
throw exception(AUTH_TEST_LOGIN_NOT_ALLOWED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user