package com.javaweb.platform.utils; import java.util.Random; import cn.hutool.cache.CacheUtil; import cn.hutool.cache.impl.TimedCache; public class CaptCodeUtils { private static long timeout = 1000* 60 ;//1分钟过期 //用户缓存hash表 public static TimedCache captcodeCache = CacheUtil.newTimedCache(timeout); public static String verifyCode(){ int a = 1234567890; String b ="abcdefghijklmnopqrstuvwxyz"; String B=b.toUpperCase(); String c=a+b+B; char[] d=c.toCharArray(); Random random = new Random(); String f=""; for (int k = 0; k < 6; k++) { int index = random.nextInt(d.length); f+=d[index]; } //添加缓存 setCahce(f); return f; } //设置 public static void setCahce(String code){ captcodeCache.put(code, code, timeout); } //验证码是否存在 public static boolean isExistCode(String code){ String s = captcodeCache.get(code, false); if(s !=null){ return true; } return false; } //删除 public static void remove(String code){ captcodeCache.remove(code); } //清空 public static void clear(){ captcodeCache.clear(); } }