最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合阿里云短信服務(wù)的方法

 更新時(shí)間:2021年10月22日 09:58:20   作者:一生酷到底  
在實(shí)際項(xiàng)目中經(jīng)常有發(fā)送短信的功能,今天進(jìn)說一下SpringBoot整合阿里云短信服務(wù)的相關(guān)知識,新建短信微服務(wù),編寫發(fā)送短信接口的方法文中給大家介紹的很詳細(xì),需要的朋友參考下吧

一、新建短信微服務(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)建啟動類

@ComponentScan({"com.south"})
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)//取消數(shù)據(jù)源自動配置
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ī)號發(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解SpringCloud是如何動態(tài)更新配置的

    詳解SpringCloud是如何動態(tài)更新配置的

    spring cloud在config配置管理的基礎(chǔ)上,提供了consul config的配置管理和動態(tài)監(jiān)聽,那么這里面到底是怎樣實(shí)現(xiàn)的,本文將為你揭秘,感興趣的小伙伴可以跟著小伙伴一起來學(xué)習(xí)
    2023-06-06
  • 詳解Maven私服Nexus的安裝與使用

    詳解Maven私服Nexus的安裝與使用

    這篇文章主要介紹了詳解Maven私服Nexus的安裝與使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • java 中HttpClient傳輸xml字符串實(shí)例詳解

    java 中HttpClient傳輸xml字符串實(shí)例詳解

    這篇文章主要介紹了java 中HttpClient傳輸xml字符串實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • java三層架構(gòu)原理與作用小結(jié)

    java三層架構(gòu)原理與作用小結(jié)

    這篇文章主要對Java三層架構(gòu)的概念、作用等進(jìn)行了介紹,需要的朋友可以參考下
    2017-04-04
  • Mybatis調(diào)用MySQL存儲過程的簡單實(shí)現(xiàn)

    Mybatis調(diào)用MySQL存儲過程的簡單實(shí)現(xiàn)

    本篇文章主要介紹了Mybatis調(diào)用MySQL存儲過程的簡單實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • Java使用Hutool+自定義注解實(shí)現(xiàn)數(shù)據(jù)脫敏

    Java使用Hutool+自定義注解實(shí)現(xiàn)數(shù)據(jù)脫敏

    我們在使用手機(jī)銀行的時(shí)候經(jīng)常能看到APP上會將銀行卡的卡號中間部分給隱藏掉使用 ***** 來代替,在某些網(wǎng)站上查看一些業(yè)務(wù)密碼時(shí)(例如簽到密碼等)也會使用 ***** 來隱藏掉真正的密碼,那么這種方式是如何實(shí)現(xiàn)的呢,本文將給大家介紹使用Hutool+自定義注解實(shí)現(xiàn)數(shù)據(jù)脫敏
    2023-09-09
  • Java 線程池框架

    Java 線程池框架

    本文主要介紹了Java 線程池框架的相關(guān)知識。具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • springboot3 使用 jasypt 加密配置文件的使用步驟

    springboot3 使用 jasypt 加密配置文件的使用步驟

    在SpringBoot項(xiàng)目中,使用Jasypt加密配置文件可以有效保護(hù)敏感信息,首先,需添加Jasypt依賴并配置加密密碼,可在application.properties或通過啟動參數(shù)、環(huán)境變量設(shè)置,本文介紹了Jasypt的配置步驟及使用方法,幫助開發(fā)者保護(hù)應(yīng)用配置信息
    2024-11-11
  • SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對數(shù)據(jù)庫密碼加密的方法

    SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對數(shù)據(jù)庫

    這篇文章主要介紹了SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對數(shù)據(jù)庫密碼加密的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Java并發(fā)工具之CountDownLatch使用詳解

    Java并發(fā)工具之CountDownLatch使用詳解

    這篇文章主要介紹了Java并發(fā)工具之CountDownLatch使用詳解,通過使用 CountDownLatch可以使當(dāng)前線程阻塞,等待其他線程完成給定任務(wù),可以類比旅游團(tuán)導(dǎo)游要等待所有的游客到齊后才能去下一個(gè)景點(diǎn),需要的朋友可以參考下
    2023-12-12

最新評論

桂林市| 黄大仙区| 乌兰县| 乐安县| 原阳县| 通江县| 武穴市| 芜湖县| 乌拉特后旗| 三门峡市| 志丹县| 电白县| 丰县| 博兴县| 延吉市| 平泉县| 上栗县| 军事| 光泽县| 平罗县| 秦安县| 武义县| 麻江县| 台中市| 德钦县| 平顶山市| 龙山县| 台北市| 平远县| 淅川县| 连平县| 固安县| 泾源县| 禹城市| 凌源市| 昌都县| 泗阳县| 永泰县| 诸暨市| 宁城县| 绥阳县|