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

写在最前

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

源码地址(后端):gitee.com/csps/mingyu...

源码地址(前端):gitee.com/csps/mingyu...

文档地址:gitee.com/csps/mingyu...

阿里云短信

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

发送验证码

引入依赖

xml 复制代码
<!-- 短信工具 -->
<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:阿里云签名名称

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

短信配置类

arduino 复制代码
@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;
​
}

注册配置类

less 复制代码
/**
 * 短信配置类
 *
 * @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

less 复制代码
/**
   * 短信验证码
   * @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();
}

发送测试

手机接收到短信即可!

小结

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

相关推荐
打工仔折腾 AI2 分钟前
LangChain 工具调用实战:从 Tool 定义到 Agent 执行链路拆解
java·前端·人工智能·后端·python·langchain·ai agent 实战
汉堡大王952718 分钟前
单次输出拉到100万Token:拆完 Gemini 4 Argon 的续写协议,才看懂谷歌这7个月在憋什么
前端·人工智能·后端
武子康1 小时前
ESP32 唤醒成功,离线命令为什么没有执行?
人工智能·后端·agent
挖掘狂人1 小时前
SQL 注入:一行代码如何撬开整个数据库
后端·sql·数据分析
今年下半年1 小时前
【短信发送】Spring Boot整合多种短信接口的实现(附源码)
java·spring boot·后端·sms·qcloudsms
不爱吃鱼的蔓热莓1 小时前
Autosar之统计SWC中port、runnable、信号名等信息(三)
后端·restful
北城笑笑2 小时前
Server 18 ,Nginx 双服务器 HTTP转HTTPS 代理实战( HTTP 地址换 HTTPS 域名 )
运维·服务器·前端·后端·nginx·http·https
紫洋葱hh2 小时前
Go 入门基础:类型、变量与运算符
开发语言·后端·ai·golang
打工仔折腾 AI2 小时前
Qt网络编程实战:UDP、TCP到HTTP的落地经验与选型
网络·人工智能·后端·python·qt·udp·ai agent 实战
打工仔折腾 AI2 小时前
SQL 编辑器不是高级记事本:KStudio 与 KSQL Developer 的建表与增删改查手法
数据库·人工智能·后端·python·sql·编辑器·ai agent 实战