add 增加 获取短信验证码接口
This commit is contained in:
parent
416088a2de
commit
71e392c1f9
@ -4,6 +4,7 @@ import cn.hutool.captcha.AbstractCaptcha;
|
||||
import cn.hutool.captcha.generator.CodeGenerator;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.enums.CaptchaType;
|
||||
@ -12,13 +13,17 @@ import com.ruoyi.common.utils.redis.RedisUtils;
|
||||
import com.ruoyi.common.utils.reflect.ReflectUtils;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
import com.ruoyi.framework.config.properties.CaptchaProperties;
|
||||
import com.ruoyi.sms.config.properties.SmsProperties;
|
||||
import com.ruoyi.system.service.ISysConfigService;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.ApiParam;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import java.time.Duration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -28,14 +33,33 @@ import java.util.Map;
|
||||
*
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Validated
|
||||
@Api(value = "验证码操作处理", tags = {"验证码管理"})
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
public class CaptchaController {
|
||||
|
||||
private final CaptchaProperties captchaProperties;
|
||||
private final SmsProperties smsProperties;
|
||||
private final ISysConfigService configService;
|
||||
|
||||
/**
|
||||
* 短信验证码
|
||||
*/
|
||||
@ApiOperation("短信验证码")
|
||||
@GetMapping("/captchaSms")
|
||||
public R<Void> smsCaptcha(@ApiParam("用户手机号")
|
||||
@NotBlank(message = "{user.phonenumber.not.blank}")
|
||||
String phonenumber) {
|
||||
if (smsProperties.getEnabled()) {
|
||||
R.fail("当前系统没有开启短信功能!");
|
||||
}
|
||||
String key = Constants.CAPTCHA_CODE_KEY + phonenumber;
|
||||
String code = RandomUtil.randomNumbers(4);
|
||||
RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
*/
|
||||
|
@ -124,6 +124,7 @@ security:
|
||||
- /logout
|
||||
- /register
|
||||
- /captchaImage
|
||||
- /captchaSms
|
||||
# 静态资源
|
||||
- /*.html
|
||||
- /**/*.html
|
||||
|
@ -29,6 +29,12 @@
|
||||
<artifactId>ruoyi-oss</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SMS功能模块 -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-sms</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
@ -79,7 +79,7 @@ public class SysLoginService {
|
||||
SysUser user = loadUserByPhonenumber(phonenumber);
|
||||
|
||||
HttpServletRequest request = ServletUtils.getRequest();
|
||||
checkLogin(LoginType.SMS, user.getUserName(), () -> !validateSmsCode(phonenumber, smsCode));
|
||||
checkLogin(LoginType.SMS, user.getUserName(), () -> !validateSmsCode(phonenumber, smsCode, request));
|
||||
// 此处可根据登录用户的数据不同 自行创建 loginUser
|
||||
LoginUser loginUser = buildLoginUser(user);
|
||||
// 生成token
|
||||
@ -121,9 +121,13 @@ public class SysLoginService {
|
||||
/**
|
||||
* 校验短信验证码
|
||||
*/
|
||||
private boolean validateSmsCode(String phonenumber, String smsCode) {
|
||||
// todo 此处使用手机号查询redis验证码与参数验证码是否一致 用户自行实现
|
||||
return true;
|
||||
private boolean validateSmsCode(String phonenumber, String smsCode, HttpServletRequest request) {
|
||||
String code = RedisUtils.getCacheObject(Constants.CAPTCHA_CODE_KEY + phonenumber);
|
||||
if (StringUtils.isNotBlank(code)) {
|
||||
asyncService.recordLogininfor(phonenumber, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"), request);
|
||||
throw new CaptchaExpireException();
|
||||
}
|
||||
return code.equals(smsCode);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -57,3 +57,15 @@ export function getCodeImg() {
|
||||
timeout: 20000
|
||||
})
|
||||
}
|
||||
|
||||
// 短信验证码
|
||||
export function getCodeSms() {
|
||||
return request({
|
||||
url: '/captchaSms',
|
||||
headers: {
|
||||
isToken: false
|
||||
},
|
||||
method: 'get',
|
||||
timeout: 20000
|
||||
})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user