100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > aliyun短信服务包含随机生成四位数字验证码工具类

aliyun短信服务包含随机生成四位数字验证码工具类

时间:2023-06-14 10:12:19

相关推荐

aliyun短信服务包含随机生成四位数字验证码工具类

.1.pom文件

<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.6.0</version></dependency><!-- redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>

配置文件

spring:redis:host: localhostpost: 6379database: 0timeout: 1800000

2.定义接口及实现类

public interface MsmService {//发送验证码boolean send(HashMap<String, Object> param, String phone);}

import com.alibaba.fastjson.JSONObject;import monRequest;import monResponse;import com.aliyuncs.DefaultAcsClient;import com.aliyuncs.IAcsClient;import com.aliyuncs.http.MethodType;import com.aliyuncs.profile.DefaultProfile;import com.aliyuncs.utils.StringUtils;import lombok.Data;import org.springframework.stereotype.Service;import java.util.HashMap;import java.util.Map;@Data@Servicepublic class MsmServiceImpl implements MsmService {/*** 发送验证码* @param param验证码* @param phone手机号* @return*/public boolean send(HashMap<String, Object> param, String phone) {if(StringUtils.isEmpty(phone)) return false;//default 地域节点,默认就好 后面是 阿里云的 id和秘钥(这里记得去阿里云复制自己的id和秘钥哦)DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI5tE69LuEkxnLKEEGcTMy", "eR0A1fiHIu9z6mBaUaFjaivyTT2w2K");IAcsClient client = new DefaultAcsClient(profile);//这里不能修改CommonRequest request = new CommonRequest();//request.setProtocol(ProtocolType.HTTPS);request.setMethod(MethodType.POST);request.setDomain("");request.setVersion("-05-25");request.setAction("SendSms");request.putQueryParameter("PhoneNumbers", phone);//手机号request.putQueryParameter("SignName", "阿里云短信测试"); //申请阿里云 签名名称(暂时用阿里云测试的,自己还不能注册签名)request.putQueryParameter("TemplateCode", "SMS_154950909"); //申请阿里云 模板code(用的也是阿里云测试的)request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));try {CommonResponse response = client.getCommonResponse(request);System.out.println(response.getData());return response.getHttpResponse().isSuccess();} catch (Exception e) {e.printStackTrace();}return false;}}

3.随机生成四位数字验证码工具类

package com.example.demo.util;import java.text.DecimalFormat;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Random;//生成随机验证码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<Object, Object> hashMap = new HashMap<Object, Object>();// 生成随机数字并存入HashMapfor (int i = 0; i < list.size(); i++) {int number = random.nextInt(100) + 1;hashMap.put(number, i);}// 从HashMap导入数组Object[] robjs = hashMap.values().toArray();ArrayList r = new ArrayList();// 遍历数组并打印数据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;}}

3.controller类

@RestController@RequestMapping("/api/msm")public class MsmApiController {@Autowiredprivate MsmService msmService;@Autowiredprivate RedisTemplate<String, String> redisTemplate;//发送短信验证码@GetMapping(value = "/send/{phone}")public Boolean code(@PathVariable String phone) {if (StringUtils.isEmpty(phone)||StringUtils.isBlank(phone)){return false;}String fourBitRandom = RandomUtil.getFourBitRandom();String key="regist-"+phone;String value= redisTemplate.opsForValue().get("key");if(value!=null){return false;}HashMap<String,Object> code=new HashMap<>();code.put("code",fourBitRandom);if(msmService.send(code,phone)){redisTemplate.opsForValue().set(key,fourBitRandom,1, TimeUnit.MINUTES);return true;}return false;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。