[feat] 新增短信验证码下发功能

This commit is contained in:
hsdllcw
2024-08-13 14:39:07 +08:00
parent f9cab79ead
commit 94fa483dd3
3 changed files with 108 additions and 51 deletions

View File

@@ -76,6 +76,11 @@
<artifactId>ruoyi-common-redis</artifactId> <artifactId>ruoyi-common-redis</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.12.13</version>
</dependency>
<dependency> <dependency>
<groupId>com.ujcms</groupId> <groupId>com.ujcms</groupId>
<artifactId>ujcms-commons</artifactId> <artifactId>ujcms-commons</artifactId>
@@ -89,10 +94,6 @@
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>
</exclusion> </exclusion>
<exclusion>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
@@ -151,6 +152,10 @@
</goals> </goals>
</execution> </execution>
</executions> </executions>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>

View File

@@ -1,46 +1,84 @@
package com.ruoyi.gateway.config.properties; package com.ruoyi.gateway.config.properties;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
/** /**
* 验证码配置 * 验证码配置
* *
* @author ruoyi * @author ruoyi
*/ */
@Configuration @Configuration
@RefreshScope @RefreshScope
@ConfigurationProperties(prefix = "security.captcha") @ConfigurationProperties(prefix = "security.captcha")
public class CaptchaProperties public class CaptchaProperties
{ {
/** /**
* 验证码开关 * 验证码开关
*/ */
private Boolean enabled; private Boolean enabled;
/** /**
* 验证码类型math 数组计算 char 字符) * 验证码类型math 数组计算 char 字符)
*/ */
private String type; private String type;
public Boolean getEnabled() public Boolean getEnabled()
{ {
return enabled; return enabled;
} }
public void setEnabled(Boolean enabled) public void setEnabled(Boolean enabled)
{ {
this.enabled = enabled; this.enabled = enabled;
} }
public String getType() public String getType()
{ {
return type; return type;
} }
public void setType(String type) public void setType(String type)
{ {
this.type = type; this.type = type;
} }
}
public static class SMS {
@RefreshScope
@Configuration
@ConfigurationProperties(prefix = "security.captcha.sms.aliyuncs")
public static class Aliyuncs {
private String accessKeyId;
private String accessKeySecret;
private String signName;
private String templateCode;
public String getAccessKeyId() {
return accessKeyId;
}
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
}
public String getAccessKeySecret() {
return accessKeySecret;
}
public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
}
public String getSignName() {
return signName;
}
public void setSignName(String signName) {
this.signName = signName;
}
public String getTemplateCode() {
return templateCode;
}
public void setTemplateCode(String templateCode) {
this.templateCode = templateCode;
}
}
}
}

View File

@@ -5,6 +5,9 @@ import java.io.IOException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import com.alibaba.fastjson2.JSONObject;
import com.ujcms.commons.sms.AliyunUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.FastByteArrayOutputStream; import org.springframework.util.FastByteArrayOutputStream;
@@ -43,6 +46,9 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
@Autowired @Autowired
private CaptchaProperties captchaProperties; private CaptchaProperties captchaProperties;
@Autowired
private CaptchaProperties.SMS.Aliyuncs smsAliyuncs;
/** /**
* 生成验证码 * 生成验证码
*/ */
@@ -112,13 +118,21 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
return ajax; return ajax;
} }
// 保存验证码信息 // 保存验证码信息
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid; String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + receiver + ":" + uuid;
long expire = redisService.getExpire(verifyKey); long expire = redisService.getExpire(verifyKey);
if (expire <= 0) { if (expire <= 0) {
String code = captchaProducerNumber.createText(); String code = captchaProducerNumber.createText();
redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
ajax.put("code", code); ajax.put("code", code);
expire = redisService.getExpire(verifyKey); expire = redisService.getExpire(verifyKey);
AliyunUtils.sendSms(
smsAliyuncs.getAccessKeyId(),
smsAliyuncs.getAccessKeySecret(),
smsAliyuncs.getSignName(),
smsAliyuncs.getTemplateCode(),
JSONObject.of("code", code),
receiver
);
} }
ajax.put("expire", expire); ajax.put("expire", expire);
ajax.put("uuid", uuid); ajax.put("uuid", uuid);