整合腾讯云短信服务
腾讯云短信服务申请
阿里短信不好申请可以使用腾讯的:微信公众号方式申请
前提:自己去创建一个微信公众号
创建签名(签名类型:公众号, 证明类型:公众号设置页面截图)
导入依赖
<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;
}
}