在Spring Boot应用中实现阿里云短信功能的整合

1.程序员必备程序网站

天梦星服务平台 (tmxkj.top)icon-default.png?t=N7T8https://tmxkj.top/#/

2.导入坐标

       <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.0.0</version>
        </dependency>

3.yml文件配置

aliyun:
  sms: #阿里云发短信
    access-key-id: "********"  #角色的id
    access-key-secret: "******" #角色的密匙
    sign-name: "天梦星科技"
    template-code: "*******"

4.核心代码

@Component
public class SmsUtil {

    @Value("${aliyun.sms.access-key-id}")
    private String accessKeyId;

    @Value("${aliyun.sms.access-key-secret}")
    private String accessKeySecret;

    @Value("${aliyun.sms.sign-name}")
    private String signName;

    @Value("${aliyun.sms.template-code}")
    private String templateCode;

    public Result sendSms(String phone, String code) throws ClientException {
        Result result = new Result();
        IAcsClient client = new DefaultAcsClient(DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret));
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        CommonResponse response = client.getCommonResponse(request);
        if (JSON.parseObject(response.getData()).get("Message").equals("OK")){
        result.setCode(200);
        result.setMsg("短信发送成功");
        }else {
            result.setCode(400);
            result.setMsg(JSON.parseObject(response.getData()).get("Message"));
        }
        return result;
    }

}

 

public class ToolUtil {

    /**
     * 判断是否是手机号
     * @param phoneNumber
     * @return
     */
    public static boolean isPhoneNumber(String phoneNumber) {
        String regex = "^1[3-9]\\d{9}$";
        return Pattern.matches(regex, phoneNumber);
    }

 /**
     * 获取长度为 5 的随机数字
     * @return 随机数字
     *  用途短信验证码
     */
    public static String getSmsRandomNumber() {
        char[] nonceChars = new char[5];  //指定长度为6位/自己可以要求设置
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS2.charAt(RANDOM.nextInt(SYMBOLS2.length()));
        }
        return new String(nonceChars);
    }
}

 

5.调用测试

    /**
     * 发送短信验证嘛
     */
    @GetMapping("/sendMessage")
    public Result sendMessage(@RequestParam("phone") String phone){
        Result result = new Result();
        String code = getSmsRandomNumber(); // 生成随机验证码
        try {
            if(isPhoneNumber(phone)){
                //redisDao.vSet(code,code,imaileEpxtime);
                result = smsUtil.sendSms(phone, code);
            }else {
                result.setCode(400);
                result.setMsg("手机号格式错误!");
            }
        } catch (ClientException e) {
            e.fillInStackTrace();
            result.setCode(500);
            result.setMsg(e.getErrMsg());
        }
        return result;
    }

6.成功示例

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值