If that.
I used here is Tencent cloud messaging platform, on the grounds that, for the first time presented the opening 100 SMS
After personal real-name authentication, enter https://cloud.tencent.com/act/free
According steps to fill and application templates
Next, create API keys https://console.cloud.tencent.com/cam/capi
Click to continue to use, click New keys
Get the code
1 import com.tencentcloudapi.common.Credential; 2 import com.tencentcloudapi.common.profile.ClientProfile; 3 import com.tencentcloudapi.common.profile.HttpProfile; 4 import com.tencentcloudapi.common.exception.TencentCloudSDKException; 5 6 import com.tencentcloudapi.sms.v20190711.SmsClient; 7 8 import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest; 9 import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse; 10 11 public class SendSms 12 { 13 public static void main(String [] args) { 14 try{ 15 16 Credential cred = new Credential("【SecretId】", "【SecretKey】"); 17 18 HttpProfile httpProfile = new HttpProfile(); 19 httpProfile.setEndpoint("sms.tencentcloudapi.com"); 20 21 ClientProfile clientProfile = new ClientProfile(); 22 clientProfile.setHttpProfile(httpProfile); 23 24 SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile); 25 26 String params = "{\"PhoneNumberSet\":[\"+86【电话号码】\"],\"TemplateID\":\"【模板ID】\",\"SmsSdkAppid\":\"【SmsSdkAppid】\"}"; 27 SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class); 28 29 SendSmsResponse resp = client.SendSms(req); 30 31 System.out.println(SendSmsRequest.toJsonString(resp)); 32 } catch (TencentCloudSDKException e) { 33 System.out.println(e.toString()); 34 } 35 36 } 37 38 }
Then you need to add pom-dependent:
. 1 < dependency > 2 < the groupId > com.tencentcloudapi </ the groupId > . 3 < the artifactId > tencentcloud-SDK-Java </ the artifactId > . 4 < Version > 3.1.27 </ Version > ! <- Note: This is only an example version number , please obtain the latest version https://mvnrepository.com/artifact/com.tencentcloudapi/tencentcloud-sdk-java -> 5 </ dependency >
Then you can use it.
---------------------------------
Suggestion tool into the package, the incoming phone number and verification code, is set to a static method.
Spring Boot codes used in combination Redis interface code:
1 package top.bigking.backstage.service.impl; 2 3 import org.springframework.data.redis.core.RedisTemplate; 4 import org.springframework.data.redis.serializer.StringRedisSerializer; 5 import org.springframework.stereotype.Service; 6 import top.bigking.backstage.service.VerifyCodeService; 7 import top.bigking.backstage.utils.SendSms; 8 9 import javax.annotation.Resource; 10 import java.util.Random; 11 import java.util.concurrent.TimeUnit; 12 13 /** 14 * @Author ABKing 15 * @since 2020/4/2 下午5:41 16 **/ 17 @Service 18 public class VerifyCodeServiceImpl implements VerifyCodeService { 19 @Resource 20 private RedisTemplate<String, String> redisTemplate; 21 22 @Override 23 public Boolean setVerifyCode(String phoneNum) { 24 redisTemplate.setKeySerializer(new StringRedisSerializer()); 25 redisTemplate.setValueSerializer(new new StringRedisSerializer ()); 26 is String codeKey = "Verify_code:" + phoneNum + ": code" ; 27 String codeCount = "Verify_code:" + phoneNum + ": COUNT" ; 28 String code = the getCode (. 6 ); 29 SendSms. sendSms (phoneNum, code); 30 // a number one day up to three times the transmission codes 31 is String COUNT = redisTemplate.opsForValue () GET (codeCount);. 32 IF (COUNT == null ) { 33 is . redisTemplate.opsForValue () SET (codeCount, ". 1",. 1 , TimeUnit.DAYS); 34 // codes 60 seconds expire 35 redisTemplate.opsForValue().set(codeKey, code, 60, TimeUnit.SECONDS); 36 return true; 37 }else if(Integer.parseInt(count) <= 2){ 38 redisTemplate.opsForValue().set(codeCount, String.valueOf(Integer.parseInt(count) + 1), 1, TimeUnit.DAYS); 39 //验证码60秒过期 40 redisTemplate.opsForValue().set(codeKey, code, 60, TimeUnit.SECONDS); 41 return true; 42 } 43 return false; 44 } 45 Private String the getCode (Integer length) { 46 is String code = "" ; 47 the Random Random = new new the Random (); 48 for ( int I = 0; I <length; I ++ ) { 49 // set bound parameters, values in the range [0, bound), if not write parameters, int value of the range, 31 is ~ 2 ^ -2 ^ 31-1 50 code random.nextInt = + (10 ); 51 is } 52 is return code; 53 is } 54 is 55 @Override 56 is public String getVerifyCode (phoneNum String) { 57 is redisTemplate.setKeySerializer(new StringRedisSerializer()); 58 redisTemplate.setValueSerializer(new StringRedisSerializer()); 59 String codeKey = "Verify_code:" + phoneNum + ":code"; 60 return redisTemplate.opsForValue().get(codeKey); 61 } 62 63 }