mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-18 01:25:28 +08:00
update CaptchaController.smsCode 增加短信开关判断对齐 emailCode
This commit is contained in:
parent
7865f5210e
commit
447f6cdd39
@ -22,6 +22,7 @@ import org.dromara.common.ratelimiter.enums.LimitType;
|
||||
import org.dromara.common.redis.utils.RedisUtils;
|
||||
import org.dromara.common.web.core.WaveAndCircleCaptcha;
|
||||
import org.dromara.common.web.config.properties.CaptchaProperties;
|
||||
import org.dromara.common.sms.config.properties.SmsProperties;
|
||||
import org.dromara.sms4j.api.SmsBlend;
|
||||
import org.dromara.sms4j.api.entity.SmsResponse;
|
||||
import org.dromara.sms4j.core.factory.SmsFactory;
|
||||
@ -51,15 +52,28 @@ public class CaptchaController {
|
||||
|
||||
private final CaptchaProperties captchaProperties;
|
||||
private final MailProperties mailProperties;
|
||||
private final SmsProperties smsProperties;
|
||||
|
||||
/**
|
||||
* 短信验证码
|
||||
*
|
||||
* @param phonenumber 用户手机号
|
||||
*/
|
||||
@RateLimiter(key = "#phonenumber", time = 60, count = 1)
|
||||
@GetMapping("/resource/sms/code")
|
||||
public R<Void> smsCode(@NotBlank(message = "{user.phonenumber.not.blank}") String phonenumber) {
|
||||
if (!smsProperties.getEnabled()) {
|
||||
return R.fail("当前系统没有开启短信功能!");
|
||||
}
|
||||
SpringUtils.getAopProxy(this).smsCodeImpl(phonenumber);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 短信验证码
|
||||
* 独立方法避免短信功能关闭之后仍然走限流
|
||||
*/
|
||||
@RateLimiter(key = "#phonenumber", time = 60, count = 1)
|
||||
public void smsCodeImpl(@NotBlank(message = "{user.phonenumber.not.blank}") String phonenumber) {
|
||||
String key = GlobalConstants.CAPTCHA_CODE_KEY + phonenumber;
|
||||
String code = RandomUtil.randomNumbers(4);
|
||||
RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
|
||||
@ -71,9 +85,8 @@ public class CaptchaController {
|
||||
SmsResponse smsResponse = smsBlend.sendMessage(phonenumber, templateId, map);
|
||||
if (!smsResponse.isSuccess()) {
|
||||
log.error("验证码短信发送异常 => {}", smsResponse);
|
||||
return R.fail(smsResponse.getData().toString());
|
||||
throw new ServiceException(smsResponse.getData().toString());
|
||||
}
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -0,0 +1,35 @@
|
||||
package org.dromara.test;
|
||||
|
||||
import org.dromara.common.core.domain.R;
|
||||
import org.dromara.common.mail.config.properties.MailProperties;
|
||||
import org.dromara.common.sms.config.properties.SmsProperties;
|
||||
import org.dromara.common.web.config.properties.CaptchaProperties;
|
||||
import org.dromara.web.controller.CaptchaController;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* CaptchaController.smsCode 关闭路径单元测试
|
||||
* <p>直接构造 Controller 调用,不启动 Spring 上下文(无需 MySQL/Redis)
|
||||
*/
|
||||
@DisplayName("短信验证码开关单元测试")
|
||||
public class CaptchaSmsCodeTest {
|
||||
|
||||
@Tag("dev")
|
||||
@DisplayName("sms.enabled=false 时 smsCode 返回未开启提示且不发信")
|
||||
@Test
|
||||
public void shouldReturnFailWhenSmsDisabled() {
|
||||
SmsProperties smsProperties = new SmsProperties();
|
||||
smsProperties.setEnabled(false);
|
||||
// 字段声明顺序决定构造参数顺序:captchaProperties, mailProperties, smsProperties
|
||||
CaptchaController controller = new CaptchaController(
|
||||
new CaptchaProperties(), new MailProperties(), smsProperties);
|
||||
|
||||
R<Void> result = controller.smsCode("13800000000");
|
||||
|
||||
Assertions.assertEquals(R.FAIL, result.getCode());
|
||||
Assertions.assertEquals("当前系统没有开启短信功能!", result.getMsg());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user