Chinaunix首页 | 论坛 | 博客
  • 博客访问: 603809
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-03-22 13:33:10


点击(此处)折叠或打开


  1. 准备工作
  2. 开通短信服务(可参考博主的“手机短信验证码”)
  3. 如果开通不成功,就只能借下别人已经开通好的短信,如果不想重复,可在其下创建一个新的模板管理
  4. 这里只是介绍如何使用
  5. 导入依赖
  6. <!--aliyun-->
  7. <dependency>
  8.     <groupId>com.aliyun</groupId>
  9.     <artifactId>aliyun-java-sdk-core</artifactId>
  10.     <version>4.5.1</version>
  11. </dependency>
  12. <dependency>
  13.     <groupId>com.aliyun</groupId>
  14.     <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
  15.     <version>1.1.0</version>
  16. </dependency>
  17. <dependency>
  18.     <groupId>com.alibaba</groupId>
  19.     <artifactId>fastjson</artifactId>
  20.     <version>1.2.62</version>
  21. </dependency>

  22. 发送验证码到手机上,验证码生成工具类(内容较为固定,也可根据需求改)

  23. package com.xsha.msmservice.utils;

  24. import java.text.DecimalFormat;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Random;

  29. /**
  30.  * 说明:短信配置
  31.  * 作者:FH Admin
  32.  * from:fhadmin.cn
  33.  */
  34. public class RandomUtil {

  35.     private static final Random random = new Random();

  36.     private static final DecimalFormat fourdf = new DecimalFormat("0000");

  37.     private static final DecimalFormat sixdf = new DecimalFormat("000000");

  38.     public static String getFourBitRandom() {
  39.         return fourdf.format(random.nextInt(10000));
  40.     }

  41.     public static String getSixBitRandom() {
  42.         return sixdf.format(random.nextInt(1000000));
  43.     }

  44.     /**
  45.      * 给定数组,抽取n个数据
  46.      * @param list
  47.      * @param n
  48.      * @return
  49.      */
  50.     public static ArrayList getRandom(List list, int n) {

  51.         Random random = new Random();

  52.         HashMap<Object, Object> hashMap = new HashMap<Object, Object>();

  53.         // 生成随机数字并存入HashMap
  54.         for (int i = 0; i < list.size(); i++) {

  55.             int number = random.nextInt(100) + 1;

  56.             hashMap.put(number, i);
  57.         }

  58.         // 从HashMap导入数组
  59.         Object[] robjs = hashMap.values().toArray();

  60.         ArrayList r = new ArrayList();

  61.         // 遍历数组并打印数据
  62.         for (int i = 0; i < n; i++) {
  63.             r.add(list.get((int) robjs[i]));
  64.             System.out.print(list.get((int) robjs[i]) + "\t");
  65.         }
  66.         System.out.print("\n");
  67.         return r;
  68.     }
  69. }

  70. 发送验证码,验证码是有有效时间的(时间可以自己设置)

  71. 这里可以创建常量类读取配置文件的阿里云密钥等信息(可参考博主的“对象存储”)

  72. package com.xsha.msmservice.service.impl;

  73. import com.alibaba.fastjson.JSONObject;
  74. import com.aliyuncs.DefaultAcsClient;
  75. import com.aliyuncs.IAcsClient;
  76. import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
  77. import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
  78. import com.aliyuncs.exceptions.ClientException;
  79. import com.aliyuncs.exceptions.ServerException;
  80. import com.aliyuncs.profile.DefaultProfile;
  81. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  82. import com.xsha.msmservice.service.MsmService;
  83. import com.xsha.msmservice.utils.RandomUtil;
  84. import org.springframework.beans.factory.annotation.Autowired;
  85. import org.springframework.data.redis.core.RedisTemplate;
  86. import org.springframework.stereotype.Service;

  87. import java.util.HashMap;
  88. import java.util.Map;
  89. import java.util.concurrent.TimeUnit;

  90. /**
  91.  * 说明:短信配置
  92.  * 作者:FH Admin
  93.  * from:fhadmin.cn
  94.  */
  95. @Service
  96. public class MsmServiceImpl implements MsmService {

  97.     // 注入redis缓存对象
  98.     @Autowired
  99.     private RedisTemplate<String, String> redisTemplate;

  100.     @Override
  101.     public boolean sendMessage(String phone) {
  102.         if(StringUtils.isEmpty(phone)) return false;
  103.         // 先获取手机号对应的验证码(该验证码没过期)
  104.         String code = redisTemplate.opsForValue().get(phone);
  105.         if(!StringUtils.isEmpty(code)) {
  106.             return true;
  107.         }
  108.         // 过期则生成随机验证码,并在发送之后向redis中存入手机号对应的验证码
  109.         code = RandomUtil.getFourBitRandom();
  110.         Map<String, Object> map = new HashMap<>();
  111.         map.put("code", code);

  112.         // 设置短信配置
  113.         DefaultProfile profile = DefaultProfile.getProfile("your region", "your accessId", "your accessSecret");

  114.         IAcsClient client = new DefaultAcsClient(profile);

  115.         SendSmsRequest request = new SendSmsRequest();
  116.         request.setPhoneNumbers(phone);//接收短信的手机号码
  117.         request.setSignName("your signature name");//短信签名名称
  118.         request.setTemplateCode("your template");//短信模板CODE
  119.         request.setTemplateParam(JSONObject.toJSONString(map));//短信模板变量对应的实际值

  120.         try {
  121.             SendSmsResponse response = client.getAcsResponse(request);
  122.             // 发送短信,尽量打印出来是否发送成功
  123.             new Gson().toJson(response);
  124.             // 将验证码放置在redis缓存中,并设置5分钟有效时间,最后一个参数是单位
  125.             redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
  126.         } catch (ServerException e) {
  127.             e.printStackTrace();
  128.             return false;
  129.         } catch (ClientException e) {
  130.             e.printStackTrace();
  131.             return false;
  132.         }
  133.         return true;
  134.     }
  135. }



阅读(621) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~