Java中对接阿里云短信能力

Java中对接阿里云短信能力

1、控制台中获取相关参数

短信服务

1.1、申请签名,申请模板

image-20240125153019923

1.2、openapi门户中在线联调

api门户

image-20240125154313726

image-20240125154331040

image-20240125152708759

填写自己的参数调用即可

2、API对接阿里云

2.1、相关代码

导入依赖:
            <!--阿里云短信sdk-->
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>dysmsapi20170525</artifactId>
                <version>2.0.24</version>
            </dependency>
#阿里云短信配置
aLiYunSms:
  accessKey: xxx
  secretKey: xxx
  signName: xxx
  templateCode: xxx
package com.wondertek.center.util.sms.shiWei;

import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.util.ReUtil;
import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.wondertek.web.exception.enums.ErrorCodeEnum;
import com.wondertek.web.exception.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AliYunSmsUtil {

    public static String accessKey;
    public static String secretKey;
    public static String signName;
    public static String templateCode;

    @Value("${aLiYunSms.accessKey}")
    public  void setAccessKey(String accessKey) {
        AliYunSmsUtil.accessKey = accessKey;
    }
    @Value("${aLiYunSms.secretKey}")
    public  void setSecretKey(String secretKey) {
        AliYunSmsUtil.secretKey = secretKey;
    }
    @Value("${aLiYunSms.signName}")
    public  void setSignName(String signName) {
        AliYunSmsUtil.signName = signName;
    }
    @Value("${aLiYunSms.templateCode}")
    public  void setTemplateCode(String templateCode) {
        AliYunSmsUtil.templateCode = templateCode;
    }

    /**
     * 发送短信
     *
     * @return
     */
    public static SendSmsResponseBody sendSms(String mobile, Long smsCode) {
        return sendSmsLocal(accessKey, secretKey, signName, templateCode, mobile, smsCode);
    }

    /**
     * 发送短信
     *
     * @return
     */
    public static SendSmsResponseBody sendSmsLocal(String accessKey, String secretKey, String signName, String templateCode, String mobile, Long smsCode) {
        //检查手机号
        if (StringUtils.isNotBlank(mobile)) {
            if (!ReUtil.isMatch(RegexPool.MOBILE, mobile)) {
                throw new BizException(ErrorCodeEnum.ACCOUNT_FORMAT_ERROR);
            }
        }
        try {
            com.aliyun.dysmsapi20170525.Client client = AliYunSmsUtil.createClient(accessKey, secretKey);
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest().setPhoneNumbers(mobile).setSignName(signName).setTemplateCode(templateCode).setTemplateParam("{\"code\":\"" + smsCode.toString() + "\"}");
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            log.info("阿里云短信响应结果:{}", JSONUtil.toJsonStr(sendSmsResponse.getBody()));
            String code = sendSmsResponse.getBody().getCode();
            log.info("状态status:{}", code);
            if (!"OK".equals(code)) {
                throw new BizException(ErrorCodeEnum.SEND_SMS_CONTROL_ERROR.getCode(), sendSmsResponse.getBody().getMessage());
            }
            log.info("请求ID:{}", sendSmsResponse.getBody().getBizId());
            return sendSmsResponse.getBody();
        } catch (Exception error) {
            // 如有需要,请打印 error
            log.error("错误信息:{}", error.getMessage());
            throw new BizException(ErrorCodeEnum.SEND_SMS_ERROR);
        }

    }

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    private static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填,您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填,您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public static void main(String[] args_) throws Exception {
        sendSmsLocal("xxx", "xxx", "xxx", "xxx", "xxx", 123456L);
    }
}

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Java小白笔记

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

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

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

打赏作者

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

抵扣说明:

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

余额充值