CaptchaCodeManager
package org.linlinjava.litemall.wx.service; import org.linlinjava.litemall.wx.dto.CaptchaItem; import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; /**
* 缓存系统中的验证码
*/
public class CaptchaCodeManager {
private static ConcurrentHashMap<String, CaptchaItem> captchaCodeCache = new ConcurrentHashMap<>(); /**
* 添加到缓存
*
* @param phoneNumber 电话号码
* @param code 验证码
*/
public static boolean addToCache(String phoneNumber, String code) { //已经发过验证码且验证码还未过期
if (captchaCodeCache.get(phoneNumber) != null) {
if (captchaCodeCache.get(phoneNumber).getExpireTime().isAfter(LocalDateTime.now())) {
return false;
} else {
//存在但是已过期,删掉
captchaCodeCache.remove(phoneNumber);
}
} CaptchaItem captchaItem = new CaptchaItem();
captchaItem.setPhoneNumber(phoneNumber);
captchaItem.setCode(code);
// 有效期为1分钟
captchaItem.setExpireTime(LocalDateTime.now().plusMinutes(1)); captchaCodeCache.put(phoneNumber, captchaItem); return true;
} /**
* 获取缓存的验证码
*
* @param phoneNumber 关联的电话号码
* @return 验证码
*/
public static String getCachedCaptcha(String phoneNumber) {
//没有这个电话记录
if (captchaCodeCache.get(phoneNumber) == null)
return null; //有电话记录但是已经过期
if (captchaCodeCache.get(phoneNumber).getExpireTime().isBefore(LocalDateTime.now())) {
return null;
} return captchaCodeCache.get(phoneNumber).getCode();
}
}
package org.linlinjava.litemall.wx.dto; import java.time.LocalDateTime; /**
* 验证码实体类,用于缓存验证码发送
*/
public class CaptchaItem {
private String phoneNumber;
private String code;
private LocalDateTime expireTime; public String getPhoneNumber() {
return phoneNumber;
} public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
} public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public LocalDateTime getExpireTime() {
return expireTime;
} public void setExpireTime(LocalDateTime expireTime) {
this.expireTime = expireTime;
}
}
CaptchaCodeManager的更多相关文章
随机推荐
- cvthreshold 的运用
什么是阈值? 最简单的图像分割的方法. 应用举例:从一副图像中利用阈值分割出我们需要的物体部分(当然这里的物体可以是一部分或者整体).这样的图像分割方法是基于图像中物体与背景之间的灰度差异,而且此分割 ...
- Mybatis报错——Mapped Statements collection already contains value for
解决办法: 看看你的mybatis-config.xml <mappers> <mapper resource="mapper/SeckillDao.xml&quo ...
- matplotlib画图实例-day1
matplotlib主要用于根据数据画各种图表 官网:https://matplotlib.org/gallery/index.html 例1:画一天中每个两个小时温度变化趋势图 #!/usr/bin ...
- 修改完Apache的配置文件,重启Apache后,仍无法打开网页
在修改Apache的配置文件时,由于某些非正常操作,导致httpd.conf文件非正常打开,需要继续enter进入, 这是会在httpd.conf同级目录中产生一个隐藏文件,.httpd.conf.s ...
- 寒假day20
今天解决了部分信息爬取不下来的问题
- 汪慧和201771010123《面向对象程序设计(Java)》第三周学习总结
1.实验目的与要求 (1)进一步掌握Eclipse集成开发环境下java程序开发基本步骤: (2)熟悉PTA平台线上测试环境: (3)掌握Java语言构造基本程序语法知识(ch1-ch3): (4)利 ...
- ELK简单配置
input { file { path => ["/usr/local/kencery/tomcat/logs/catalina.out"] type => " ...
- tomcat配置配置文件和war包进行分离
应用部署 war包.配置文件分离 部署主机路径规划以及tomcat中间件改造 1.新建存放war包路径 /appsystems/apps 将war包放置其中 2.新建存放配置文件路径 /apps ...
- Hexo博客NexT主题美化之评论系统
前言 更多效果展示,请访问我的博客 https://kangmingxian.github.io/ 效果图: image Valine 诞生于2017年8月7日,是一款基于Leancloud的快速 ...
- 三阶平面魔方(BFS)
有一个 3×3 的平面魔方,在平面魔方中,每个格子里分别无重复地写上 1 - 9 这 9 个数字.一共有 4 种对平面魔方的操作: 选择某一行左移. 选择某一行右移. 选择某一列上移. 选择某一列下 ...