SpringBoot整合阿里云短信服務(wù)的方法
一、新建短信微服務(wù)
1、在service模塊下創(chuàng)建子模塊service-msm
2.創(chuàng)建controller和service代碼
3.配置application.properties
# 服務(wù)端口 server.port=8006 # 服務(wù)名 spring.application.name=service-msm # mysql數(shù)據(jù)庫連接 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root spring.redis.host=192.168.44.131 spring.redis.port=6379 spring.redis.database= 0 spring.redis.timeout=1800000 spring.redis.lettuce.pool.max-active=20 spring.redis.lettuce.pool.max-wait=-1 #最大阻塞等待時(shí)間(負(fù)數(shù)表示沒限制) spring.redis.lettuce.pool.max-idle=5 spring.redis.lettuce.pool.min-idle=0 #最小空閑 #返回json的全局時(shí)間格式 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 #配置mapper xml文件的路徑 mybatis-plus.mapper-locations=classpath:com/atguigu/cmsservice/mapper/xml/*.xml #mybatis日志 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
4、創(chuàng)建啟動(dòng)類
@ComponentScan({"com.south"})
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消數(shù)據(jù)源自動(dòng)配置
public class ServiceMsmApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceMsmApplication.class, args);
}
}
二、阿里云短信服務(wù)
幫助文檔:
https://help.aliyun.com/product/44282.html?spm=5176.10629532.0.0.38311cbeYzBm73
三、編寫發(fā)送短信接口
1.在service-msm的pom中引入依賴
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</dependency>
</dependencies>
2.編寫controller,根據(jù)手機(jī)號(hào)發(fā)送短信
@CrossOrigin //跨域
public class MsmApiController {
@Autowired
private MsmService msmService;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@GetMapping(value = "/send/{phone}")
public R code(@PathVariable String phone) {
String code = redisTemplate.opsForValue().get(phone);
if(!StringUtils.isEmpty(code)) return R.ok();
code = RandomUtil.getFourBitRandom();
Map<String,Object> param = new HashMap<>();
param.put("code", code);
boolean isSend = msmService.send(phone, "SMS_180051135", param);
if(isSend) {
redisTemplate.opsForValue().set(phone, code,5,TimeUnit.MINUTES);
return R.ok();
} else {
return R.error().message("發(fā)送短信失敗");
}
}
}
3.編寫service
@Service
public class MsmServiceImpl implements MsmService {
/**
* 發(fā)送短信
*/
public boolean send(String PhoneNumbers, String templateCode, Map<String,Object> param) {
if(StringUtils.isEmpty(PhoneNumbers)) return false;
DefaultProfile profile =
DefaultProfile.getProfile("default", "LTAIq6nIPY09VROj", "FQ7UcixT9wEqMv9F35nORPqKr8XkTF");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("PhoneNumbers", PhoneNumbers);
request.putQueryParameter("SignName", "我的谷粒在線教育網(wǎng)站");
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
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;
}
}
到此這篇關(guān)于SpringBoot整合阿里云短信服務(wù)的文章就介紹到這了,更多相關(guān)SpringBoot阿里云短信服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解SpringCloud是如何動(dòng)態(tài)更新配置的
spring cloud在config配置管理的基礎(chǔ)上,提供了consul config的配置管理和動(dòng)態(tài)監(jiān)聽,那么這里面到底是怎樣實(shí)現(xiàn)的,本文將為你揭秘,感興趣的小伙伴可以跟著小伙伴一起來學(xué)習(xí)2023-06-06
java 中HttpClient傳輸xml字符串實(shí)例詳解
這篇文章主要介紹了java 中HttpClient傳輸xml字符串實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Mybatis調(diào)用MySQL存儲(chǔ)過程的簡(jiǎn)單實(shí)現(xiàn)
本篇文章主要介紹了Mybatis調(diào)用MySQL存儲(chǔ)過程的簡(jiǎn)單實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
Java使用Hutool+自定義注解實(shí)現(xiàn)數(shù)據(jù)脫敏
我們?cè)谑褂檬謾C(jī)銀行的時(shí)候經(jīng)常能看到APP上會(huì)將銀行卡的卡號(hào)中間部分給隱藏掉使用 ***** 來代替,在某些網(wǎng)站上查看一些業(yè)務(wù)密碼時(shí)(例如簽到密碼等)也會(huì)使用 ***** 來隱藏掉真正的密碼,那么這種方式是如何實(shí)現(xiàn)的呢,本文將給大家介紹使用Hutool+自定義注解實(shí)現(xiàn)數(shù)據(jù)脫敏2023-09-09
springboot3 使用 jasypt 加密配置文件的使用步驟
在SpringBoot項(xiàng)目中,使用Jasypt加密配置文件可以有效保護(hù)敏感信息,首先,需添加Jasypt依賴并配置加密密碼,可在application.properties或通過啟動(dòng)參數(shù)、環(huán)境變量設(shè)置,本文介紹了Jasypt的配置步驟及使用方法,幫助開發(fā)者保護(hù)應(yīng)用配置信息2024-11-11
SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對(duì)數(shù)據(jù)庫
這篇文章主要介紹了SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對(duì)數(shù)據(jù)庫密碼加密的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Java并發(fā)工具之CountDownLatch使用詳解
這篇文章主要介紹了Java并發(fā)工具之CountDownLatch使用詳解,通過使用 CountDownLatch可以使當(dāng)前線程阻塞,等待其他線程完成給定任務(wù),可以類比旅游團(tuán)導(dǎo)游要等待所有的游客到齊后才能去下一個(gè)景點(diǎn),需要的朋友可以參考下2023-12-12

