文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Java整合腾讯云短信发送实例代码

2024-04-02 19:55

关注

1. 引入相关maven依赖

<dependency>
	<groupId>com.github.qcloudsms</groupId>
	<artifactId>qcloudsms</artifactId>
	<version>1.0.6</version>
</dependency>
<dependency>
    <groupId>com.qcloud</groupId>
	<artifactId>qcloud-java-sdk</artifactId>
	<version>2.0.1</version>
</dependency>
<dependency>
	<groupId>com.tencentcloudapi</groupId>
	<artifactId>tencentcloud-sdk-java</artifactId>
	<version>3.1.270</version><!-- 注:这里只是示例版本号(可直接使用),可获取并替换为 最新的版本号,注意不要使用4.0.x版本(非最新版本) -->
</dependency>
<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-all</artifactId>
	<version>5.4.2</version>
</dependency>

2. 编写短信发送工具类

package com.cloud.system.common.utils;
 
import ch.qos.logback.core.net.server.Client;
import cn.hutool.core.util.RandomUtil;
import com.cloud.system.api.enums.SmsLengthEnum;
import com.cloud.system.api.enums.SmsTypeEnum;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
 

@Component
@Slf4j
public class SmsUtil {
 
    
    private static String secretId;
 
    
    private static String secretKey;
 
    
    private static String appId;
 
    
    private static String appKey;
 
    
    private static String smsSign;
 
    
    private static String expireTime;
 
    
    private static String loginTemplateId;
 
    
    private static String pwdTemplateId;
 
    static {
        try {
            Properties prop=new Properties();
            prop.load(new InputStreamReader(
                    Client.class.getClassLoader().getResourceAsStream("sms.properties"), "gbk"));
            secretId = prop.get("secretId").toString();
            secretKey = prop.get("secretKey").toString();
            appId = prop.get("appId").toString();
            appKey = prop.get("appKey").toString();
            smsSign = prop.get("smsSign").toString();
            expireTime = prop.get("expireTime").toString();
            loginTemplateId = prop.get("loginTemplateId").toString();
            pwdTemplateId = prop.get("pwdTemplateId").toString();
 
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
 
    
    public static SendStatus sendMessage(String[] phoneNumbers, String[] templateParamSet,Integer type) throws TencentCloudSDKException {
        // 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setReqMethod("POST");
        httpProfile.setConnTimeout(60);
 
        //实例化一个客户端配置对象,指定超时时间等配置
        ClientProfile clientProfile = new ClientProfile();
        
        clientProfile.setSignMethod("HmacSHA256");
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new SmsClient(cred, "ap-guangzhou",clientProfile);
        SendSmsRequest req = new SendSmsRequest();
        //短信应用ID
        req.setSmsSdkAppId(appId);
        //短信签名内容
        req.setSignName(smsSign);
        
        //根据不同业务使用不同的模板
        switch (type){
            case 1:
                req.setTemplateId(pwdTemplateId);
                break;
            case 2:
                req.setTemplateId(loginTemplateId);
                break;
        }
        //下发手机号码
        req.setPhoneNumberSet(phoneNumbers);
        
        req.setTemplateParamSet(templateParamSet);
        SendSmsResponse res = client.SendSms(req);
        return res.getSendStatusSet()[0];
    }
 
    
    public static String createSmsRandomCode(SmsLengthEnum smsLengthEnum) {
        return RandomUtil.randomNumbers(smsLengthEnum.getLength());
    }
 
    
    public static String createSmsCacheKey(String prefix, String phone, String businessStr) {
        return prefix + "_" + businessStr + "_" + phone;
    }
}

3. 业务层

使用redis存储随机生成的验证码,并在使用后销毁

package com.cloud.system.api.service.impl;
 
import com.cloud.system.api.bean.SmsPwdBean;
import com.cloud.system.api.bean.SmsResult;
import com.cloud.system.api.entity.HumanInfo;
import com.cloud.system.api.entity.SmsRecord;
import com.cloud.system.api.enums.SmsLengthEnum;
import com.cloud.system.api.enums.SmsTypeEnum;
import com.cloud.system.api.mapper.HumanInfoMapper;
import com.cloud.system.api.mapper.SmsRecordMapper;
import com.cloud.system.api.service.SmsRecordService;
import com.cloud.system.common.utils.RedisUtil;
import com.cloud.system.common.utils.RegUtils;
import com.cloud.system.common.utils.SmsUtil;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;
 

@Service("smsRecordService")
@Transactional
@Slf4j
public class SmsRecordServiceImpl implements SmsRecordService {
 
    @Resource
    private SmsRecordMapper smsRecordMapper;
 
    @Resource
    private HumanInfoMapper humanInfoMapper;
 
    @Resource
    private RedisUtil redisUtil;
 
    @Override
    public SmsResult getModPwdVcode(String humanName, String phoneNumber) {
        //首先校验手机号码
        if (!RegUtils.isMoblie(phoneNumber)) return new SmsResult(500,"手机号格式不正确!",null);
        //然后校验输入手机号是否和该用户名绑定
        if (humanInfoMapper.existByNameAndPhone(humanName, phoneNumber) <= 0){
            return new SmsResult(500,"该用户不存在,请联系管理员注册!",null);
        }
        //生成六位的随机验证码
        return sendSms(phoneNumber, SmsTypeEnum.SMS_TYPE_1, humanName);
    }
 
    
    @Override
    public SmsResult updatePwdByHuman(SmsPwdBean bean) {
        //创建相同的缓存键
        String cacheKey = SmsUtil.createSmsCacheKey(String.valueOf(bean.getUsername().hashCode()),bean.getPhoneNumber(),
                SmsTypeEnum.SMS_TYPE_1.getTypeName());
        //验证码校验,若返回500,则直接返回
        SmsResult result = vCodeCheck(cacheKey, bean.getCaptcha());
        if (500 == result.getCode()){
            return result;
        }
        //获取人员id与密码进行加密,然后修改密码
        Long humanId = humanInfoMapper.existByNameAndPhone(bean.getUsername(),bean.getPhoneNumber());
        String code = DigestUtils.md5Hex(bean.getRepassword().concat(String.valueOf(humanId))).toUpperCase();
        humanInfoMapper.modifyPwdByNameAndPhone(bean.getUsername(),bean.getPhoneNumber(),code);
        //清除已使用的验证码
        redisUtil.del(cacheKey);
        return new SmsResult(200,"密码已重置!",null);
    }
 
 
    
    @Override
    public SmsResult getLoginCode(String humanName){
        //获取用户信息
        Example example = new Example(HumanInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deleteFlag", 0);
        criteria.andEqualTo("userName", humanName);
        List<HumanInfo> humanInfoList = humanInfoMapper.selectByExample(example);
        //校验用户手机号
        if (CollectionUtils.isEmpty(humanInfoList) || humanInfoList.size() != 1
            || StringUtils.isBlank(humanInfoList.get(0).getTelephone())
            || !RegUtils.isMoblie(humanInfoList.get(0).getTelephone())) {
            return new SmsResult(500,"手机号格式不正确!",null);
        }
        return sendSms(humanInfoList.get(0).getTelephone(), SmsTypeEnum.SMS_TYPE_2, humanInfoList.get(0).getHumanName());
    }
 
    
    @Override
    public SmsResult checkLoginCode(HumanInfo info,String captCha){
        //创建相同的缓存键
        String cacheKey = SmsUtil.createSmsCacheKey(String.valueOf(info.getHumanName().hashCode()),info.getTelephone(),
                SmsTypeEnum.SMS_TYPE_2.getTypeName());
        //验证码校验,若返回500,则直接返回
        SmsResult result = vCodeCheck(cacheKey, captCha);
        if (200 == result.getCode()){
            //清除已使用的验证码
            redisUtil.del(cacheKey);
        }
        return result;
    }
 
    
    private SmsResult sendSms(String telephone, SmsTypeEnum smsType2, String humanName) {
        //生成六位的随机验证码
        String randomCode = SmsUtil.createSmsRandomCode(SmsLengthEnum.SMS_LENGTH_6);
        //构建参数,发送短信
        SendStatus sendStatus = null;
        try {
            sendStatus = SmsUtil.sendMessage(new String[]{telephone}, new String[]{randomCode}, smsType2.getType());
        } catch (TencentCloudSDKException e) {
            log.error("********短信发送失败" + e.getMessage() + "*********");
            e.printStackTrace();
        }
        if (Objects.nonNull(sendStatus) && "Ok".equals(sendStatus.getCode())) {
            //发送成功后往记录表插值同时将验证码存储至redis,300s过期
            insertSmsRecord(telephone,smsType2.getType());
            //创建缓存键
            String cacheKey = SmsUtil.createSmsCacheKey(String.valueOf(humanName.hashCode()), telephone, smsType2.getTypeName());
            redisUtil.set(cacheKey, randomCode, 300);
            return new SmsResult(200, "验证码发送成功!", null);
        } else {
            return new SmsResult(500, "验证码发送失败!", null);
        }
    }
 
    
    private void insertSmsRecord(String phoneNumber, int type) {
        SmsRecord smsRecord = new SmsRecord();
        smsRecord.setRecipient(phoneNumber);
        smsRecord.setSendTime(new Date());
        smsRecord.setSmsType(type);
        smsRecordMapper.insert(smsRecord);
    }
 
    
    private SmsResult vCodeCheck(String cacheKey,String captcha){
        if (!redisUtil.hasKey(cacheKey)){
            return new SmsResult(500,"验证码已过期,请重新获取!",null);
        }
        if (!captcha.equals(redisUtil.get(cacheKey))){
            return new SmsResult(500,"验证码错误!",null);
        }
        return new SmsResult(200,"验证码正确!",null);
    }
}

3. 相关工具类

package com.cloud.system.common.utils;
 

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import org.apache.commons.lang.StringUtils;
 

public class RegUtils {
    
    public static final String EMAIL = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
    
    public static final String PHONE = "^(1[3-9]([0-9]{9}))$";
    
    public static final String CHINESE = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$";
    
    public static final String INTEGER = "^-?[1-9]\\d*$";
    
    public static final String NUMBER = "^([+-]?)\\d*\\.?\\d+$";
    
    public static final String INTEGER_POS = "^[1-9]\\d*$";
    
    public static final String FLOAT = "^([+-]?)\\d*\\.\\d+$";
    
    public static final String FLOAT_POS = "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$";
    
    public static final String INTEGER_WITH_ZERO_POS = "^(([0-9])|([1-9]([0-9]+)))$";
    
    public static final String NUMBER_WITH_ZERO = "^((-)?(([0-9])|([1-9]([0-9]+))))$";
    
    public static final String NUMBER_TEXT = "^([0-9]+)$";
    
    public static final String NUMBER_ALL = "^((-)?(([0-9])|([1-9][0-9]+))(\\.([0-9]+))?)$";
    
    public static final String QQ = "^[1-9][0-9]{4,13}$";
    
    public static final String IP = "((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))";
    
    public static final String POST_CODE = "[1-9]\\d{5}(?!\\d)";
    
    public static final String DATE = "^[1-9]\\d{3}-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|(3[0-1]))$";
    
    public static final String DATE_COMPLEX = "^(([1-2]\\d{3})(-|/|.|年)((((01|03|05|07|08|10|12))(-|/|.|月)((0[1-9])|([1-2][0-9])|(3[0-1])))|(((04|06|11))(-|/|.|月)((0[1-9])|([1-2][0-9])|(30)))|(02-((0[1-9])|([1-2][0-9]))))(日)?)$";
 
    
    public static final String DATE_COMPLEX_LEAP_YEAR = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";
 
 
    
    public static boolean isMatch(String regex, CharSequence content){
        return Pattern.matches(regex, content);
    }
 
 
    
    public static final boolean isMoblie(String mobile){
        boolean flag = false;
        if (null != mobile && !mobile.trim().equals("") && mobile.trim().length() == 11) {
            Pattern pattern = Pattern.compile(PHONE);
            Matcher matcher = pattern.matcher(mobile.trim());
            flag = matcher.matches();
        }
        return flag;
    }
 
 
    
    public static final boolean isEmail(String value){
        boolean flag = false;
        if (null != value && !value.trim().equals("")) {
            Pattern pattern = Pattern.compile(EMAIL);
            Matcher matcher = pattern.matcher(value.trim());
            flag = matcher.matches();
        }
        return flag;
    }
 
 
    
    public static final boolean isPassword(String password){
        boolean flag = false;
        if (null != password && !password.trim().equals("")) {
            password = password.trim();
            if(password.length() >= 6 && password.length() <= 30){
                return true;
            }
        }
        return flag;
    }
 
 
    
    public static final boolean isPhoneValidateCode(String value){
        boolean flag = false;
        if (null != value && !value.trim().equals("")) {
            Pattern pattern = Pattern.compile("^8\\d{5}$");
            Matcher matcher = pattern.matcher(value.trim());
            flag = matcher.matches();
        }
        return flag;
    }
 
 
    
    public static boolean isUpperCase(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        String reg = "^[A-Z]$";
        return isMatch(reg,str);
    }
 
 
    
    public static boolean isLowercase(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        String reg = "^[a-z]$";
        return isMatch(reg,str);
    }
 
 
    
    public static boolean isIP(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        return isMatch(IP, str);
    }
 
    
    public static boolean isDate(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        return isMatch(DATE_COMPLEX_LEAP_YEAR, str);
    }
 
 
    
    public static boolean isDateSimple(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        return isMatch(DATE, str);
    }
 
 
    
    public static boolean isDateComplex(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        return isMatch(DATE_COMPLEX, str);
    }
 
 
    
    public static boolean isNumberText(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        return isMatch(NUMBER_TEXT, str);
    }
 
 
    
    public static boolean isNumberAll(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        return isMatch(NUMBER_ALL, str);
    }
 
 
    
    public static boolean isIntegerWithZeroPos(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        return isMatch(INTEGER_WITH_ZERO_POS, str);
    }
 
 
    
    public static boolean isIntegerWithZero(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        return isMatch(NUMBER_WITH_ZERO, str);
    }
 
 
    
    public static boolean isQQ(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        return isMatch(QQ, str);
    }
 
 
    public static void main(String[] args) {
        System.out.println(isMoblie("13430800244"));
        System.out.println(isMoblie("17730800244"));
        System.out.println(isMoblie("17630800244"));
        System.out.println(isMoblie("14730800244"));
        System.out.println(isMoblie("18330800244"));
        System.out.println(isMoblie("19330800244"));
        System.out.println(isMoblie("1333000244"));
    }
 
 
}

到此这篇关于Java整合腾讯云短信发送实例代码的文章就介绍到这了,更多相关Java腾讯云短信发送内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯