![JAR search and dependency download from the Maven repository](/logo.png)
love.keeping.starter.web.components.CaptchaValidator Maven / Gradle / Ivy
The newest version!
package love.keeping.starter.web.components;
import love.keeping.starter.common.constants.StringPool;
import love.keeping.starter.common.exceptions.impl.DefaultClientException;
import love.keeping.starter.common.utils.StringUtil;
import love.keeping.starter.web.components.redis.RedisHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 验证码校验
*/
@Slf4j
public class CaptchaValidator {
@Autowired
private RedisHandler redisHandler;
/**
* 校验验证码 如果未通过校验则会抛出异常
*
* @param sn
* @param captcha
*/
public void validate(String sn, String captcha) {
log.debug("开始校验验证码,sn={}, captcha={}", sn, captcha);
if (StringUtil.isEmpty(sn) || StringUtil.isEmpty(captcha)) {
log.debug("sn、captcha为空,校验不通过");
throw new DefaultClientException("验证码错误,请重新输入!");
}
String captchaKey = StringUtil.format(StringPool.LOGIN_CAPTCHA_KEY, sn);
try {
String oriCaptcha = (String) redisHandler.get(captchaKey);
log.debug("从redis中取出正确的captcha={}", oriCaptcha);
if (StringUtil.isEmpty(oriCaptcha)) {
log.debug("验证码已过期");
throw new DefaultClientException("验证码已过期,请重新输入!");
}
if (!StringUtil.equalsIgnoreCase(oriCaptcha, captcha)) {
log.debug("验证码错误");
throw new DefaultClientException("验证码错误,请重新输入!");
}
} finally {
// 无论校验是否通过,都需要重新生成验证码
redisHandler.del(captchaKey);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy