整合腾讯云短信服务

该博客介绍了如何整合腾讯云短信服务,通过微信公众号申请并配置短信签名和SDK,然后在Java后端编写Controller和服务实现手机号码的验证码发送功能。具体步骤包括创建微信公众号获取签名,引入腾讯云SDK依赖,编写发送短信的Controller和Service,并展示了随机生成四位验证码的代码。
摘要由CSDN通过智能技术生成

整合腾讯云短信服务

腾讯云短信服务申请

阿里短信不好申请可以使用腾讯的:微信公众号方式申请
前提:自己去创建一个微信公众号
创建签名(签名类型:公众号, 证明类型:公众号设置页面截图)

导入依赖

        <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <version>3.1.210</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>

1、编写controller,根据手机号发送短信

@RestController
@RequestMapping("/edumsm/msm")
//@CrossOrigin
public class MsmController {
    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("send/{phone}")
    public R sendMsm(@PathVariable String phone) throws TencentCloudSDKException {
        String code = redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)){
            return R.ok();
        }
        code = RandomUtil.getFourBitRandom();
        boolean isSend = msmService.send(code,phone);
        if(isSend) {
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return R.ok();
        } else {
            return R.error().message("发送短信失败");
        }
    }

}

2、编写service

public interface MsmService {
    //发送短信的方法
    Boolean send(String code,String phone);
}

3、编写serviceImpl

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public Boolean send(String code,String phone){
        try{
            Credential cred = new Credential("AKID3xt65MtHsYXh6HFxlTxO0we8VAjhu0IM",
                                             "10pyub3MSIv6mHZcR2Th7JZbPGynPJzT");
            //自己的腾讯key

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred,"",clientProfile);
            SendSmsRequest request = new SendSmsRequest();
            String[] phone1 = {"+86"+phone};
            String[] templateParamSet1 = {code};//验证码
            request.setPhoneNumberSet(phone1);
            request.setTemplateParamSet(templateParamSet1);
            //必填:短信签名-可在短信控制台中找到
            request.setSign("咩咩哦真不错个人公众号");
            //短信SdkAppid在 短信控制台 添加应用后生成的实际SdkAppid,示例如1400006666。
            request.setSmsSdkAppid("1400513619");
            //必填:短信模板-可在短信控制台中找到
            request.setTemplateID("936098");
            SendSmsResponse resp = client.SendSms(request);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


}

4、编写随机生成数代码RandomUtil

public class RandomUtil {

	private static final Random random = new Random();

	private static final DecimalFormat fourdf = new DecimalFormat("0000");

	private static final DecimalFormat sixdf = new DecimalFormat("000000");

	public static String getFourBitRandom() {
		return fourdf.format(random.nextInt(10000));
	}

	public static String getSixBitRandom() {
		return sixdf.format(random.nextInt(1000000));
	}

	/**
	 * 给定数组,抽取n个数据
	 * @param list
	 * @param n
	 * @return
	 */
	public static ArrayList getRandom(List list, int n) {

		Random random = new Random();

		HashMap<Object, Object> hashMap = new HashMap<Object, Object>();

		// 生成随机数字并存入HashMap
		for (int i = 0; i < list.size(); i++) {

			int number = random.nextInt(100) + 1;

			hashMap.put(number, i);
		}

		// 从HashMap导入数组
		Object[] robjs = hashMap.values().toArray();

		ArrayList r = new ArrayList();

		// 遍历数组并打印数据
		for (int i = 0; i < n; i++) {
			r.add(list.get((int) robjs[i]));
			System.out.print(list.get((int) robjs[i]) + "\t");
		}
		System.out.print("\n");
		return r;
	}
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值