飞道的博客

SpringBoot实战系列之发送短信验证码

440人阅读  评论(0)
大家好,我是工藤学编程 🦉 大二在读
作业侠系列最新文章😉 Java实现聊天程序
SpringBoot实战系列🐷 SpringBoot实战系列之发送短信验证码
一起刷算法与数据结构最新文章🐷 一起刷算法与数据结构-树篇1
环境搭建大集合 环境搭建大集合(持续更新)

内容速览:
1.短信验证码平台选择考虑点
2.短信平台
3.实战发送短信验证码

1.短信验证码平台选择考虑点

  • 各个类型短信价格
  • 短信到达率、到达时间
  • 短信内容变量灵活,⽅便⽀持多场景
  • ⽀持多种推⼴内容的短信发放,例如业务推⼴、新产品宣讲、
  • 会员关怀等内容的短信
  • 多维度数据统计-查看请求量、发送成功量、失败量、等

2.短信平台

  • 阿⾥云:https://www.aliyun.com/product/sms
    公司使用推荐
  • 腾讯云:https://cloud.tencent.com/product/sms
    公司使用推荐
  • 第三⽅⼚商:https://market.aliyun.com/products/5700000
    2/cmapi00046920.html
    个人测试推荐

注意
由于申请接入阿里云,腾讯云需要企业认证,所以我们使用第三方厂商,复制上方链接浏览器打开即可

进入之后,点击立即购买,不用选择什么,直接支付即可,大家看自己需求选择即可,自己测试着玩,三元即可

购买好之后会有对应api文档,当然包括一些类似密钥的东西
购买之后,api文档过一会就出现

代码实战:
依赖说明,能够跑起来的Springboot项目就行
在对应application.yml中添加加如下内容:

sms:
  app-code: ${你自己的appcode}
  template-id: M72CB42894

template-id就是你短信发送的模板id,这个是官方默认的,想自己定义需要申请

使用restTemplate用于第三方接口调用
对应封装:

@Configuration
public class RestTemplateConfig {
   
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory requestFactory){
   
        return new RestTemplate(requestFactory);
    }
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
   
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(10000);
        factory.setConnectTimeout(10000);
        return factory;
    }
}

然后封装smsConfig,使用了Lombok,大家没有对应依赖的生成对应setter,getter方法即可

@ConfigurationProperties(prefix = "sms")
@Configuration
@Data
public class SmsConfig {
   
    private String appCode;

    private String templateId;
}

封装发送业务,信息打印使用了slf4j,大家没有对应依赖换成sout即可:

@Component
@Slf4j
public class SmsComponent {
   

    private static final String URL_TEMPLATE = "https://jmsms.market.alicloudapi.com/sms/send?mobile=%s&templateId=%s&value=%s";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private SmsConfig smsConfig;


    public void send(String to, String templateId, String value) {
   
        String url = String.format(URL_TEMPLATE, to, templateId, value);

        HttpHeaders headers = new HttpHeaders();
        //最后在header中的格式(中间是英⽂空格)为
        headers.set("Authorization", "APPCODE " + smsConfig.getAppCode());
        HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        log.info("url={},body={}", url, response.getBody());
        if (response.getStatusCode() == HttpStatus.OK) {
   
            log.info("发送短信成功,响应信息:{}", response.getBody());
        } else {
   
            log.error("发送短信失败,响应信息:{}", response.getBody());
        }
    }

}


 

在对应单元测试中编写测试方法:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AccountApplication.class)
@Slf4j
public class SmsTest {
   


    @Autowired
    private SmsConfig smsConfig;
    @Autowired
    private SmsComponent smsComponent;

    @Test
    public void testSmsSend(){
   
        smsComponent.send("110",smsConfig.getTemplateId(),"54688");
    }

}

 

运行,发送成功

我的手机也收到了消息:


转载:https://blog.csdn.net/chandfy/article/details/125921257
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场