SpringBoot整合阿里云開(kāi)通短信服務(wù)詳解
準(zhǔn)備工作
開(kāi)通短信服務(wù)
如果開(kāi)通不成功,就只能借下別人已經(jīng)開(kāi)通好的短信,如果不想重復(fù),可在其下創(chuàng)建一個(gè)新的模板管理
這里只是介紹如何使用
導(dǎo)入依賴
com.aliyun aliyun-java-sdk-core 4.5.1 com.aliyun aliyun-java-sdk-dysmsapi 1.1.0 com.alibaba fastjson 1.2.62
發(fā)送驗(yàn)證碼到手機(jī)上,驗(yàn)證碼生成工具類(內(nèi)容較為固定,也可根據(jù)需求改)
package com.xsha.msmservice.utils;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
/**
* 說(shuō)明:短信配置
* 作者:FH Admin
* from:fhadmin.cn
*/
public class RandomUtil {
private static final Random random = new Random();
private static final DecimalFormat fourdf = new DecimalFormat("0000");
private static final DecimalFormat sixdf = new DecimalFormat("000000");
public static String getFourBitRandom() {
return fourdf.format(random.nextInt(10000));
}
public static String getSixBitRandom() {
return sixdf.format(random.nextInt(1000000));
}
/**
* 給定數(shù)組,抽取n個(gè)數(shù)據(jù)
* @param list
* @param n
* @return
*/
public static ArrayList getRandom(List list, int n) {
Random random = new Random();
HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
// 生成隨機(jī)數(shù)字并存入HashMap
for (int i = 0; i < list.size(); i++) {
int number = random.nextInt(100) + 1;
hashMap.put(number, i);
}
// 從HashMap導(dǎo)入數(shù)組
Object[] robjs = hashMap.values().toArray();
ArrayList r = new ArrayList();
// 遍歷數(shù)組并打印數(shù)據(jù)
for (int i = 0; i < n; i++) {
r.add(list.get((int) robjs[i]));
System.out.print(list.get((int) robjs[i]) + "\t");
}
System.out.print("\n");
return r;
}
}發(fā)送驗(yàn)證碼,驗(yàn)證碼是有有效時(shí)間的(時(shí)間可以自己設(shè)置)
這里可以創(chuàng)建常量類讀取配置文件的阿里云密鑰等信息
package com.xsha.msmservice.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xsha.msmservice.service.MsmService;
import com.xsha.msmservice.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* 說(shuō)明:短信配置
* 作者:FH Admin
* from:fhadmin.cn
*/
@Service
public class MsmServiceImpl implements MsmService {
// 注入redis緩存對(duì)象
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Override
public boolean sendMessage(String phone) {
if(StringUtils.isEmpty(phone)) return false;
// 先獲取手機(jī)號(hào)對(duì)應(yīng)的驗(yàn)證碼(該驗(yàn)證碼沒(méi)過(guò)期)
String code = redisTemplate.opsForValue().get(phone);
if(!StringUtils.isEmpty(code)) {
return true;
}
// 過(guò)期則生成隨機(jī)驗(yàn)證碼,并在發(fā)送之后向redis中存入手機(jī)號(hào)對(duì)應(yīng)的驗(yàn)證碼
code = RandomUtil.getFourBitRandom();
Map<String, Object> map = new HashMap<>();
map.put("code", code);
// 設(shè)置短信配置
DefaultProfile profile = DefaultProfile.getProfile("your region", "your accessId", "your accessSecret");
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers(phone);//接收短信的手機(jī)號(hào)碼
request.setSignName("your signature name");//短信簽名名稱
request.setTemplateCode("your template");//短信模板CODE
request.setTemplateParam(JSONObject.toJSONString(map));//短信模板變量對(duì)應(yīng)的實(shí)際值
try {
SendSmsResponse response = client.getAcsResponse(request);
// 發(fā)送短信,盡量打印出來(lái)是否發(fā)送成功
new Gson().toJson(response);
// 將驗(yàn)證碼放置在redis緩存中,并設(shè)置5分鐘有效時(shí)間,最后一個(gè)參數(shù)是單位
redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
} catch (ServerException e) {
e.printStackTrace();
return false;
} catch (ClientException e) {
e.printStackTrace();
return false;
}
return true;
}
}到此這篇關(guān)于SpringBoot整合阿里云開(kāi)通短信服務(wù)詳解的文章就介紹到這了,更多相關(guān)SpringBoot阿里云短信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用hutool-captcha實(shí)現(xiàn)驗(yàn)證碼生成與驗(yàn)證
在springboot的登陸頁(yè)面中為了防止機(jī)器大規(guī)模注冊(cè),機(jī)器暴力破解數(shù)據(jù)密碼等危害,需要驗(yàn)證隨機(jī)生成的驗(yàn)證碼,本文主要介紹了SpringBoot使用hutool-captcha實(shí)現(xiàn)驗(yàn)證碼生成與驗(yàn)證,感興趣的可以了解一下2023-12-12
Java字符串相關(guān)類StringBuffer的用法詳解
java.lang包下的StringBuffer類,代表著可變的字符序列,可以用來(lái)對(duì)字符串內(nèi)容進(jìn)行增刪改操作。本文將通過(guò)示例詳細(xì)說(shuō)說(shuō)它的用法,感興趣的可以跟隨小編一起學(xué)習(xí)一下2022-10-10
SpringSecurity進(jìn)行認(rèn)證與授權(quán)的示例代碼
SpringSecurity是Spring家族中的一個(gè)安全管理框架,而認(rèn)證和授權(quán)也是SpringSecurity作為安全框架的核心功能,本文主要介紹了SpringSecurity進(jìn)行認(rèn)證與授權(quán)的示例代碼,感興趣的可以了解一下2024-06-06
JavaWeb動(dòng)態(tài)導(dǎo)出Excel可彈出下載
這篇文章主要介紹了JavaWeb動(dòng)態(tài)導(dǎo)出Excel,對(duì)Excel可彈出進(jìn)行下載操作,感興趣的小伙伴們可以參考一下2016-03-03
java Collection 之List學(xué)習(xí)介紹
本篇文章小編為大家介紹,java Collection 之List學(xué)習(xí)介紹。需要的朋友參考下2013-04-04
Java實(shí)現(xiàn)XML與JSON秒級(jí)轉(zhuǎn)換示例詳解
這篇文章主要為大家介紹了Java實(shí)現(xiàn)XML與JSON秒級(jí)轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Java代碼實(shí)現(xiàn)Map和Object互轉(zhuǎn)及Map和Json互轉(zhuǎn)
這篇文章主要介紹了Java代碼實(shí)現(xiàn)map和Object互轉(zhuǎn)及Map和json互轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2016-05-05

