SpringBoot接入阿里云短信服务,实现登录验证功能

为了丰富博客功能,周末闲来无事做了个登录,目前已完成手机登录、github登录。

今天这篇文章先来整理下在springboot中接入阿里云短信服务的一个具体流程。

还请多多关注我的公众号:前端进阶攻城狮 每周固定更新技术分享

或者来到我的博客进行留言 Cc技术人生[1]

获取AccessKey信息

首先让我们登录阿里云官网,然后在右上角进入AccessKey管理

AccessKey相当于一个身份验证,调用所有阿里云提供的服务时根据这个来进行一个收费。

这里根据实际创建自己的id与secret

签名与模板

接下来通过控制台进入短信服务

如果你和我一样真实项目中需要,这里请添加你的签名和模板

签名的名称还请严谨,最好是网站的备案名,否则你会像我一样被拒绝很多次

在签名审核成功后来添加你的模板,当然这也是需要人工审核的

如果你觉得以上审核流程太过于麻烦,也可以使用提供测试的签名与模板

来到这个页面添加你的测试手机号,选择专用签名/模板,这里是不要审核的

好了,做到这里你现在一共有四样信息在手上了

accessKeyId accessKeySecret signName templateCode

现在可以开始写代码了

进入你的Springboot

首先我们把刚才拿到的几样信息写在配置文件里

message:
  accessKeyId: LTAxxxxxxxxxxxxxxxxx2Ej
  accessKeySecret: GuyxxxxxxxxxxxxxxxxxxxXG
  signName: xxxx博客
  templateCode: SMS_xxxxxxxx

根据实际换成自己的

引入jar包

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>dysmsapi20170525</artifactId>
    <version>2.0.1</version>
</dependency>


编写发送短信工具函数

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Data
@Component
@ConfigurationProperties(prefix = "message")
public class SendCode {
    private String accessKeyId;
  
    private String accessKeySecret;

    private String signName;

    private String templateCode;



    @SneakyThrows
    public Client createClient(String accessKeyId, String accessKeySecret){
        Config config = new Config()
                
                .setAccessKeyId(accessKeyId)
                
                .setAccessKeySecret(accessKeySecret);
        
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }


    
     * 发送短信
     *
     * @param phoneNumber 手机号
     * @return 返回结果
     */
    @SneakyThrows
    public SendSmsResponseBody sendMessage(String phoneNumber) {

        Client client = createClient(accessKeyId, accessKeySecret);
        String code = RandomUtil.buildCheckCode(4);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phoneNumber)
                .setTemplateParam("{\"code\":\""+code+"\"}");
        RuntimeOptions runtime = new RuntimeOptions();
        
        SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);

        SendSmsResponseBody body = response.getBody();

        if(body.getCode().equals("OK")){
            return body;
        }else {
           throw new RuntimeException(body.getMessage());
        }
    }
}



编写生成随机数字的工具函数

import java.util.Random;

public class RandomUtil {
    
     * 生成验证码
     * @param digit 位数
     * @return
     */
    public static String buildCheckCode(int digit){
        String str = "0123456789";
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < digit; i++) {
            char ch = str.charAt(random.nextInt(str.length()));
            sb.append(ch);
        }
        return sb.toString();
    }
}



最后我们来测试一下

也是成功收到了测试短信。

总结

整体流程还是比较简单的,比较耽误时间的可能就是签名与模板的审核,大家可以提前去申请准备好以备不时之需。

如有不足之处还请评价留言交流,谢谢大家!

参考资料

[1]

https://cc-cloud.cn/: https://link.juejin.cn/?target=https%3A%2F%2Fcc-cloud.cn%2F

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

技术小羊

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

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

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

打赏作者

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

抵扣说明:

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

余额充值