栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

谷粒学院 java 整合 腾讯云短信

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

谷粒学院 java 整合 腾讯云短信

由于阿里云短信现在不对个人客户开放,所以就进行了整合腾讯云短信

腾讯云整合和视频中逻辑有些差别,网上的教程有限,参考了别人的代码,最大限度的还原视频中的代码

pom.xml



    
        org.springframework.boot
        spring-boot-starter-web
    
    
    
        org.projectlombok
        lombok
    
    
    
        com.tencentcloudapi
        tencentcloud-sdk-java-common
        3.1.488
    
    
        com.tencentcloudapi
        tencentcloud-sdk-java
        3.1.209
    
    
        com.tencentcloudapi
        tencentcloud-sdk-java-sms
        3.1.488
    
    
    
    
        org.springframework.boot
        spring-boot-configuration-processor
        true
    
application.yml
tencentcloud:
  sms:
    secretId: 
    secretKey: 
    #短信应用 ID
    appId: 
    #短信签名内容
    sign: 
    #短信模板ID
    templateID: 

 从配置文件读取常量

@Getter  //idea2020.2.3版配置文件自动提示需要这个
@Setter
@Component
@ConfigurationProperties(prefix="tencentcloud.sms")
public class SmsProperties implements InitializingBean {

    private String secretId;
    private String secretKey;
    private String appid;
    private String sign;
    private String templateID;

    private static String SECRET_ID;
    private static String SECRET_KEY;
    private static String APP_ID;
    private static String TEMPLATE_ID;
    private static String SIGN;


    @Override
    public void afterPropertiesSet() throws Exception {
        SECRET_ID=secretId;
        SECRET_KEY=secretKey;
        APP_ID=appid;
        SIGN=sign;
        TEMPLATE_ID=templateID;
    }
}
common-util中引入工具类:RandomUtils.java、FormUtils.java

1、创建controller
@RestController
@RequestMapping("/api/sms")
@Api(tags = "短信管理")
@CrossOrigin //跨域
@Slf4j
public class ApiSmsController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("send/{mobile}")
    public R getCode(@PathVariable String mobile) throws ClientException {

        //校验手机号是否合法
        if(StringUtils.isEmpty(mobile) || !FormUtils.isMobile(mobile)){
            log.error("请输入正确的手机号码 ");
            throw new GuliException(ResultCodeEnum.LOGIN_PHONE_ERROR);
        }

        //生成验证码
        String checkCode= RandomUtils.getFourBitRandom();
        //发送验证码
        smsService.send(mobile, checkCode);

        redisTemplate.opsForValue().set(mobile,checkCode, 5, TimeUnit.MINUTES);

        return R.ok().message("短信发送成功");
}
}
接口:SmsService.java
package com.atguigu.guli.service.sms.service;

import com.netflix.client.ClientException;

public interface SmsService {

    void send(String mobile, String checkCode) throws ClientException;
}
接口:SmsServiceImpl.java
package com.atguigu.guli.service.sms.service.impl;


import com.atguigu.guli.common.base.result.ResultCodeEnum;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.sms.service.SmsService;
import com.atguigu.guli.service.sms.util.SmsProperties;
import com.netflix.client.ClientException;


import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Autowired
    private SmsProperties smsProperties;
    @Override
    public void send(String mobile, String checkCode) throws ClientException {

        String jointMobile="+86"+mobile;
        String secretId = smsProperties.getSecretId();
        String secretKey = smsProperties.getSecretKey();
        String appid = smsProperties.getAppid();
        String sign = smsProperties.getSign();
        String templateID = smsProperties.getTemplateID();

        String[] phoneNumbers = { jointMobile};

        //模板参数: 若无模板参数,则设置为空
        String[] templateParams = { checkCode };//对应模板中{1}

        try {
            //必要步骤: 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId 和 secretKey
            Credential cred = new Credential(secretId , secretKey);
            ClientProfile clientProfile = new ClientProfile();
            //SDK 默认用 TC3-HMAC-SHA256 进行签名 非必要请不要修改该字段
            clientProfile.setSignMethod("HmacSHA256");
            // 实例化 SMS 的 client 对象 第二个参数是地域信息,可以直接填写字符串 ap-guangzhou,或者引用预设的常量
            SmsClient client = new SmsClient(cred, "", clientProfile);
            //实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数 您可以直接查询 SDK 源码确定接口有哪些属性可以设置
            SendSmsRequest req = new SendSmsRequest();

            // 短信应用 ID: 在 [短信控制台] 添加应用后生成的实际 SDKAppID,例如1400006666
            req.setSmsSdkAppid(appid);

            // 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,可登录 [短信控制台] 查看签名信息
            req.setSign(sign);

            //短信模板 ID: 必须填写已审核通过的模板 ID,可登录 [短信控制台] 查看模板 ID
            req.setTemplateID(templateID);

            //下发手机号码,采用 e.164 标准,+[国家或地区码][手机号] 例如+8613711112222
            req.setPhoneNumberSet(phoneNumbers);

            req.setTemplateParamSet(templateParams);

            // 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的 返回的 res 是一个SendSmsResponse 类的实例,与请求对象对应
            SendSmsResponse res = client.SendSms(req);
            //获取响应结果
            SendStatus[] sendStatusSet = res.getSendStatusSet();
            log.info("短信发送返回的响应:"+sendStatusSet);

        } catch (TencentCloudSDKException e) {
            log.error("腾讯云短信发送sdk调用失败:"+e.getErrorCode()+","+e.getMessage());
            throw new GuliException(ResultCodeEnum.SMS_SEND_ERROR);        }
    }

}

参考博客:腾讯云短信服务接口实现 - Learn to Cherish (jzjzzzz.icu)https://www.jzjzzzz.icu/cn/%E8%85%BE%E8%AE%AF%E4%BA%91%E7%9F%AD%E4%BF%A1%E6%9C%8D%E5%8A%A1%E6%8E%A5%E5%8F%A3%E5%AE%9E%E7%8E%B0/

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/831892.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号