日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

请求第三方阿里发送验证码

發(fā)布時(shí)間:2024/3/12 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 请求第三方阿里发送验证码 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

第一步,發(fā)送驗(yàn)證碼

public String sendSms(String mobile) {DefaultProfile profile = DefaultProfile.getProfile(this.aliyunSMSConfig.getRegionId(),this.aliyunSMSConfig.getAccessKeyId(), this.aliyunSMSConfig.getAccessKeySecret());IAcsClient client = new DefaultAcsClient(profile);String code = RandomUtils.nextInt(100000, 999999) + "";//生成六位數(shù)的字符串CommonRequest request = new CommonRequest();request.setSysMethod(MethodType.POST);request.setSysDomain(this.aliyunSMSConfig.getDomain());request.setSysVersion("2017-05-25");request.setSysAction("SendSms");request.putQueryParameter("RegionId", this.aliyunSMSConfig.getRegionId());request.putQueryParameter("PhoneNumbers", mobile);request.putQueryParameter("SignName", this.aliyunSMSConfig.getSignName()); //簽名名稱request.putQueryParameter("TemplateCode", this.aliyunSMSConfig.getTemplateCode()); //短信模板coderequest.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");//模板中變量替換try {CommonResponse response = client.getCommonResponse(request);String data = response.getData();if (StringUtils.contains(data, "\"Message\":\"OK\"")) {return code;}log.info("發(fā)送短信驗(yàn)證碼失敗~ data = " + data);} catch (Exception e) {log.error("發(fā)送短信驗(yàn)證碼失敗~ mobile = " + mobile, e);}return null;}

第二步,校驗(yàn)驗(yàn)證碼是否正確

/*** 發(fā)送短信驗(yàn)證碼* 實(shí)現(xiàn):發(fā)送完成短信驗(yàn)證碼后,需要將驗(yàn)證碼保存到redis中* @param phone* @return*/public ErrorResult sendCheckCode(String phone) {//把手機(jī)號(hào)為key,code為值String redisKey = "CHECK_CODE_" + phone;//先判斷該手機(jī)號(hào)發(fā)送的驗(yàn)證碼是否還未失效if(this.redisTemplate.hasKey(redisKey)){String msg = "上一次發(fā)送的驗(yàn)證碼還未失效!";return ErrorResult.builder().errCode("000001").errMessage(msg).build();}//給手機(jī)發(fā)送驗(yàn)證碼 // String code = this.sendSms(phone);String code="123456";//判斷驗(yàn)證碼是否發(fā)送成功if(StringUtils.isEmpty(code)){//如果為空發(fā)送失敗String msg = "發(fā)送短信驗(yàn)證碼失敗!";//返回失敗的值return ErrorResult.builder().errCode("000000").errMessage(msg).build();}//短信發(fā)送成功,將驗(yàn)證碼保存到redis中,有效期為5分鐘this.redisTemplate.opsForValue().set(redisKey, code, Duration.ofMinutes(5));//驗(yàn)證碼發(fā)送成功不用返回空return null;}

編寫配置類

import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource;@Data @Configuration @PropertySource("aliyun.properties")//加載指定的配置文件 @ConfigurationProperties(prefix = "aliyun.sms")//加載配置文件以aliyun.sms開頭的 public class AliyunSMSConfig {private String regionId;private String accessKeyId;private String accessKeySecret;private String domain;private String signName;private String templateCode; }

配置文件

```xml aliyun.sms.regionId = cn-hangzhou aliyun.sms.accessKeyId = LTAI5tCix6mvUNvDWFYe9waGaliyun.sms.accessKeySecret = 0EUNiz7mCWnVpatLi5QMopPvfAG9lqaliyun.sms.domain= dysmsapi.aliyuncs.com aliyun.sms.signName= 聚賢莊aliyun.sms.templateCode= SMS_208640690

總結(jié)

以上是生活随笔為你收集整理的请求第三方阿里发送验证码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。