SpringBoot整合阿里云短信驗證服務(wù)的使用示例

來源: CSDN
作者:BLUcoding
時間:2021-02-23
17539
添加 RAM 用戶并賦予權(quán)限:AliyunDysmsFullAccess(管理短信服務(wù)(SMS)的權(quán)限),添加短信模板并等待審核通過,記錄下模板CODE:SMS_205403229。

SpringBoot整合阿里云短信驗證服務(wù)的使用示例

1.添加 RAM 用戶并賦予權(quán)限:AliyunDysmsFullAccess(管理短信服務(wù)(SMS)的權(quán)限)

2.添加短信模板并等待審核通過,記錄下模板CODE:SMS_205403229

ia_500000006.png

3. 添加簽名,適用場景選擇驗證碼,等待審核通過,記錄下簽名名稱:BLU的java自學(xué)網(wǎng)站

4.SpringBoot項目導(dǎo)入依賴:

<dependency>

    <groupId>com.aliyun</groupId>

    <artifactId>aliyun-java-sdk-core</artifactId>

    <version>4.5.3</version>

</dependency>

<dependency>

    <groupId>com.alibaba</groupId>

    <artifactId>fastjson</artifactId>

    <version>1.2.62</version>

</dependency>

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-data-redis</artifactId>

</dependency>

測試類:

@Test

void contextLoads() {

    //連接阿里云

    DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "你的AccessKey ID", "你的AccessKey Secret");

    IAcsClient client = new DefaultAcsClient(profile);

    //構(gòu)建請求

    CommonRequest request = new CommonRequest();

    //下面的信息不要改

    request.setSysMethod(MethodType.POST);

    request.setSysDomain("dysmsapi.aliyuncs.com");

    request.setSysVersion("2017-05-25");

    request.setSysAction("SendSms");

    //自定義的參數(shù)(手機(jī)號、簽名和模版CODE)

    request.putQueryParameter("PhoneNumbers", "測試的手機(jī)號碼");

    request.putQueryParameter("SignName", "你的簽名名稱");

    request.putQueryParameter("TemplateCode", "你的模版CODE");

    //構(gòu)建一個短信驗證碼

    HashMap<String, Object> map = new HashMap<>();

    map.put("code", 2333);

    request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));

    //嘗試發(fā)送

    try {

        CommonResponse response = client.getCommonResponse(request);

        System.out.println(response.getData());

    } catch (ServerException e) {

        e.printStackTrace();

    } catch (ClientException e) {

        e.printStackTrace();

    }

}

ia_500000007.png

配置文件:

server.port=9090

spring.redis.host=127.0.0.1

spring.redis.port=6379

Service接口:

package com.blu.service;

import java.util.Map;

public interface SendSms {

public boolean send(String phoneNum,String trmplateCode,Map<String,Object> code);

}

Service實現(xiàn)類:

package com.blu.service.Impl;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import com.aliyuncs.CommonRequest;

import com.aliyuncs.CommonResponse;

import com.aliyuncs.DefaultAcsClient;

import com.aliyuncs.IAcsClient;

import com.aliyuncs.exceptions.ClientException;

import com.aliyuncs.exceptions.ServerException;

import com.aliyuncs.http.MethodType;

import com.aliyuncs.profile.DefaultProfile;

import com.blu.service.SendSms;

@Service

public class SendSmsImpl implements SendSms {

@Override

public boolean send(String phoneNum, String trmplateCode, Map<String, Object> code) {

// 連接阿里云

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "你的AccessKey ID",

"你的AccessKey Secret");

IAcsClient client = new DefaultAcsClient(profile);

// 構(gòu)建請求

CommonRequest request = new CommonRequest();

// 下面的信息不要改

request.setSysMethod(MethodType.POST);

request.setSysDomain("dysmsapi.aliyuncs.com");

request.setSysVersion("2017-05-25");

request.setSysAction("SendSms");

// 自定義的參數(shù)(手機(jī)號,驗證碼,簽名,模板)

request.putQueryParameter("PhoneNumbers", phoneNum);

request.putQueryParameter("SignName", "BLU的java自學(xué)網(wǎng)站");

request.putQueryParameter("TemplateCode", trmplateCode);

request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));

try {

CommonResponse response = client.getCommonResponse(request);

System.out.println(response.getData());

//返回是否成功

return response.getHttpResponse().isSuccess();

} catch (ServerException e) {

e.printStackTrace();

} catch (ClientException e) {

e.printStackTrace();

}

return false;

}

}

Controller接口:

package com.blu.controller;

import java.util.HashMap;

import java.util.UUID;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.web.bind.annotation.CrossOrigin;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RestController;

import com.aliyuncs.utils.StringUtils;

import com.blu.service.SendSms;

@RestController

@CrossOrigin

public class SmsApiController {

@Autowired

private SendSms sendSms;

@Autowired

private RedisTemplate<String,String> redisTemplate;

@GetMapping("send/{phone}")

public String code(@PathVariable("phone") String phone) {

String code = redisTemplate.opsForValue().get(phone);

if(!StringUtils.isEmpty(code)) {

return phone + ':' + code + "還沒有過期";

}else {

//生成4位驗證碼并存儲到 redis 中

code = UUID.randomUUID().toString().substring(0,4);

HashMap<String, Object> map = new HashMap<>();

map.put("code", code);

boolean isSend = sendSms.send(phone, "SMS_205403229", map);

if(isSend) {

                //將驗證碼存入redis,并設(shè)置1分鐘過期時間

redisTemplate.opsForValue().set(phone, code,1,TimeUnit.MINUTES);

return phone + ':' + code + "發(fā)送成功!";

}else {

return "發(fā)送失敗!";

}

}

}

}

測試請求:http://localhost:9090/send/1565177xxxx

ia_500000008.png

ia_500000009.png

立即登錄,閱讀全文
版權(quán)說明:
本文內(nèi)容來自于CSDN,本站不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。文章內(nèi)容系作者個人觀點,不代表快出海對觀點贊同或支持。如有侵權(quán),請聯(lián)系管理員(zzx@kchuhai.com)刪除!
相關(guān)文章
阿里云助力《誅仙世界》端游正式開服!
阿里云助力《誅仙世界》端游正式開服!
近?,完美世界游戲歷時多年打造的新國?仙俠MMORPG端游《誅仙世界》在阿?云上正式開服。
阿里云
云服務(wù)
2024-12-292024-12-29
一文詳解阿里云AI大基建
一文詳解阿里云AI大基建
面向AI時代,阿里云基礎(chǔ)設(shè)施是如何創(chuàng)新與發(fā)展的?計算、網(wǎng)絡(luò)、存儲、服務(wù)器、集群、可觀測等,阿里云全新升級的AI Infra到底有哪些重磅更新?
阿里云
云服務(wù)
2024-11-022024-11-02
AI時代云安全新范式,阿里云安全能力全線升級!
AI時代云安全新范式,阿里云安全能力全線升級!
AI時代,云安全面臨著新的挑戰(zhàn),不僅要持續(xù)面對以往的傳統(tǒng)問題,更需要全新理念落地于產(chǎn)品設(shè)計、技術(shù)演進(jìn)、架構(gòu)設(shè)計,才能實現(xiàn)效果、性能、和成本的最優(yōu)解。
AI
阿里云
云服務(wù)
2024-09-272024-09-27
連續(xù)四年!阿里云領(lǐng)跑中國公有云大數(shù)據(jù)平臺
連續(xù)四年!阿里云領(lǐng)跑中國公有云大數(shù)據(jù)平臺
近日,國際數(shù)據(jù)公司(IDC)發(fā)布《中國大數(shù)據(jù)平臺市場份額,2023:數(shù)智融合時代的真正到來》報告——2023年中國大數(shù)據(jù)平臺公有云服務(wù)市場規(guī)模達(dá)72.2億元人民幣,其中阿里巴巴市場份額保持領(lǐng)先,占比達(dá)40.2%,連續(xù)四年排名第一。
阿里云
云服務(wù)
2024-09-182024-09-18
優(yōu)質(zhì)服務(wù)商推薦
更多
掃碼登錄
打開掃一掃, 關(guān)注公眾號后即可登錄/注冊
加載中
二維碼已失效 請重試
刷新
賬號登錄/注冊
個人VIP
小程序
快出海小程序
公眾號
快出海公眾號
商務(wù)合作
商務(wù)合作
投稿采訪
投稿采訪
出海管家
出海管家