阿里云短信验证加redis限制验证码过期时间
Posted upupup-999
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里云短信验证加redis限制验证码过期时间相关的知识,希望对你有一定的参考价值。
1.导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</dependency>
2.配置redis参数
#redis配置
spring.redis.host=8.140.124.212
spring.redis.port=6379
spring.redis.database=0
spring.redis.password=******
spring.redis.timeout=1800000
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0
3.编写接口
@RestController
@RequestMapping("/edumsm/msm")
@CrossOrigin
public class MsmController {
@Autowired
MsmService msmService;
@Autowired
RedisTemplate<String,String> redisTemplate;
@GetMapping("send/{phone}")
public R sendMsm(@PathVariable String phone) {
String code = redisTemplate.opsForValue().get(phone);
if (!StringUtils.isEmpty(code))
{
return R.ok();
}
code = RandomUtil.getFourBitRandom();
HashMap<String, Object> param = new HashMap<>();
param.put("code",code);
boolean isSend = msmService.send(param,phone);
if (isSend)
{
redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
return R.ok();
}
else
{
return R.error().message("短信发送失败");
}
}
}
4.实现接口
import com.aliyuncs.CommonResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.wang.msmservice.service.MsmService;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import java.util.HashMap;
public class MsmServiceImpl implements MsmService {
@Override
public boolean send(HashMap<String, Object> param, String phone) {
if(StringUtils.isEmpty(phone)) return false;
DefaultProfile profile =
DefaultProfile.getProfile("default", "LTAI4FvvVEWiTJ3GNJJqJnk7", "9st82dv7EvFk9mTjYO1XXbM632fRbG");
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",phone); //手机号
request.putQueryParameter("SignName","我的谷粒在线教育网站"); //申请阿里云 签名名称
request.putQueryParameter("TemplateCode","SMS_180051135"); //申请阿里云 模板code
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码数据,转换json数据传递
//设置发送相关的参数
request.putQueryParameter("PhoneNumbers",phone); //手机号
request.putQueryParameter("SignName","我的谷粒在线教育网站"); //申请阿里云 签名名称
request.putQueryParameter("TemplateCode","SMS_180051135"); //申请阿里云 模板code
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码数据,转换json数据传递
try {
//最终发送
CommonResponse response = client.getCommonResponse(request);
boolean success = response.getHttpResponse().isSuccess();
return success;
}catch(Exception e) {
e.printStackTrace();
return false;
}
}
}
以上是关于阿里云短信验证加redis限制验证码过期时间的主要内容,如果未能解决你的问题,请参考以下文章