寫(xiě)在最前
如果這個(gè)項(xiàng)目讓你有所收獲,記得 Star 關(guān)注哦,這對(duì)我是非常不錯(cuò)的鼓勵(lì)與支持。
源碼地址(后端):https://gitee.com/csps/mingyue-springcloud-learning
源碼地址(前端):https://gitee.com/csps/mingyue-springcloud-ui
文檔地址:https://gitee.com/csps/mingyue-springcloud-learning/wikis
阿里云短信
需要注冊(cè)一個(gè)阿里云賬號(hào),進(jìn)入阿里云短信服務(wù)的控制臺(tái),選擇快速學(xué)習(xí)和測(cè)試:https://dysms.console.aliyun.com/quickstart
發(fā)送驗(yàn)證碼
引入依賴(lài)
<!-- 短信工具 -->
<dependency>
<groupId>com.csp.mingyue</groupId>
<artifactId>mingyue-common-sms</artifactId>
</dependency>
<!-- 阿里云短信依賴(lài) -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
</dependency>
Nacos 短信配置
accessKeyId:阿里云 AccessKey ID
accessKeySecret:阿里云 AccessKey Secret
signName:阿里云簽名名稱(chēng)
sms:
enabled: true
endpoint: "dysmsapi.aliyuncs.com"
accessKeyId: xxx
accessKeySecret: xxx
signName: 阿里云短信測(cè)試
短信配置類(lèi)
@Data
@ConfigurationProperties(prefix = "sms")
public class SmsProperties {
private Boolean enabled;
/**
* 配置節(jié)點(diǎn) 阿里云 dysmsapi.aliyuncs.com
*/
private String endpoint;
/**
* key
*/
private String accessKeyId;
/**
* 密匙
*/
private String accessKeySecret;
/*
* 短信簽名
*/
private String signName;
}
注冊(cè)配置類(lèi)
/**
* 短信配置類(lèi)
*
* @author Strive
* @date 2023/8/25 10:04
*/
@AutoConfiguration
@EnableConfigurationProperties(SmsProperties.class)
public class SmsAutoConfiguration {
@Configuration
@ConditionalOnProperty(value = "sms.enabled", havingValue = "true")
@ConditionalOnClass(com.aliyun.dysmsapi20170525.Client.class)
static class AliyunSmsConfiguration {
@Bean
public SmsTemplate aliyunSmsTemplate(SmsProperties smsProperties) {
return new AliyunSmsTemplate(smsProperties);
}
}
}
短信接口
String templateId = “”;
templateId:阿里云模版Code,例如:SMS_154950909
/**
* 短信驗(yàn)證碼
* @param phone 用戶(hù)手機(jī)號(hào)
*/
@GetMapping("/code")
@Operation(summary = "短信驗(yàn)證碼", parameters = { @Parameter(name = "phone", description = "手機(jī)號(hào)", required = true) })
public R<Void> smsCaptcha(@NotBlank(message = "手機(jī)號(hào)不能為空") String phone) {
if (!smsProperties.getEnabled()) {
return R.fail("當(dāng)前系統(tǒng)沒(méi)有開(kāi)啟短信功能!");
}
String key = CacheConstants.CAPTCHA_CODE_KEY + phone;
String code = RandomUtil.randomNumbers(4);
redisTemplate.opsForValue().set(key, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
log.info("驗(yàn)證碼短信「{}」發(fā)送至手機(jī)「{}」 => {}", code, phone);
// 驗(yàn)證碼模板 ID 暫時(shí)可以寫(xiě)死
String templateId = "";
Map<String, String> map = new HashMap<>(1);
map.put("code", code);
SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
SmsResult result = smsTemplate.send(phone, templateId, map);
if (!result.getIsSuccess()) {
log.error("驗(yàn)證碼短信發(fā)送異常 => {}", result);
return R.fail(result.getMessage());
}
return R.ok();
}
發(fā)送測(cè)試
手機(jī)接收到短信即可!文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-675362.html
小結(jié)
現(xiàn)在短信驗(yàn)證碼已經(jīng)可以推送至手機(jī)上了,接下來(lái)修改短信登錄,通過(guò)手機(jī)號(hào)發(fā)送短信驗(yàn)證碼,然后登錄!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-675362.html
到了這里,關(guān)于022-從零搭建微服務(wù)-短信服務(wù)(二)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!