Spring Boot & 阿里短信服务(Redis+短信验证码)

Spring Boot & 阿里短信服务(Redis+短信验证码)


[toc]


1. 创建签名

  • 如下位置创建签名,等待审核通过
    enter description here
    enter description here

2. 创建模板

  • 如下位置创建模板,等待审核通过
    enter description here
    enter description here

3. 测试验证码功能

  • 填写相关信息,测试发送
    enter description here
    enter description here
    注:发送失败可能因为你余额不足。可以选择购买套餐。新号可去尝试下图位置领取免费短信。
  • 官网主页下拉最后
  • enter description here
  • enter description here

4. 查看Api Demo

  • enter description here
  • enter description here

5. 获取AK信息

  • enter description here
  • 创建AccessKey
  • enter description here
  • 保存一下AccessKeyId和AccessKeySecret
  • enter description here
  • 将其复制分别填到AccessKeyId和AccessKeySecret的位置
  • enter description here

6. pom.xml

1
2
3
4
5
6
<!-- 短信服务-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.9</version>
</dependency>

7.yml配置

1
2
3
4
5
6
7
8
#阿里短息配置
ali:
endpoint: dysmsapi.aliyuncs.com
accessKey: LT***C4
secretKey: BQ***1M
signName: IDSE
templateCode: SMS_***75
expireTime: 300

8.自建工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.hbq.cms.util;

/**
* @Author: huibq
* @Date: 2022/5/6 15:33
* @Description: 短信发送工具类
*/
@Slf4j
@Component
public class MessageUtil {
private static Client messageClient;

@Resource
private RedisUtils redisUtils;

@Value("${ali.endpoint}")
private String endpoint;
@Value("${ali.accessKey}")
private String accessKey;
@Value("${ali.secretKey}")
private String secretKey;
@Value("${ali.signName}")
private String signName;
@Value("${ali.templateCode}")
private String templateCode;
@Value("${ali.expireTime}")
private Long expireTime;

@PostConstruct
public void init() {
try {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKey)
// 您的AccessKey Secret
.setAccessKeySecret(secretKey);
// 访问的域名
config.endpoint = endpoint;
messageClient = new Client(config);
} catch (Exception e) {
e.printStackTrace();
log.error("初始化messageClient配置异常: 【{}】", e.fillInStackTrace());
}
}

/**
* 发送短信
*
* @param tel 手机号
*/
public void sendMessage(String tel) {
String code = RandomUtil.randomNumbers(6);
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setSignName(signName)
.setTemplateCode(templateCode)
.setPhoneNumbers(tel)
.setTemplateParam("{\"code\":\"" + code + "\"}");
try {
SendSmsResponse sendSmsResponse = messageClient.sendSms(sendSmsRequest);
String result = sendSmsResponse.getBody().getCode();
if ("OK".equals(result)) {
redisUtils.set(String.format(RedisKey.MESSAGE_KEY,tel), code, expireTime);
}
} catch (Exception e) {
log.error("发送短信异常:{}", e.fillInStackTrace());
}
}

/**
* 验证短信验证码
* @param tel 手机号
* @param code 验证码
* @return 验证结果
*/
public boolean isCode(String tel, String code) {
String resultCode = redisUtils.get(String.format(RedisKey.MESSAGE_KEY,tel));
if (resultCode != null && resultCode.equals(code)) {
return true;
}
return false;
}

/**
* 不是正确的验证码
* @param tel 手机号
* @param code 验证码
* @return 验证结果
*/
public boolean isNotCode(String tel, String code) {
return !isCode(tel, code);
}
}

Spring Boot & 阿里短信服务(Redis+短信验证码)
http://example.com/28651.html
作者
John Doe
发布于
2022年9月5日
许可协议