fix(sso): rename lineCaptchaBean to lineCaptcha
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
3fa6747c10
commit
1b8168ba10
@ -1,12 +1,22 @@
|
|||||||
package com.css.txw.sso.service.verify;
|
package com.css.txw.sso.service.verify;
|
||||||
|
|
||||||
|
import cn.hutool.captcha.CaptchaUtil;
|
||||||
|
import cn.hutool.captcha.ICaptcha;
|
||||||
|
import cn.hutool.captcha.generator.RandomGenerator;
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
import com.css.ggzc.framework.common.pojo.CommonResult;
|
import com.css.ggzc.framework.common.pojo.CommonResult;
|
||||||
import com.css.txw.sso.properties.SsoProperties;
|
import com.css.txw.sso.properties.SsoProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
|
||||||
@ -17,6 +27,16 @@ public class VerifyServiceImpl implements VerifyService {
|
|||||||
private StringRedisTemplate stringRedisTemplate;
|
private StringRedisTemplate stringRedisTemplate;
|
||||||
@Resource
|
@Resource
|
||||||
private SsoProperties ssoProperties;
|
private SsoProperties ssoProperties;
|
||||||
|
@Resource
|
||||||
|
private ICaptcha lineCaptcha;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ICaptcha lineCaptcha() {
|
||||||
|
RandomGenerator randomGenerator = new RandomGenerator("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 4);
|
||||||
|
cn.hutool.captcha.CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(120, 40, 4, 20);
|
||||||
|
captcha.setGenerator(randomGenerator);
|
||||||
|
return captcha;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CommonResult<String> getVerifyToken(String remoteId) {
|
public CommonResult<String> getVerifyToken(String remoteId) {
|
||||||
@ -38,4 +58,51 @@ public class VerifyServiceImpl implements VerifyService {
|
|||||||
String VERIFY_TOKEN = "verify_token:%s";
|
String VERIFY_TOKEN = "verify_token:%s";
|
||||||
return String.format(VERIFY_TOKEN, verifyToken);
|
return String.format(VERIFY_TOKEN, verifyToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommonResult<Map<String, String>> getCaptcha(String remoteId) {
|
||||||
|
String code = lineCaptcha.getCode();
|
||||||
|
String uuid = IdUtil.fastSimpleUUID();
|
||||||
|
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
try {
|
||||||
|
ImageIO.write(lineCaptcha.getImage(), "png", baos);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("生成验证码图片失败", e);
|
||||||
|
}
|
||||||
|
String imageBase64 = "data:image/png;base64," + Base64.getEncoder().encodeToString(baos.toByteArray());
|
||||||
|
|
||||||
|
stringRedisTemplate.opsForValue().set(
|
||||||
|
formatCaptchaKey(uuid),
|
||||||
|
code.toLowerCase(),
|
||||||
|
5,
|
||||||
|
TimeUnit.MINUTES
|
||||||
|
);
|
||||||
|
|
||||||
|
Map<String, String> result = new HashMap<>();
|
||||||
|
result.put("uuid", uuid);
|
||||||
|
result.put("imageBase64", imageBase64);
|
||||||
|
return CommonResult.success(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Boolean checkCaptcha(String uuid, String code) {
|
||||||
|
if (!this.ssoProperties.isLoginCaptcha()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (org.springframework.util.StringUtils.isBlank(uuid) || org.springframework.util.StringUtils.isBlank(code)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String key = formatCaptchaKey(uuid);
|
||||||
|
String cachedCode = stringRedisTemplate.opsForValue().get(key);
|
||||||
|
if (cachedCode == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
stringRedisTemplate.delete(key);
|
||||||
|
return cachedCode.equalsIgnoreCase(code);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String formatCaptchaKey(String uuid) {
|
||||||
|
return "captcha:" + uuid;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user