update:去除验证码重复服务类

This commit is contained in:
hrz 2025-03-07 22:26:36 +08:00
parent 763f74201d
commit 54ec20a5b8
3 changed files with 2 additions and 99 deletions

View file

@ -1,24 +0,0 @@
package xiaozhi.modules.sys.service;
import jakarta.servlet.http.HttpServletResponse;
/**
* @author zjy
* @since 2025-3-7
*/
public interface CaptchaService {
/**
* 生成验证码
* @param response 响应对象
* @param uuid 验证码唯一id
*/
void create(HttpServletResponse response, String uuid);
/**
* 验证码验证
* @param uuid 验证码唯一id
* @param code 验证码内容
* @return
*/
void validate(String uuid, String code);
}

View file

@ -1,65 +0,0 @@
package xiaozhi.modules.sys.service.impl;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import xiaozhi.common.exception.ErrorCode;
import xiaozhi.common.exception.RenException;
import xiaozhi.common.redis.RedisUtils;
import xiaozhi.modules.sys.service.CaptchaService;
import java.io.IOException;
/**
* @author zjy
* @since 2025-3-7
*/
@AllArgsConstructor
@Service("captchaService")
public class CaptchaServiceImpl implements CaptchaService {
private final RedisUtils redisUtils;
@Value("${captcha.expire}")
private Integer captchaExpireTime;
@Override
public void create(HttpServletResponse response, String uuid) {
response.setContentType("image/png");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 创建验证码对象 宽度、高度、字符长度
SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
// 默认字符类型
captcha.setCharType(Captcha.TYPE_DEFAULT);
// 缓存验证码
redisUtils.set(uuid,captcha.text(),captchaExpireTime);
// 输出验证码图片
try {
captcha.out(response.getOutputStream());
} catch (IOException e) {
throw new RenException(ErrorCode.VERIFICATION_CODE,"验证码生成错误");
}
}
@Override
public void validate(String uuid, String code) {
// 从缓存中获取验证码
Object storedCode = redisUtils.get(uuid);
//对验证码进行验证过程
if(storedCode == null){
throw new RenException(ErrorCode.VERIFICATION_CODE,"验证码过期,请从新获取");
}else if(code.equalsIgnoreCase(storedCode.toString())){
throw new RenException(ErrorCode.VERIFICATION_CODE,"验证码错误");
}
// 从缓存删除验证码
redisUtils.delete(uuid);
}
}

View file

@ -15,7 +15,7 @@ spring:
druid:
#MySQL
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.110.130:3306/xiaozhi_esp32_server?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
url: jdbc:mysql://localhost:3306/xiaozhi_esp32_server?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
username: root
password: 123456
initial-size: 10
@ -41,12 +41,4 @@ spring:
merge-sql: false
wall:
config:
multi-statement-allow: true
logging:
level:
org.flowable.engine.impl.persistence.entity.*: debug
org.flowable.task.service.impl.persistence.entity.*: debug
# 登录验证码过期时间(秒)
captcha:
expire: 300
multi-statement-allow: true