1. 新增测试环境的登录测试接口

This commit is contained in:
chenbowen
2025-09-16 09:10:48 +08:00
parent bea39f9b57
commit e1a14564d0
6 changed files with 90 additions and 9 deletions

13
pom.xml
View File

@@ -16,20 +16,15 @@
<!-- 各种 module 拓展 --> <!-- 各种 module 拓展 -->
<module>yudao-module-system</module> <module>yudao-module-system</module>
<module>yudao-module-infra</module> <module>yudao-module-infra</module>
<!-- <module>yudao-module-member</module>-->
<module>yudao-module-bpm</module> <module>yudao-module-bpm</module>
<!-- <module>yudao-module-pay</module>-->
<module>yudao-module-report</module> <module>yudao-module-report</module>
<module>yudao-module-mp</module> <!-- <module>yudao-module-mp</module>-->
<!-- <module>yudao-module-mall</module>-->
<!-- <module>yudao-module-crm</module>-->
<!-- <module>yudao-module-erp</module>-->
<!-- <module>yudao-module-ai</module>--> <!-- <module>yudao-module-ai</module>-->
<module>yudao-module-template</module> <module>yudao-module-template</module>
<!-- <module>yudao-module-iot</module>--> <!-- <module>yudao-module-iot</module>-->
<module>yudao-module-databus</module> <!-- <module>yudao-module-databus</module>-->
<module>yudao-module-rule</module> <!-- <module>yudao-module-rule</module>-->
<module>yudao-module-html2pdf</module> <!-- <module>yudao-module-html2pdf</module>-->
</modules> </modules>
<name>${project.artifactId}</name> <name>${project.artifactId}</name>

View File

@@ -17,6 +17,7 @@ public interface ErrorCodeConstants {
ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1_002_000_005, "未绑定账号,需要进行绑定"); 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_MOBILE_NOT_EXISTS = new ErrorCode(1_002_000_007, "手机号不存在");
ErrorCode AUTH_REGISTER_CAPTCHA_CODE_ERROR = new ErrorCode(1_002_000_008, "验证码不正确,原因:{}"); 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 ========== // ========== 菜单模块 1-002-001-000 ==========
ErrorCode MENU_NAME_DUPLICATE = new ErrorCode(1_002_001_000, "已经存在该名字的菜单"); ErrorCode MENU_NAME_DUPLICATE = new ErrorCode(1_002_001_000, "已经存在该名字的菜单");

View File

@@ -71,6 +71,14 @@ public class AuthController {
return success(authService.login(reqVO)); 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") @PostMapping("/logout")
@PermitAll @PermitAll
@Operation(summary = "登出系统") @Operation(summary = "登出系统")

View File

@@ -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;
}

View File

@@ -30,6 +30,15 @@ public interface AdminAuthService {
*/ */
AuthLoginRespVO login(@Valid AuthLoginReqVO reqVO); AuthLoginRespVO login(@Valid AuthLoginReqVO reqVO);
/**
* 测试登录(无验证码校验)
* 仅用于测试环境和本地开发,生产环境不可用
*
* @param reqVO 登录信息
* @return 登录结果
*/
AuthLoginRespVO testLogin(@Valid AuthTestLoginReqVO reqVO);
/** /**
* 基于 token 退出登录 * 基于 token 退出登录
* *

View File

@@ -78,6 +78,13 @@ public class AdminAuthServiceImpl implements AdminAuthService {
@Setter // 为了单测:开启或者关闭验证码 @Setter // 为了单测:开启或者关闭验证码
private Boolean captchaEnable; private Boolean captchaEnable;
/**
* 测试登录的开关,默认为 false
* 仅在测试环境test和本地开发环境local/dev下允许
*/
@Value("${spring.profiles.active:}")
private String activeProfile;
@Override @Override
public AdminUserDO authenticate(String username, String password) { public AdminUserDO authenticate(String username, String password) {
final LoginLogTypeEnum logTypeEnum = LoginLogTypeEnum.LOGIN_USERNAME; final LoginLogTypeEnum logTypeEnum = LoginLogTypeEnum.LOGIN_USERNAME;
@@ -116,6 +123,18 @@ public class AdminAuthServiceImpl implements AdminAuthService {
return createTokenAfterLoginSuccess(user.getId(), reqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME); 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 @Override
public void sendSmsCode(AuthSmsSendReqVO reqVO) { public void sendSmsCode(AuthSmsSendReqVO reqVO) {
// 如果是重置密码场景,需要校验图形验证码是否正确 // 如果是重置密码场景,需要校验图形验证码是否正确
@@ -307,4 +326,23 @@ public class AdminAuthServiceImpl implements AdminAuthService {
userService.updateUserPassword(userByMobile.getId(), reqVO.getPassword()); 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);
}
}
} }