|
@@ -2,9 +2,13 @@ package com.onemap.gateway.service.impl;
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.IOException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.NoSuchProviderException;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.imageio.ImageIO;
|
|
|
+
|
|
|
+import com.onemap.common.core.utils.RSAUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.FastByteArrayOutputStream;
|
|
@@ -26,8 +30,7 @@ import com.onemap.gateway.service.ValidateCodeService;
|
|
|
* @author onemap
|
|
|
*/
|
|
|
@Service
|
|
|
-public class ValidateCodeServiceImpl implements ValidateCodeService
|
|
|
-{
|
|
|
+public class ValidateCodeServiceImpl implements ValidateCodeService {
|
|
|
@Resource(name = "captchaProducer")
|
|
|
private Producer captchaProducer;
|
|
|
|
|
@@ -44,16 +47,17 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
|
|
|
* 生成验证码
|
|
|
*/
|
|
|
@Override
|
|
|
- public AjaxResult createCaptcha() throws IOException, CaptchaException
|
|
|
- {
|
|
|
+ public AjaxResult createCaptcha() throws IOException, CaptchaException, NoSuchAlgorithmException, NoSuchProviderException {
|
|
|
AjaxResult ajax = AjaxResult.success();
|
|
|
boolean captchaEnabled = captchaProperties.getEnabled();
|
|
|
ajax.put("captchaEnabled", captchaEnabled);
|
|
|
- if (!captchaEnabled)
|
|
|
- {
|
|
|
+ if (!captchaEnabled) {
|
|
|
return ajax;
|
|
|
}
|
|
|
|
|
|
+ //生成公私钥
|
|
|
+ String[] keyPair = RSAUtils.generateKeyPair();
|
|
|
+
|
|
|
// 保存验证码信息
|
|
|
String uuid = IdUtils.simpleUUID();
|
|
|
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
|
|
@@ -63,15 +67,12 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
|
|
|
|
|
|
String captchaType = captchaProperties.getType();
|
|
|
// 生成验证码
|
|
|
- if ("math".equals(captchaType))
|
|
|
- {
|
|
|
+ if ("math".equals(captchaType)) {
|
|
|
String capText = captchaProducerMath.createText();
|
|
|
capStr = capText.substring(0, capText.lastIndexOf("@"));
|
|
|
code = capText.substring(capText.lastIndexOf("@") + 1);
|
|
|
image = captchaProducerMath.createImage(capStr);
|
|
|
- }
|
|
|
- else if ("char".equals(captchaType))
|
|
|
- {
|
|
|
+ } else if ("char".equals(captchaType)) {
|
|
|
capStr = code = captchaProducer.createText();
|
|
|
image = captchaProducer.createImage(capStr);
|
|
|
}
|
|
@@ -79,17 +80,19 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
|
|
|
redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
|
|
|
// 转换流信息写出
|
|
|
FastByteArrayOutputStream os = new FastByteArrayOutputStream();
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
ImageIO.write(image, "jpg", os);
|
|
|
- }
|
|
|
- catch (IOException e)
|
|
|
- {
|
|
|
+ } catch (IOException e) {
|
|
|
return AjaxResult.error(e.getMessage());
|
|
|
}
|
|
|
|
|
|
ajax.put("uuid", uuid);
|
|
|
ajax.put("img", Base64.encode(os.toByteArray()));
|
|
|
+ //返回公钥信息
|
|
|
+ ajax.put("publicKeyString", keyPair[0]);
|
|
|
+ // 保存私钥信息
|
|
|
+ String keyPairKey = CacheConstants.LOGIN_RSA_KEY + uuid;
|
|
|
+ redisService.setCacheObject(keyPairKey, keyPair[1], Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
|
|
|
return ajax;
|
|
|
}
|
|
|
|
|
@@ -97,22 +100,18 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
|
|
|
* 校验验证码
|
|
|
*/
|
|
|
@Override
|
|
|
- public void checkCaptcha(String code, String uuid) throws CaptchaException
|
|
|
- {
|
|
|
- if (StringUtils.isEmpty(code))
|
|
|
- {
|
|
|
+ public void checkCaptcha(String code, String uuid) throws CaptchaException {
|
|
|
+ if (StringUtils.isEmpty(code)) {
|
|
|
throw new CaptchaException("验证码不能为空");
|
|
|
}
|
|
|
- if (StringUtils.isEmpty(uuid))
|
|
|
- {
|
|
|
+ if (StringUtils.isEmpty(uuid)) {
|
|
|
throw new CaptchaException("验证码已失效");
|
|
|
}
|
|
|
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
|
|
|
String captcha = redisService.getCacheObject(verifyKey);
|
|
|
redisService.deleteObject(verifyKey);
|
|
|
|
|
|
- if (!code.equalsIgnoreCase(captcha))
|
|
|
- {
|
|
|
+ if (!code.equalsIgnoreCase(captcha)) {
|
|
|
throw new CaptchaException("验证码错误");
|
|
|
}
|
|
|
}
|