ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

springboot 发送验证码 redis控制在规定时间内的短信发送规定次数思路

2021-12-20 16:30:50  阅读:186  来源: 互联网

标签:短信 springboot redis 发送 tbuMsgSend sendSmsResponse new String


if (!bolTimeOut1H) {

return new Result().getError(SmsRes

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

ultCode.VERMOBILE_TIME1H);

}

//24小时内只能发送20次

Boolean bolTimeOut24H = sendCount(key24h, 20, 24);

if (!bolTimeOut24H) {

return new Result().getError(SmsResultCode.VERMOBILE_TIME24H);

}

long l = System.currentTimeMillis();

SendSmsResponse sendSmsResponse = sendSms(phoneNumbers, JSON.toJSONString(map), templateCode);

TbuMsgSend tbuMsgSend = new TbuMsgSend();

tbuMsgSend.setC_mobile(phoneNumbers);

tbuMsgSend.setM_type(type);

tbuMsgSend.setC_result(sendSmsResponse.getCode());

tbuMsgSend.setC_result_detail(sendSmsResponse.getMessage());

tbuMsgSend.setRequestid(sendSmsResponse.getRequestId());

tbuMsgSend.setBack_up(sendSmsResponse.getBizId());

String dt = DateUtil.format(new Date(), “yyyyMMddHHmmss”);

tbuMsgSend.setCreate_time(dt);

tbuMsgSend.setUpdate_time(dt);

String code = “”;

if (map.containsKey(“code”)) {

code = map.get(“code”);

tbuMsgSend.setC_content(code);

}

if (sendSmsResponse.getCode().equals(“OK”)) {

redisTemplate.opsForList().rightPush(key1h, l + “”);

redisTemplate.opsForList().rightPush(key24h, l + “”);

// 发送成功之后往redis中存入该手机号以及验证码 并设置超时时间 1 分钟

redisTemplate.opsForValue().set(key1m, code, 1, TimeUnit.MINUTES);

//记录到数据库

insertTbuMsgSend(tbuMsgSend);

return new Result().getOk(MessageFormat.format(SmsResultCode.YZMSUCCESS, phoneNumbers), “”);

} else {

//记录到数据库

insertTbuMsgSend(tbuMsgSend);

//发送失败

return new Result().getError(sendSmsResponse.getCode(), sendSmsResponse.getMessage());

}

} catch (Exception e) {

//throw new CommonException(CodeMsg.SERVER_ERROR, SmsResultCode.ERROR);

return new Result().getError(SmsResultCode.ERROR);

}

}

public void insertTbuMsgSend(TbuMsgSend tbuMsgSend) {

try {

if (tbuMsgSend == null) {

log.error(“短信记录失败,mobile为空”);

return;

}

tbuMsgSendMapper.insertTbuMsgSend(tbuMsgSend);

log.error(“短信已记录:{}” + tbuMsgSend.getC_mobile());

} catch (Exception e) {

log.error(“短信记录失败:{}” + tbuMsgSend.getC_mobile());

}

}

/**

  • 同一个手机号一小时内发送短讯是否超过count次

  • @param key key 的拼接是 key + 手机号 + 业务类型

  • @param count 次数

  • @param h 小时 必须整数

  • @author

*/

public Boolean sendCount(String key, int count, int h) {

Boolean boo = true;

long size = redisTemplate.opsForList().size(key);

if (size <= count) {

//redisTemplate.opsForList().rightPush(key, System.currentTimeMillis() + “”);

boo = true;

} else {

List t = redisTemplate.opsForList().range(key, 0, (int) size);

Long now = System.currentTimeMillis();

if (now - Long.valueOf(t.get(0)) > h * 60 * 60 * 1000) {

//最开始的一条距现在超过一小时就移除左边的,并添加一条

redisTemplate.opsForList().leftPop(key);

//redisTemplate.opsForList().rightPush(key, System.currentTimeMillis() + “”);

boo = true;

} else {//最左的一条也在n小时内,不能发送短信

boo = false;

}

}

return boo;

}

/**

  • @param phoneNumbers: 手机号

  • @param templateParamJson: 模板参数json {“sellerName”:“123456”,“orderSn”:“123456”}

  • @param templateCode: 阿里云短信模板code

  • @Description: 对接阿里云短信服务实现短信发送

  • 发送验证码类的短信时,每个号码每分钟最多发送一次,每个小时最多发送5次。其它类短信频控请参考阿里云

  • @Date: 2019/4/18 16:35

  • @Version: V1.0

*/

public SendSmsResponse sendSms(String phoneNumbers, String templateParamJson, String templateCode) {

//可自助调整超时时间

//设定连接超时

System.setProperty(“sun.net.client.defaultConnectTimeout”, “10000”);

//设定读取超时

System.setProperty(“sun.net.client.defaultReadTimeout”, “10000”);

// 封装短信发送对象

Sms sms = new Sms();

sms.setPhoneNumbers(phoneNumbers);

sms.setTemplateParam(templateParamJson);

sms.setTemplateCode(templateCode);

// 获取短信发送服务机

IAcsClient acsClient = getClient();

//获取短信请求

SendSmsRequest request = getSmsRequest(sms);

SendSmsResponse sendSmsResponse = new SendSmsResponse();

try {

sendSmsResponse = acsClient.getAcsResponse(request);

} catch (ClientException e) {

log.error(“发送短信发生错误。错误代码是 [{}],错误消息是 [{}],错误请求ID是 [{}],错误Msg是 [{}],错误类型是 [{}]”,

e.getErrCode(),

e.getMessage(),

e.getRequestId(),

e.getErrMsg(),

e.getErrorType());

}

return sendSmsResponse;

}

AliyunPropert 类只是阿里云的配置文件类

import lombok.Getter;

import lombok.Setter;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

/**

  • @Auther: heng

  • @Date: 2020/12/9 16:58

  • @Description: AliyunPropert

  • @Version 1.0.0

*/

@Getter

@Setter

@PropertySource(value = “classpath:aliyun.properties”, encoding = “UTF-8”)

@ConfigurationProperties(prefix = “aliyun.sms”)

@Configuration

public class AliyunPropert {

/**

  • 短信API产品名称(短信产品名固定,无需修改)

*/

private String product;

/**

  • 短信API产品域名,接口地址固定,无需修改

*/

private String domain;

/**

  • 此处需要替换成开发者自己的accessKeyId和accessKeySecret(在阿里云访问控制台寻找)

  • TODO: 这里要写成你自己生成的

*/

private String accessKeyId; //

/**

  • accessKeySecret

  • TODO: 这里要写成你自己生成的

*/

private String accessKeySecret;

/**

标签:短信,springboot,redis,发送,tbuMsgSend,sendSmsResponse,new,String
来源: https://blog.csdn.net/m0_65320833/article/details/122044332

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有