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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

阿里云发送短信

發布時間:2023/12/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 阿里云发送短信 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.具備條件

1.阿里云開通短信服務

進入阿里云搜索短信就有對應的短信服務,支付寶購買后進入短信控制臺,會有對應的短信服務等信息。包括國內學習、業務統計、系統設置等。

注意:簽名和模板不支持個人用戶申請未上線業務

?2.控制臺測試

填寫對應信息即可,手機就會收到驗證碼信息。

?2.java整合短信服務

1.代碼開發

1.獲取?AccessKey和secret-key

因為之前做oss對象存儲的時候已經獲取過AccessKey和secret-key,所以需要在用戶中綁定并授權短信服務即可。

?2.SpringBoot整合阿里云短信

首先看下官方文檔的介紹與說明。

1.pom文件

<dependency><groupId>com.aliyun</groupId><artifactId>dysmsapi20170525</artifactId><version>2.0.18</version> </dependency>

2.配置文件

?spring.cloud.nacos.discovery.server-addr:nacos服務注冊地址

?alicloud.secrect-key:密鑰

?alicloud.access-key:密鑰

?sms.endpoint:地域節點

?sms.templateCode:模板

3.封裝組件

其實就是把官方文檔的代碼拿過來自己用。

@Component @Data @ConfigurationProperties(prefix = "spring.cloud.alicloud.sms") //和配置文件綁定 public class SmsComponent {// private String host; // private String path; // private String templateId; // private String appcode;@Value("${spring.cloud.alicloud.access-key}")private String accessId;@Value("${spring.cloud.alicloud.secret-key}")private String accessKey;/*** 配置文件對應的*/private String endpoint;private String templateCode; //public void sendSmsCode(String phone,String code) throws Exception { // // // String method = "POST"; // // Map<String, String> headers = new HashMap<String, String>(); // //最后在header中的格式(中間是英文空格)為Authorization:APPCODE 83359fd73fe94948385f570e3c139105 // headers.put("Authorization", "APPCODE " + appcode); // //根據API的要求,定義相對應的Content-Type // headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // Map<String, String> querys = new HashMap<String, String>(); // Map<String, String> bodys = new HashMap<String, String>(); // bodys.put("mobile", phone); // bodys.put("tag", code); // bodys.put("templateId", templateId); // // // try { // // HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); // System.out.println(response.toString()); // //獲取response的body // System.out.println(EntityUtils.toString(response.getEntity())); // } catch (Exception e) { // e.printStackTrace(); // } // }Config config = new Config()// 您的 AccessKey ID.setAccessKeyId(accessId)// 您的 AccessKey Secret.setAccessKeySecret(accessKey);// 訪問的域名config.endpoint = endpoint;Client client = new Client(config);SendSmsRequest sendSmsRequest = new SendSmsRequest().setSignName("阿里云短信測試").setTemplateCode(templateCode).setPhoneNumbers(phone).setTemplateParam("{\"code\":\""+code+"\"}");RuntimeOptions runtime = new RuntimeOptions();SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);System.out.println(sendSmsResponse);}}

4.發送短信接口

@ResponseBody@GetMapping("/sms/sendCode")public R sendCode(@RequestParam("phone") String phone) throws Exception {String redisCode = stringRedisTemplate.opsForValue().get(AuthConstant.SMS_CODE_CACHE_PREFIX + phone);if (!StringUtils.isEmpty(redisCode)) {long currentTime = Long.parseLong(redisCode.split("_")[1]);//系統時間減去當前時間小于60s不能發送if (System.currentTimeMillis() - currentTime < 60000) {return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(), BizCodeEnum.SMS_CODE_EXCEPTION.getMsg());}}//接口防刷//驗證碼的再次校驗 redis保存 存key->手機號 value->驗證碼 sms:code:1234567898--->123456String code = UUID.randomUUID().toString().substring(0, 5);String subString = code + "_" + System.currentTimeMillis();//redis緩存驗證碼 防止同一個手機號在60s內再次發送驗證碼stringRedisTemplate.opsForValue().set(AuthConstant.SMS_CODE_CACHE_PREFIX + phone, subString, 10, TimeUnit.MINUTES);feignService.sendCode(phone, code);return R.ok();}

?需要將前端傳遞的手機號進行發送短信收取驗證碼判斷,如果redis中為空調取短信發送服務(通過feign),如果redis不為空,判斷系統時間減去當前時間小于60s不能發送,這樣有效防止接口防刷惡意操作。前端注冊通過手機號調起后端接口,后端通過uuid等技術生成驗證碼并調取短信服務接口,使用戶收取驗證碼。

3.MD5&MD5鹽值加密

1.MD5

Message Digest algorithm 5,信息摘要算法

  • 壓縮性:任意長度的數據,算出的 MD5 值長度都是固定的;
  • 容易計算:從原數據計算出 MD5 值很容易;
  • 抗修改性:對原數據進行任何改動,哪怕只修改 1 個字節,所得到的 MD5 值都有很大區別;
  • 強抗碰撞:想找到兩個不同的數據,使它們具有相同的 MD5 值是非常困難的;
  • 不可逆
  • @Testvoid contextLoads() {// e10adc3949ba59abbe56e057f20f883e // md5不能直接用來存儲加密密碼//可破解 值一直不變 抗修改性: 利用彩虹表 暴力破解法// 鹽值加密 // String s = DigestUtils.md5Hex("123456"); // System.out.println(s); // // //加鹽:$1$+八位字符 每次鹽值加密不一樣 // String s1 = Md5Crypt.md5Crypt(s.getBytes(),"$1$qwertyui");//$1$qwertyui$vP2GtrM4.h4RUK.3HGS9J. // System.out.println(s1);//BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();String encode = bCryptPasswordEncoder.encode("123456"); // $2a$10$1lc96GWrG7.Lte4f9FsniOAUbpc4t4oaPhXpyG3OiLAxrfsjXqdB6 // $2a$10$2gXOTIfzQDw4VYKhKWNWreNUxCiySPnhAhJiCVLTaXyo6vlPGDPbSboolean matches = bCryptPasswordEncoder.matches("123456", "$2a$10$2gXOTIfzQDw4VYKhKWNWreNUxCiySPnhAhJiCVLTaXyo6vlPGDPbS");System.out.println(encode+"========>"+matches);}

    encode產生的和123456始終都會匹配。

    @Overridepublic MemberEntity login(MemberLoginVo vo) {//獲取用戶名String loginAccount = vo.getLoginAccount();//獲取用戶的密碼String pagePassword = vo.getPassword();//去數據庫查詢MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginAccount).or().eq("mobile", loginAccount));if (memberEntity == null) {//登錄失敗return null;} else {//從數據庫返回的密碼字段值String password = memberEntity.getPassword();BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();//用頁面提交的代碼和數據庫注冊的鹽值進行匹配boolean b = passwordEncoder.matches(pagePassword, password);if (b) {return memberEntity;} else {return null;}}

    總結

    以上是生活随笔為你收集整理的阿里云发送短信的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。