地址:https://cloud.tencent.com/act/free?from=14600
选完后等待审核!!如果没问题,回是这样的!
地址:https://console.cloud.tencent.com/cam/capi
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>4.0.11</version>
</dependency>
lanys:
txunyun:
#腾讯云API密钥管理 id
secret_id: AKIDPcZFemeFZcPwCBtL*********WkhNmu
#腾讯云API密钥管理 key
secret_key: sTnqd8MJ8gx***********lMfhOo
#短信应用SDK AppID
app_id: 140*******6448
#短信模板ID,需要在短信应用中申请
template_id: 1102*****969
#签名,使用是签名内容,而不是签名ID
sms_sign: 知识记忆
/**
* @author lanys
* @Description: 腾讯云短信配置
* @date 2/9/2021 上午10:20
*/
@Configuration
public class TenxunyunSmsConfig {
@Value("${lanys.txunyun.secret_id}")
private String secretId;
@Value("${lanys.txunyun.secret_key}")
private String secretKey;
private String endpoint = "sms.tencentcloudapi.com";
@Bean
public SmsClient smsClient() {
/* 必要步骤:
* 实例化一个认证对象,入参需要传入腾讯云账户密钥对 secretId 和 secretKey
* 本示例采用从环境变量读取的方式,需要预先在环境变量中设置这两个值
* 您也可以直接在代码中写入密钥对,但需谨防泄露,不要将代码复制、上传或者分享给他人
* CAM 密钥查询:https://console.cloud.tencent.com/cam/capi
*/
Credential credential = new Credential(secretId, secretKey);
// 实例化一个 http 选项,可选,无特殊需求时可以跳过
HttpProfile httpProfile = new HttpProfile();
//SDK 默认使用 POST 方法。
httpProfile.setReqMethod("POST");
//SDK 有默认的超时时间,非必要请不要进行调整
httpProfile.setConnTimeout(60);
//SDK 会自动指定的域名
httpProfile.setEndpoint(endpoint);
//实例化一个客户端配置对象,可以指定超时时间等配置
ClientProfile clientProfile = new ClientProfile();
clientProfile.setSignMethod("HmacSHA256");
clientProfile.setHttpProfile(httpProfile);
//实例化 SMS 的 client 对象
return new SmsClient(credential, "ap-guangzhou", clientProfile);
}
}
/**
* @author lanys
* @Description:
* @date 1/9/2021 上午11:48
*/
public interface ISmsService {
/**
* 发送手机验短信证码
*
* @param phone 电话号码
* @return 返回数据
*/
String sendLoginSms(String phone);
/**
* 验证验证码
*
* @param code 验证码
* @param phone 手机号
* @return Boolean
*/
Boolean validationCode(String phone,String code);
}
/**
* @author lanys
* @Description:
* @date 1/9/2021 上午11:49
*/
@Service
@Slf4j
public class SmsServiceImpl implements ISmsService {
@Autowired
private SmsClient smsClient;
@Value("${lanys.txunyun.app_id}")
private String appId;
@Value("${lanys.txunyun.template_id}")
private String templateId;
@Value("${lanys.txunyun.sms_sign}")
private String smsSign;
/**
* 短信验证码长度
*/
private final Integer LENGTH = 6;
/**
* redis 缓存
*/
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 发送手机验短信证码
*
* @param phone 电话号码
* @return 返回数据
*/
@Override
public String sendLoginSms(String phone) {
String[] phoneNumbers = {"+86" + phone};
//生成随机验证码
final String code = generateCode();
//加入数组
String[] templateParams = {code};
//实例请求
SendSmsRequest sendSmsRequest = new SendSmsRequest();
//设置[短信控制台] 添加应用后生成的实际 SDKAppID
sendSmsRequest.setSmsSdkAppid(appId);
//短信签名内容
sendSmsRequest.setSign(smsSign);
//模板 ID
sendSmsRequest.setTemplateID(templateId);
//电话
sendSmsRequest.setPhoneNumberSet(phoneNumbers);
//验证码
sendSmsRequest.setTemplateParamSet(templateParams);
try {
//发送
final SendSmsResponse sendSmsResponse = smsClient.SendSms(sendSmsRequest);
log.info("短信发送成功:{}",sendSmsResponse.toString());
//加入缓存
redisTemplate.opsForValue().set(phone,code,1, TimeUnit.MINUTES);
return "ok";
} catch (TencentCloudSDKException e) {
log.error("发送失败,或者剩余短信数量不足",e);
}
return "发送失败,或者剩余短信数量不足";
}
/**
* 验证验证码
*
* @param code 验证码
* @param phone 手机号
* @return Boolean
*/
@Override
public Boolean validationCode(String phone,String code) {
final String data = (String) redisTemplate.opsForValue().get(phone);
if (code.equals(data)){
return true;
}
return false;
}
/**
* 生成随机的验证码
*
* @return
*/
public String generateCode() {
return RandomStringUtils.randomNumeric(LENGTH);
}
}
@ApiOperation(value = "发送短信和验证")
@ResponseBody
@GetMapping("/send")
@ApiImplicitParams({
@ApiImplicitParam(name = "phone", value = "手机号", required = true, example = "18376662071", paramType = "query", dataTypeClass = Integer.class),
@ApiImplicitParam(name = "code",value = "验证码验证",required = false,example = "1234",paramType = "query")
})
public Result sendLoginSms(@NotEmpty(message = "非法的手机号") @Pattern(regexp = "^1[0-9]{10}$", message = "非法的手机号") String phone,String code) throws IOException {
if (StringUtils.isNotBlank(code)){
if (smsService.validationCode(phone,code)){
return Result.success("验证码正确");
}
return Result.success("验证码错误");
}
String result = smsService.sendLoginSms(phone);
return Result.success(result);
}
年轻人,什么都想去尝试,加油。