Commit 5adc0d93 authored by shiyu's avatar shiyu

后台管理登录验证码

parent fb6c07b4
......@@ -39,6 +39,9 @@ public class AdminAuthController {
@Autowired
private AdminAuthService adminAuthService;
@Autowired
CaptchaCodeManager captchaCodeManager;
/*
* { username : value, password : value }
*/
......@@ -56,7 +59,7 @@ public class AdminAuthController {
}
//验证码校验
String cachedCaptcha = CaptchaCodeManager.getCachedCaptcha(uuid);
String cachedCaptcha = captchaCodeManager.getCachedCaptcha(uuid);
if (cachedCaptcha == null) {
logger.error("系统管理->用户登录 错误:{},", AdminResponseCode.AUTH_CAPTCHA_EXPIRED.desc());
return AdminResponseUtil.fail(AdminResponseCode.AUTH_CAPTCHA_EXPIRED);
......
......@@ -118,6 +118,11 @@ public class CoinVo implements Entity {
*/
private String weight;
/**
* 原始的资源url
*/
private String originalSourceUrl;
/**
* 对应的分类
*
......
......@@ -39,6 +39,9 @@ public class AdminAuthServiceImpl implements AdminAuthService {
@Autowired
private ApplicationContext context;
@Autowired
CaptchaCodeManager captchaCodeManager;
@Override
public Object info() {
......@@ -72,7 +75,8 @@ public class AdminAuthServiceImpl implements AdminAuthService {
String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
// 唯一标识
String uuid = UUID.randomUUID().toString(true);
boolean successful = CaptchaCodeManager.addToCache(uuid, verifyCode,10);//存储内存
//存入redis
boolean successful = captchaCodeManager.addToCache(uuid, verifyCode);
if (!successful) {
logger.error("请求验证码出错:{}", AdminResponseCode.AUTH_CAPTCHA_FREQUENCY.desc());
return AdminResponseUtil.fail(AdminResponseCode.AUTH_CAPTCHA_FREQUENCY);
......
package com.wwdz.ch.core.captcha;
import com.wwdz.ch.core.util.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.*;
......@@ -10,12 +13,17 @@ import java.util.Map.Entry;
/**
* 缓存系统中的验证码
*/
@Component
public class CaptchaCodeManager {
private static final Logger logger = LoggerFactory.getLogger(CaptchaCodeManager.class);
private static final Integer DEFAULT_EXPIRE_MINUTES = 3;//验证码默认存储的有效期,单位:分钟
private static Map<String, CaptchaItem> captchaCodeCache = new HashMap<>();
@Autowired
RedisUtils redisUtils;
/**
* 添加到缓存
*
......@@ -24,30 +32,8 @@ public class CaptchaCodeManager {
* @param code
* 验证码
*/
public static boolean addToCache(String flagUid, String code,Integer expireTime) {
cleanExpireCacheData();//清理过期内存数据
// 已经发过验证码且验证码还未过期
if (captchaCodeCache.get(flagUid) != null) {
if (captchaCodeCache.get(flagUid).getExpireTime().isAfter(LocalDateTime.now())) {
return false;
} else {
// 存在但是已过期,删掉
captchaCodeCache.remove(flagUid);
}
}
CaptchaItem captchaItem = new CaptchaItem();
captchaItem.setFlagUid(flagUid);
captchaItem.setCode(code);
// 有效期为expireTime分钟
if (expireTime == null) {
expireTime = DEFAULT_EXPIRE_MINUTES;
}
captchaItem.setExpireTime(LocalDateTime.now().plusMinutes(expireTime));
captchaCodeCache.put(flagUid, captchaItem);
public boolean addToCache(String flagUid, String code) {
redisUtils.set(flagUid, code, 3 * 60);
return true;
}
......@@ -59,24 +45,18 @@ public class CaptchaCodeManager {
* 关联的标志码
* @return 验证码
*/
public static String getCachedCaptcha(String flagUid) {
public String getCachedCaptcha(String flagUid) {
// 没有标志码记录
if (captchaCodeCache.get(flagUid) == null)
return null;
// 记录但是已经过期
if (captchaCodeCache.get(flagUid).getExpireTime().isBefore(LocalDateTime.now())) {
if (!redisUtils.hasKey(flagUid)) {
return null;
}
cleanExpireCacheData();//清理过期内存数据
return captchaCodeCache.get(flagUid).getCode();
return redisUtils.get(flagUid).toString();
}
/**
* 清理过期验证码
*/
private static void cleanExpireCacheData() {
private void cleanExpireCacheData() {
Iterator<Entry<String, CaptchaItem>> iterator = captchaCodeCache.entrySet().iterator(); //map.entrySet()得到的是set集合,可以使用迭代器遍历
List<String> keys = new ArrayList<String>();
while(iterator.hasNext()){
......@@ -93,7 +73,7 @@ public class CaptchaCodeManager {
}
}
public static void removeCacheData(String flagUid) {
public void removeCacheData(String flagUid) {
captchaCodeCache.remove(flagUid);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment