SpringBoot整合阿里云开通短信服务详解
准备工作
开通短信服务
如果开通不成功,就只能借下别人已经开通好的短信,如果不想重复,可在其下创建一个新的模板管理
这里只是介绍如何使用
【SpringBoot整合阿里云开通短信服务详解】导入依赖
com.aliyun aliyun-java-sdk-core 4.5.1 com.aliyun aliyun-java-sdk-dysmsapi 1.1.0 com.alibaba fastjson 1.2.62
发送验证码到手机上,验证码生成工具类(内容较为固定,也可根据需求改)
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)); } /*** 给定数组,抽取n个数据* @param list* @param n* @return*/ public static ArrayList getRandom(List list, int n) {Random random = new Random(); HashMap
发送验证码,验证码是有有效时间的(时间可以自己设置)
这里可以创建常量类读取配置文件的阿里云密钥等信息
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 */@Servicepublic class MsmServiceImpl implements MsmService {// 注入redis缓存对象@Autowiredprivate RedisTemplate redisTemplate; @Overridepublic boolean sendMessage(String phone) {if(StringUtils.isEmpty(phone)) return false; // 先获取手机号对应的验证码(该验证码没过期)String code = redisTemplate.opsForValue().get(phone); if(!StringUtils.isEmpty(code)) {return true; }// 过期则生成随机验证码,并在发送之后向redis中存入手机号对应的验证码code = RandomUtil.getFourBitRandom(); Map 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"); //短信模板CODErequest.setTemplateParam(JSONObject.toJSONString(map)); //短信模板变量对应的实际值try {SendSmsResponse response = client.getAcsResponse(request); // 发送短信,尽量打印出来是否发送成功new Gson().toJson(response); // 将验证码放置在redis缓存中,并设置5分钟有效时间,最后一个参数是单位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阿里云短信内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 阿里巴巴云原生混部系统 Koordinator 正式开源
- Springboot|Springboot +redis+谷歌开源Kaptcha实现图片验证码功能
- 投稿|态棒,阿里的长矛还是利盾?
- SpringBoot|SpringBoot揭密(spring-boot-starter-actuator与应用监控)
- Java|Java整合腾讯云短信发送
- Hilo|Hilo - 阿里巴巴出品的免费开源 H5 游戏引擎,轻巧无依赖,适合用来开发营销互动小游戏
- 通过 Github Action 发布 SpringBoot Docker 项目到云服务器
- 学习总结|阿里云ACA云原生助理工程师认证50道真题,附部分答案和扩展
- 初识gradle|初识gradle, idea+springboot Demo
- 阿里三面(Java的synchronized|阿里三面:Java的synchronized 能防止指令重排序吗())