Springboot通过腾讯云服务器发送短信验证

package com.li.opus.common.utils;

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;


public class SmsUtils {
    private static final String secretId="你腾讯云服务器的secretId";

    private static final String secretKey="你腾讯云服务器的secretKey";

    private static final String sdkAppId = "你腾讯云服务器的sdkAppId";

    private static final String signName = "你腾讯云服务器的signName ";

    private static final String templateId = "你腾讯云服务器的templateId ";


    //创建实例化认证对象
    private static Credential cred = new Credential(secretId, secretKey);
    // 实例化一个http选项
    private static HttpProfile httpProfile = new HttpProfile();
  //实例化一个客户端配置对象,可以指定超时时间等配置
    private static ClientProfile clientProfile = new ClientProfile();
public static String send(String [] phone, String[] verify)  {
        SmsClient client = new SmsClient(cred, "ap-guangzhou",clientProfile);
        httpProfile.setReqMethod("POST");
        httpProfile.setConnTimeout(60);
        httpProfile.setEndpoint("sms.tencentcloudapi.com");
        clientProfile.setSignMethod("HmacSHA256");
        clientProfile.setHttpProfile(httpProfile);
        SendSmsRequest req = new SendSmsRequest();
        SendSmsResponse res=new SendSmsResponse();
        try {
            req.setSmsSdkAppId(sdkAppId);
            req.setTemplateId(templateId);
            req.setSignName(signName);
            //模板参数:  自动生成一个6位数的随机数
            req.setTemplateParamSet(verify);
            System.out.println(verify);
           //下发手机号码:前端传手机号过来
            req.setPhoneNumberSet(phone);
            System.out.println(phone);
            res=client.SendSms(req);
            System.out.println(req);
            System.out.println(SendSmsResponse.toJsonString(res));
            return SendSmsResponse.toJsonString(res);
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return SendSmsResponse.toJsonString(res);
    }
}
package com.li.opus.common;
import java.util.UUID;
public class Random {
    /**
     * 短信验证生成随机数
     * @return
     */
    public static String [] generateCaptcha() {
        /** 生成6位短信随机数 */
        String captcha = UUID.randomUUID().toString()
                .replaceAll("-", "")
                .replaceAll("[a-z|A-Z]","")
                .substring(0, 6);
        String [] arr={captcha};
        return arr;
    }

}
package com.li.opus.service;

import com.li.opus.common.Random;
import com.li.opus.common.utils.SmsUtils;
import com.li.opus.dao.UserInfoDao;
import com.li.opus.entity.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.util.StringUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoService {
    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private RedisTemplate redisTemplate;


 
    /**
     * 通过电话号码发送短信
     * @param phone
     * @return
     */
    public static String sendMessage(String[] phone) {
        try {
           String[] verify=Random.generateCaptcha();
            String data = SmsUtils.send(phone,verify);
            return data;
        } catch (Exception ex){
            throw new RuntimeException(ex);
        }
    }
}
package com.li.opus.controller;

import cn.hutool.json.JSONArray;
import com.li.opus.common.Result;
import com.li.opus.common.utils.SmsUtils;
import com.li.opus.entity.UserInfo;
import com.li.opus.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.util.StringUtil;
import java.util.Random;
import java.util.UUID;

@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

 
    /**
     * 发送短信验证码功能
     * @return
     */
    @PostMapping("/send")
    public Result sendPhone(@RequestParam("phone") String[] phone){
        //1.获取用户的电话号码,通过调用发送短信验证的service生成验证码
        return Result.success(userInfoService.sendMessage(phone));
    }
}

.

你可能感兴趣的:(腾讯云,java,云计算)