022-从零搭建微服务-短信服务(二)

写在最前

如果这个项目让你有所收获,记得 Star 关注哦,这对我是非常不错的鼓励与支持。

源码地址(后端):https://gitee.com/csps/mingyue-springcloud-learning

源码地址(前端):https://gitee.com/csps/mingyue-springcloud-ui

文档地址:https://gitee.com/csps/mingyue-springcloud-learning/wikis

阿里云短信

需要注册一个阿里云账号,进入阿里云短信服务的控制台,选择快速学习和测试:https://dysms.console.aliyun.com/quickstart

image-20230826160525328

发送验证码

引入依赖

<!-- 短信工具 -->
<dependency>
    <groupId>com.csp.mingyue</groupId>
    <artifactId>mingyue-common-sms</artifactId>
</dependency>
<!-- 阿里云短信依赖 -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>dysmsapi20170525</artifactId>
</dependency>

Nacos 短信配置

accessKeyId:阿里云 AccessKey ID

accessKeySecret:阿里云 AccessKey Secret

signName:阿里云签名名称

sms:
  enabled: true
  endpoint: "dysmsapi.aliyuncs.com"
  accessKeyId: xxx
  accessKeySecret: xxx
  signName: 阿里云短信测试

短信配置类

@Data
@ConfigurationProperties(prefix = "sms")
public class SmsProperties {

	private Boolean enabled;

	/**
	 * 配置节点 阿里云 dysmsapi.aliyuncs.com
	 */
	private String endpoint;

	/**
	 * key
	 */
	private String accessKeyId;

	/**
	 * 密匙
	 */
	private String accessKeySecret;

	/*
	 * 短信签名
	 */
	private String signName;

}

注册配置类

/**
 * 短信配置类
 *
 * @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

/**
	 * 短信验证码
	 * @param phone 用户手机号
	 */
	@GetMapping("/code")
	@Operation(summary = "短信验证码", parameters = { @Parameter(name = "phone", description = "手机号", required = true) })
	public R<Void> smsCaptcha(@NotBlank(message = "手机号不能为空") String phone) {
		if (!smsProperties.getEnabled()) {
			return R.fail("当前系统没有开启短信功能!");
		}
		String key = CacheConstants.CAPTCHA_CODE_KEY + phone;
		String code = RandomUtil.randomNumbers(4);
		redisTemplate.opsForValue().set(key, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
		log.info("验证码短信「{}」发送至手机「{}」  => {}", code, phone);
		// 验证码模板 ID 暂时可以写死
		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("验证码短信发送异常 => {}", result);
			return R.fail(result.getMessage());
		}
		return R.ok();
}

发送测试

手机接收到短信即可!

小结

现在短信验证码已经可以推送至手机上了,接下来修改短信登录,通过手机号发送短信验证码,然后登录!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Strive_MY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值