add 增加 验证码短信发送接口 与 完善短信登录逻辑
parent
696daae92c
commit
21224a4c82
|
|
@ -26,6 +26,7 @@ security:
|
||||||
- /auth/smsLogin
|
- /auth/smsLogin
|
||||||
- /auth/xcxLogin
|
- /auth/xcxLogin
|
||||||
- /auth/register
|
- /auth/register
|
||||||
|
- /resource/sms/code
|
||||||
- /*/v2/api-docs
|
- /*/v2/api-docs
|
||||||
- /csrf
|
- /csrf
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,12 +48,12 @@ public class TokenController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 短信登录(示例)
|
* 短信登录
|
||||||
*
|
*
|
||||||
* @param smsLoginBody 登录信息
|
* @param smsLoginBody 登录信息
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@ApiOperation("短信登录(示例)")
|
@ApiOperation("短信登录")
|
||||||
@PostMapping("/smsLogin")
|
@PostMapping("/smsLogin")
|
||||||
public R<Map<String, Object>> smsLogin(@Validated @RequestBody SmsLoginBody smsLoginBody) {
|
public R<Map<String, Object>> smsLogin(@Validated @RequestBody SmsLoginBody smsLoginBody) {
|
||||||
Map<String, Object> ajax = new HashMap<>();
|
Map<String, Object> ajax = new HashMap<>();
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import com.ruoyi.common.core.constant.Constants;
|
||||||
import com.ruoyi.common.core.enums.DeviceType;
|
import com.ruoyi.common.core.enums.DeviceType;
|
||||||
import com.ruoyi.common.core.enums.LoginType;
|
import com.ruoyi.common.core.enums.LoginType;
|
||||||
import com.ruoyi.common.core.enums.UserType;
|
import com.ruoyi.common.core.enums.UserType;
|
||||||
|
import com.ruoyi.common.core.exception.user.CaptchaExpireException;
|
||||||
import com.ruoyi.common.core.exception.user.UserException;
|
import com.ruoyi.common.core.exception.user.UserException;
|
||||||
import com.ruoyi.common.core.utils.MessageUtils;
|
import com.ruoyi.common.core.utils.MessageUtils;
|
||||||
import com.ruoyi.common.core.utils.ServletUtils;
|
import com.ruoyi.common.core.utils.ServletUtils;
|
||||||
|
|
@ -130,8 +131,12 @@ public class SysLoginService {
|
||||||
* 校验短信验证码
|
* 校验短信验证码
|
||||||
*/
|
*/
|
||||||
private boolean validateSmsCode(String phonenumber, String smsCode) {
|
private boolean validateSmsCode(String phonenumber, String smsCode) {
|
||||||
// todo 此处使用手机号查询redis验证码与参数验证码是否一致 用户自行实现
|
String code = RedisUtils.getCacheObject(Constants.CAPTCHA_CODE_KEY + phonenumber);
|
||||||
return true;
|
if (StringUtils.isNotBlank(code)) {
|
||||||
|
recordLogininfor(phonenumber, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
|
||||||
|
throw new CaptchaExpireException();
|
||||||
|
}
|
||||||
|
return code.equals(smsCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,22 @@
|
||||||
<artifactId>ruoyi-common-oss</artifactId>
|
<artifactId>ruoyi-common-oss</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-common-sms</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 短信 用哪个导入哪个依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun</groupId>
|
||||||
|
<artifactId>dysmsapi20170525</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>com.tencentcloudapi</groupId>-->
|
||||||
|
<!-- <artifactId>tencentcloud-sdk-java</artifactId>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.ruoyi</groupId>
|
<groupId>com.ruoyi</groupId>
|
||||||
<artifactId>ruoyi-common-mybatis</artifactId>
|
<artifactId>ruoyi-common-mybatis</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,69 @@
|
||||||
|
package com.ruoyi.resource.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.hutool.core.util.RandomUtil;
|
||||||
|
import com.ruoyi.common.core.constant.Constants;
|
||||||
|
import com.ruoyi.common.core.domain.R;
|
||||||
|
import com.ruoyi.common.core.web.controller.BaseController;
|
||||||
|
import com.ruoyi.common.redis.utils.RedisUtils;
|
||||||
|
import com.ruoyi.common.sms.config.properties.SmsProperties;
|
||||||
|
import com.ruoyi.common.sms.core.SmsTemplate;
|
||||||
|
import com.ruoyi.common.sms.entity.SmsResult;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import io.swagger.annotations.ApiParam;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 短信功能
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Validated
|
||||||
|
@Api(value = "短信功能控制器", tags = {"短信功能管理"})
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/sms")
|
||||||
|
public class SysSmsController extends BaseController {
|
||||||
|
|
||||||
|
private final SmsProperties smsProperties;
|
||||||
|
private final SmsTemplate smsTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 短信验证码
|
||||||
|
*/
|
||||||
|
@ApiOperation("短信验证码")
|
||||||
|
@GetMapping("/code")
|
||||||
|
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));
|
||||||
|
// 验证码模板id 自行处理 (查数据库或写死均可)
|
||||||
|
String templateId = "";
|
||||||
|
Map<String, String> map = new HashMap<>(1);
|
||||||
|
map.put("code", code);
|
||||||
|
SmsResult result = smsTemplate.send(phonenumber, templateId, map);
|
||||||
|
if (!result.isSuccess()) {
|
||||||
|
log.error("验证码短信发送异常 => {}", result);
|
||||||
|
return R.fail(result.getMessage());
|
||||||
|
}
|
||||||
|
return R.ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue