|
@@ -5,13 +5,13 @@ import cn.tr.core.exception.ServiceException;
|
|
|
import cn.tr.core.exception.TRExcCode;
|
|
import cn.tr.core.exception.TRExcCode;
|
|
|
import com.wf.captcha.base.Captcha;
|
|
import com.wf.captcha.base.Captcha;
|
|
|
import com.wf.captcha.utils.CaptchaUtil;
|
|
import com.wf.captcha.utils.CaptchaUtil;
|
|
|
-import org.springframework.data.redis.core.BoundValueOperations;
|
|
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
|
|
+import org.redisson.api.RMap;
|
|
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
|
+import java.time.Duration;
|
|
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -23,38 +23,36 @@ import java.util.Objects;
|
|
|
*/
|
|
*/
|
|
|
@Component
|
|
@Component
|
|
|
public class CaptchaOperator {
|
|
public class CaptchaOperator {
|
|
|
- private RedisTemplate<String,String> redisTemplate;
|
|
|
|
|
- public CaptchaOperator(RedisTemplate redisTemplate) {
|
|
|
|
|
- this.redisTemplate=redisTemplate;
|
|
|
|
|
|
|
+ private RMap<String, String> captchaMap;
|
|
|
|
|
+ public CaptchaOperator(RedissonClient redissonClient) {
|
|
|
|
|
+ captchaMap = redissonClient.getMap("captcha:");
|
|
|
|
|
+ captchaMap.expire(Duration.of(30, ChronoUnit.MINUTES));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 输出验证码
|
|
* 输出验证码
|
|
|
* @param key
|
|
* @param key
|
|
|
* @param captcha Captcha
|
|
* @param captcha Captcha
|
|
|
- * @param request HttpServletRequest
|
|
|
|
|
* @param response HttpServletResponse
|
|
* @param response HttpServletResponse
|
|
|
* @throws IOException IO异常
|
|
* @throws IOException IO异常
|
|
|
*/
|
|
*/
|
|
|
- public void out(String key, Captcha captcha, int expire, HttpServletRequest request, HttpServletResponse response)
|
|
|
|
|
|
|
+ public void out(String key, Captcha captcha, HttpServletResponse response)
|
|
|
throws IOException {
|
|
throws IOException {
|
|
|
CaptchaUtil.setHeader(response);
|
|
CaptchaUtil.setHeader(response);
|
|
|
//验证码放入缓存30分钟
|
|
//验证码放入缓存30分钟
|
|
|
- redisTemplate.boundValueOps("captcha:"+key).set(captcha.text().toLowerCase(), expire);
|
|
|
|
|
|
|
+ captchaMap.put(key,captcha.text().toLowerCase());
|
|
|
captcha.out(response.getOutputStream());
|
|
captcha.out(response.getOutputStream());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean ver(String key,String code){
|
|
public boolean ver(String key,String code){
|
|
|
- BoundValueOperations<String, String> operations = redisTemplate.boundValueOps("captcha:" + key);
|
|
|
|
|
|
|
+ String cacheCode = captchaMap.get(key);
|
|
|
if(StrUtil.isBlank(code)){
|
|
if(StrUtil.isBlank(code)){
|
|
|
throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"验证码不能为空");
|
|
throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"验证码不能为空");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- Object cacheCode = operations.get();
|
|
|
|
|
if (Objects.nonNull(cacheCode)) {
|
|
if (Objects.nonNull(cacheCode)) {
|
|
|
if (Objects.equals(cacheCode,code)) {
|
|
if (Objects.equals(cacheCode,code)) {
|
|
|
- operations.getAndDelete();
|
|
|
|
|
|
|
+ captchaMap.remove(key);
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"验证码错误");
|
|
throw new ServiceException(TRExcCode.SYSTEM_ERROR_B0001,"验证码错误");
|