SpringBoot整合阿里云開通短信服務詳解
準備工作
開通短信服務
如果開通不成功,就只能借下別人已經(jīng)開通好的短信,如果不想重復,可在其下創(chuàng)建一個新的模板管理
這里只是介紹如何使用
導入依賴
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ā)送驗證碼到手機上,驗證碼生成工具類(內(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;
/**
* 說明:短信配置
* 作者: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個數(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>();
// 生成隨機數(shù)字并存入HashMap
for (int i = 0; i < list.size(); i++) {
int number = random.nextInt(100) + 1;
hashMap.put(number, i);
}
// 從HashMap導入數(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ā)送驗證碼,驗證碼是有有效時間的(時間可以自己設置)
這里可以創(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;
/**
* 說明:短信配置
* 作者:FH Admin
* from:fhadmin.cn
*/
@Service
public class MsmServiceImpl implements MsmService {
// 注入redis緩存對象
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Override
public boolean sendMessage(String phone) {
if(StringUtils.isEmpty(phone)) return false;
// 先獲取手機號對應的驗證碼(該驗證碼沒過期)
String code = redisTemplate.opsForValue().get(phone);
if(!StringUtils.isEmpty(code)) {
return true;
}
// 過期則生成隨機驗證碼,并在發(fā)送之后向redis中存入手機號對應的驗證碼
code = RandomUtil.getFourBitRandom();
Map<String, Object> map = new HashMap<>();
map.put("code", code);
// 設置短信配置
DefaultProfile profile = DefaultProfile.getProfile("your region", "your accessId", "your accessSecret");
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers(phone);//接收短信的手機號碼
request.setSignName("your signature name");//短信簽名名稱
request.setTemplateCode("your template");//短信模板CODE
request.setTemplateParam(JSONObject.toJSONString(map));//短信模板變量對應的實際值
try {
SendSmsResponse response = client.getAcsResponse(request);
// 發(fā)送短信,盡量打印出來是否發(fā)送成功
new Gson().toJson(response);
// 將驗證碼放置在redis緩存中,并設置5分鐘有效時間,最后一個參數(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;
}
}到此這篇關于SpringBoot整合阿里云開通短信服務詳解的文章就介紹到這了,更多相關SpringBoot阿里云短信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot使用hutool-captcha實現(xiàn)驗證碼生成與驗證
在springboot的登陸頁面中為了防止機器大規(guī)模注冊,機器暴力破解數(shù)據(jù)密碼等危害,需要驗證隨機生成的驗證碼,本文主要介紹了SpringBoot使用hutool-captcha實現(xiàn)驗證碼生成與驗證,感興趣的可以了解一下2023-12-12
SpringSecurity進行認證與授權(quán)的示例代碼
SpringSecurity是Spring家族中的一個安全管理框架,而認證和授權(quán)也是SpringSecurity作為安全框架的核心功能,本文主要介紹了SpringSecurity進行認證與授權(quán)的示例代碼,感興趣的可以了解一下2024-06-06
Java實現(xiàn)XML與JSON秒級轉(zhuǎn)換示例詳解
這篇文章主要為大家介紹了Java實現(xiàn)XML與JSON秒級轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09
Java代碼實現(xiàn)Map和Object互轉(zhuǎn)及Map和Json互轉(zhuǎn)
這篇文章主要介紹了Java代碼實現(xiàn)map和Object互轉(zhuǎn)及Map和json互轉(zhuǎn)的相關資料,需要的朋友可以參考下2016-05-05

