| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package com.coffee.system.utils;
- import cn.hutool.core.util.StrUtil;
- import com.coffee.common.exception.CustomException;
- import com.coffee.common.redis.RedisUtils;
- import com.wf.captcha.base.Captcha;
- import com.wf.captcha.utils.CaptchaUtil;
- import lombok.AllArgsConstructor;
- import org.springframework.stereotype.Component;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.Objects;
- import java.util.concurrent.TimeUnit;
- /**
- * @author lifang
- * @version 1.0.0
- * @ClassName CaptchaTool.java
- * @Description TODO
- * @createTime 2022年06月10日 09:59:00
- */
- @Component
- @AllArgsConstructor
- public class CaptchaTool {
- private final RedisUtils redisUtils;
- /**
- * 输出验证码
- * @param key
- * @param captcha Captcha
- * @param request HttpServletRequest
- * @param response HttpServletResponse
- * @throws IOException IO异常
- */
- public void out(String key,Captcha captcha, int expire,HttpServletRequest request, HttpServletResponse response)
- throws IOException {
- CaptchaUtil.setHeader(response);
- //验证码放入缓存30分钟
- redisUtils.set(key,captcha.text().toLowerCase(), expire);
- captcha.out(response.getOutputStream());
- }
- public boolean ver(String key,String code){
- if(StrUtil.isBlank(code)){
- throw new CustomException("验证码不能为空");
- }
- Object cacheCode = redisUtils.get(key);
- if (Objects.nonNull(cacheCode)) {
- if (Objects.equals(cacheCode,code)) {
- return Boolean.TRUE.equals(redisUtils.del(key));
- }
- throw new CustomException("验证码错误");
- }
- throw new CustomException("验证码已失效");
- }
- }
|