Commit a48d5905 authored by 技术部-任文超's avatar 技术部-任文超

完成验证码重构,未联调

parent e9a51fa7
package cn.quantgroup.xyqb.aspect.captcha; package cn.quantgroup.xyqb.aspect.captcha;
import cn.quantgroup.xyqb.Constants; import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.model.ClientType;
import cn.quantgroup.xyqb.model.JsonResult; import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.service.captcha.IGeetestCaptchaService; import cn.quantgroup.xyqb.service.captcha.IGeetestCaptchaService;
import cn.quantgroup.xyqb.service.captcha.IQuantgroupCaptchaService; import cn.quantgroup.xyqb.service.captcha.IQuantgroupCaptchaService;
...@@ -78,7 +79,7 @@ public class CaptchaNewValidateAdvisor { ...@@ -78,7 +79,7 @@ public class CaptchaNewValidateAdvisor {
String phoneNo = request.getParameter("phoneNo"); String phoneNo = request.getParameter("phoneNo");
String clientType = request.getParameter("clientType"); String clientType = request.getParameter("clientType");
log.info("使用极验二次验证,phoneNo:{}", phoneNo); log.info("使用极验二次验证,phoneNo:{}", phoneNo);
return geetestCaptchaService.isCaptchaValidateSuccess(clientType, PasswordUtil.MD5(phoneNo), IPUtil.getRemoteIP(request), challenge, validate, seccode); return geetestCaptchaService.validGeetestCaptcha(ClientType.valueByName(clientType), PasswordUtil.MD5(phoneNo), IPUtil.getRemoteIP(request), challenge, validate, seccode);
} }
/** /**
...@@ -90,7 +91,7 @@ public class CaptchaNewValidateAdvisor { ...@@ -90,7 +91,7 @@ public class CaptchaNewValidateAdvisor {
private boolean qgValid(HttpServletRequest request) { private boolean qgValid(HttpServletRequest request) {
String captchaId = Optional.ofNullable(request.getParameter("captchaId")).orElse(""); String captchaId = Optional.ofNullable(request.getParameter("captchaId")).orElse("");
String captchaValue = request.getParameter("captchaValue"); String captchaValue = request.getParameter("captchaValue");
return quantgroupCaptchaService.validCaptcha(captchaId, captchaValue); return quantgroupCaptchaService.validQuantgroupCaptcha(captchaId, captchaValue);
} }
} }
package cn.quantgroup.xyqb.controller.external.captcha; package cn.quantgroup.xyqb.controller.external.captcha;
import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.aspect.captcha.CaptchaNewValidator; import cn.quantgroup.xyqb.aspect.captcha.CaptchaNewValidator;
import cn.quantgroup.xyqb.aspect.logcaller.LogHttpCaller; import cn.quantgroup.xyqb.aspect.logcaller.LogHttpCaller;
import cn.quantgroup.xyqb.model.ClientType;
import cn.quantgroup.xyqb.model.JsonResult; import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.service.captcha.IGeetestCaptchaService; import cn.quantgroup.xyqb.service.captcha.IGeetestCaptchaService;
import cn.quantgroup.xyqb.service.captcha.IQuantgroupCaptchaService; import cn.quantgroup.xyqb.service.captcha.IQuantgroupCaptchaService;
...@@ -9,11 +11,16 @@ import cn.quantgroup.xyqb.util.IPUtil; ...@@ -9,11 +11,16 @@ import cn.quantgroup.xyqb.util.IPUtil;
import cn.quantgroup.xyqb.util.PasswordUtil; import cn.quantgroup.xyqb.util.PasswordUtil;
import cn.quantgroup.xyqb.util.ValidationUtil; import cn.quantgroup.xyqb.util.ValidationUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/** /**
* @author xufei on 2018/1/30. * @author xufei on 2018/1/30.
...@@ -22,35 +29,36 @@ import javax.servlet.http.HttpServletRequest; ...@@ -22,35 +29,36 @@ import javax.servlet.http.HttpServletRequest;
@RestController @RestController
@RequestMapping("/api") @RequestMapping("/api")
public class NewCaptchaController { public class NewCaptchaController {
@Resource @Resource
private IGeetestCaptchaService geetestCaptchaService; private IGeetestCaptchaService geetestCaptchaService;
@Resource @Resource
private IQuantgroupCaptchaService quantgroupCaptchaService; private IQuantgroupCaptchaService quantgroupCaptchaService;
@LogHttpCaller @LogHttpCaller
@RequestMapping(value = "/newCaptcha") @RequestMapping(value = "/captcha/new")
public JsonResult getCaptcha(HttpServletRequest request, String phoneNo) { public JsonResult getCaptcha(String phoneNo, String clientType, HttpServletRequest request) {
if (!ValidationUtil.validatePhoneNo(phoneNo)) { if (!ValidationUtil.validatePhoneNo(phoneNo)) {
log.info("获取验证码失败,phoneNo:{}, clientType:{}", phoneNo, clientType);
return JsonResult.buildErrorStateResult("手机号格式错误", null); return JsonResult.buildErrorStateResult("手机号格式错误", null);
} }
// 数据容器
log.info("[newCaptcha]获取验证码,phoneNo:{}", phoneNo); Map<String, String> data = new HashMap<String, String>();
String geetestCaptcha = geetestCaptchaService.fetchGeetestCaptcha(PasswordUtil.MD5(phoneNo), // 优先获取极验
IPUtil.getRemoteIP(request), request.getParameter("clientType")); Map<String, String> imgMap = geetestCaptchaService.fetchGeetestCaptcha(PasswordUtil.MD5(phoneNo), IPUtil.getRemoteIP(request), ClientType.valueByName(clientType));
log.info("结果geetestCaptcha:{},phoneNo:{}", geetestCaptcha,phoneNo); data.put(Constants.TEST_PARAM, Constants.TEST_TYPE_GT);
if (null != geetestCaptcha) { // 备选方案:量化派图形验证码
return JsonResult.buildSuccessResult("", geetestCaptcha); if(Objects.isNull(imgMap) || imgMap.isEmpty()){
} else { imgMap = quantgroupCaptchaService.fetchQuantgroupCaptcha(request.getLocale());
try { data.put(Constants.TEST_PARAM, Constants.TEST_TYPE_QG);
return JsonResult.buildSuccessResult("", quantgroupCaptchaService.fetchCaptcha(request.getLocale()));
} catch (Exception e) {
log.error("获取验证码失败e:{}", e);
return JsonResult.buildErrorStateResult("", "fail");
} }
// 返回结果
if(Objects.isNull(imgMap) || imgMap.isEmpty()){
return JsonResult.buildErrorStateResult("获取验证码失败", "");
} }
// 填充数据并返回
data.putAll(imgMap);
return JsonResult.buildSuccessResult("", data);
} }
@CaptchaNewValidator @CaptchaNewValidator
...@@ -59,5 +67,4 @@ public class NewCaptchaController { ...@@ -59,5 +67,4 @@ public class NewCaptchaController {
return JsonResult.buildSuccessResult("", null); return JsonResult.buildSuccessResult("", null);
} }
} }
package cn.quantgroup.xyqb.model;
import java.util.Optional;
/**
* 验证码验证类型
* @author renwc
* @date 2018-02-02
*/
public enum ClientType {
WEB, APP, H5;
public final static ClientType valueByName(String name) {
name = Optional.ofNullable(name).orElse("").toLowerCase();
switch (name){
case "app":
return APP;
case "h5":
return H5;
case "web":
return WEB;
default:
return APP;
}
}
}
...@@ -11,6 +11,7 @@ import java.net.URL; ...@@ -11,6 +11,7 @@ import java.net.URL;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
/** /**
* Java SDK * Java SDK
...@@ -46,7 +47,7 @@ public class GeetestLib { ...@@ -46,7 +47,7 @@ public class GeetestLib {
/** /**
* 返回字符串 * 返回字符串
*/ */
private String responseStr; private Map<String, String> responseStr;
/** /**
* 调试开关,是否输出调试日志 * 调试开关,是否输出调试日志
...@@ -73,7 +74,7 @@ public class GeetestLib { ...@@ -73,7 +74,7 @@ public class GeetestLib {
* *
* @return 初始化结果 * @return 初始化结果
*/ */
public String getResponseStr(HashMap<String, String> param) { public Map<String, String> getResponseStr(HashMap<String, String> param) {
preProcess(param); preProcess(param);
return responseStr; return responseStr;
} }
...@@ -84,32 +85,20 @@ public class GeetestLib { ...@@ -84,32 +85,20 @@ public class GeetestLib {
* *
* @return * @return
*/ */
private String getFailPreProcessRes() { private Map<String, String> getFailPreProcessRes() {
return null; return null;
} }
/** /**
* 预处理成功后的标准串 * 预处理成功后的标准串
*/ */
private String getSuccessPreProcessRes(String challenge) { private Map<String, String> getSuccessPreProcessRes(String challenge) {
gtlog("challenge:" + challenge); gtlog("challenge:" + challenge);
Map<String, String> data = new HashMap<String, String>(3);
JSONObject jsonObject = new JSONObject(); data.put("success", "0");
try { data.put("gt", this.captchaId);
data.put("challenge", challenge);
jsonObject.put("success", "0"); return data;
jsonObject.put("gt", this.captchaId);
jsonObject.put("challenge", challenge);
} catch (JSONException e) {
gtlog("json dumps error");
}
return jsonObject.toString();
} }
/** /**
......
package cn.quantgroup.xyqb.service.captcha; package cn.quantgroup.xyqb.service.captcha;
import cn.quantgroup.xyqb.model.ClientType;
import java.util.Map;
/** /**
* @author xufei on 2018/1/30. * @author xufei on 2018/1/30.
*/ */
...@@ -13,7 +17,7 @@ public interface IGeetestCaptchaService { ...@@ -13,7 +17,7 @@ public interface IGeetestCaptchaService {
* @param clientType * @param clientType
* @return 获取失败返回null * @return 获取失败返回null
*/ */
String fetchGeetestCaptcha(String markStr, String remoteIp, String clientType); Map<String, String> fetchGeetestCaptcha(String markStr, String remoteIp, ClientType clientType);
/** /**
...@@ -27,6 +31,6 @@ public interface IGeetestCaptchaService { ...@@ -27,6 +31,6 @@ public interface IGeetestCaptchaService {
* @param seccode * @param seccode
* @return * @return
*/ */
Boolean isCaptchaValidateSuccess(String clientType, String phoneNo, String remoteIp, String challenge, String validate, String seccode); boolean validGeetestCaptcha(ClientType clientType, String phoneNo, String remoteIp, String challenge, String validate, String seccode);
} }
...@@ -13,7 +13,7 @@ public interface IQuantgroupCaptchaService { ...@@ -13,7 +13,7 @@ public interface IQuantgroupCaptchaService {
* @return * @return
* @throws Exception EX * @throws Exception EX
*/ */
Map<String, String> fetchCaptcha(Locale locale); Map<String, String> fetchQuantgroupCaptcha(Locale locale);
/** /**
* 校验QG验证码 * 校验QG验证码
...@@ -21,5 +21,5 @@ public interface IQuantgroupCaptchaService { ...@@ -21,5 +21,5 @@ public interface IQuantgroupCaptchaService {
* @param code * @param code
* @return * @return
*/ */
boolean validCaptcha(String key, String code); boolean validQuantgroupCaptcha(String key, String code);
} }
package cn.quantgroup.xyqb.service.captcha.impl; package cn.quantgroup.xyqb.service.captcha.impl;
import cn.quantgroup.xyqb.Constants; import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.model.ClientType;
import cn.quantgroup.xyqb.service.captcha.GeetestLib; import cn.quantgroup.xyqb.service.captcha.GeetestLib;
import cn.quantgroup.xyqb.service.captcha.IGeetestCaptchaService; import cn.quantgroup.xyqb.service.captcha.IGeetestCaptchaService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
...@@ -11,6 +11,8 @@ import org.springframework.stereotype.Service; ...@@ -11,6 +11,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/** /**
* @author xufei on 2018/1/30. * @author xufei on 2018/1/30.
...@@ -37,32 +39,32 @@ public class GeetestCaptchaServiceImpl implements IGeetestCaptchaService { ...@@ -37,32 +39,32 @@ public class GeetestCaptchaServiceImpl implements IGeetestCaptchaService {
@Value("${geetest.api.url}") @Value("${geetest.api.url}")
private String apiUrl; private String apiUrl;
private GeetestLib getGeetestSdk() { @Override
return new GeetestLib(captchaId, privateKey, newFailback, apiUrl); public Map<String, String> fetchGeetestCaptcha(String markStr, String remoteIp, ClientType clientType) {
GeetestLib gtSdk = new GeetestLib(captchaId, privateKey, newFailback, apiUrl);
HashMap<String, String> param = getParam(markStr, remoteIp, clientType);
return gtSdk.getResponseStr(param);
} }
@Override @Override
public String fetchGeetestCaptcha(String markStr, String remoteIp, String clientType) { public boolean validGeetestCaptcha(ClientType clientType, String markStr, String remoteIp, String challenge, String validate, String seccode) {
GeetestLib gtSdk = new GeetestLib(captchaId, privateKey, newFailback, apiUrl);
HashMap<String, String> param = getParam(markStr, remoteIp, clientType); HashMap<String, String> param = getParam(markStr, remoteIp, clientType);
return Constants.GT_CAPTCHA_VALIDATE_SUCCESS==getGeetestSdk().enhencedValidateRequest(challenge, validate, seccode, param);
}
return gtSdk.getResponseStr(param); private GeetestLib getGeetestSdk() {
return new GeetestLib(captchaId, privateKey, newFailback, apiUrl);
} }
private HashMap<String, String> getParam(String markStr, String remoteIp, String clientType) { private HashMap<String, String> getParam(String markStr, String remoteIp, ClientType clientType) {
HashMap<String, String> param = new HashMap<>(); HashMap<String, String> param = new HashMap<>();
param.put("user_id", markStr); param.put("user_id", markStr);
if (StringUtils.isBlank(clientType)) { if (Objects.isNull(clientType)) {
param.put("client_type", "APP"); param.put("client_type", ClientType.APP.name());
}else{
param.put("client_type", clientType.name());
} }
param.put("client_type", clientType);
param.put("ip_address", remoteIp); param.put("ip_address", remoteIp);
return param; return param;
} }
@Override
public Boolean isCaptchaValidateSuccess(String clientType, String markStr, String remoteIp, String challenge, String validate, String seccode) {
HashMap<String, String> param = getParam(markStr, remoteIp, clientType);
return Constants.GT_CAPTCHA_VALIDATE_SUCCESS==getGeetestSdk().enhencedValidateRequest(challenge, validate, seccode, param);
}
} }
...@@ -35,9 +35,7 @@ public class QuantgroupCaptchaServiceImpl implements IQuantgroupCaptchaService { ...@@ -35,9 +35,7 @@ public class QuantgroupCaptchaServiceImpl implements IQuantgroupCaptchaService {
private AbstractManageableImageCaptchaService imageCaptchaService; private AbstractManageableImageCaptchaService imageCaptchaService;
@Override @Override
public Map<String, String> fetchCaptcha(Locale locale) { public Map<String, String> fetchQuantgroupCaptcha(Locale locale) {
Map<String, String> data = new HashMap<>();
data.put(Constants.TEST_PARAM, Constants.TEST_TYPE_QG);
String imageId = UUID.randomUUID().toString(); String imageId = UUID.randomUUID().toString();
BufferedImage challenge = imageCaptchaService.getImageChallengeForID(Constants.IMAGE_CAPTCHA_KEY + imageId, locale); BufferedImage challenge = imageCaptchaService.getImageChallengeForID(Constants.IMAGE_CAPTCHA_KEY + imageId, locale);
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream(); ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
...@@ -45,21 +43,22 @@ public class QuantgroupCaptchaServiceImpl implements IQuantgroupCaptchaService { ...@@ -45,21 +43,22 @@ public class QuantgroupCaptchaServiceImpl implements IQuantgroupCaptchaService {
ImageIO.write(challenge, IMAGE_FORMAT_PNG, jpegOutputStream); ImageIO.write(challenge, IMAGE_FORMAT_PNG, jpegOutputStream);
} catch (IOException e) { } catch (IOException e) {
log.error("生成QG图形验证码", e); log.error("生成QG图形验证码", e);
return data; return null;
} }
String imageBase64 = Base64.encodeBase64String(jpegOutputStream.toByteArray()); String imageBase64 = Base64.encodeBase64String(jpegOutputStream.toByteArray());
Map<String, String> data = new HashMap<String, String>(2);
data.put("imageId", imageId); data.put("imageId", imageId);
data.put("image", String.format(IMG_BASE64_PATTREN, imageBase64)); data.put("image", String.format(IMG_BASE64_PATTREN, imageBase64));
return data; return data;
} }
@Override @Override
public boolean validCaptcha(String key, String code) { public boolean validQuantgroupCaptcha(String key, String code) {
Boolean validCaptcha = false; boolean validCaptcha = false;
if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(code)) { if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(code)) {
// 验证码校验(忽略用户输入的大小写) // 验证码校验(忽略用户输入的大小写)
try { try {
validCaptcha = imageCaptchaService.validateResponseForID(Constants.IMAGE_CAPTCHA_KEY + key, code.toLowerCase()); validCaptcha = Boolean.valueOf(imageCaptchaService.validateResponseForID(Constants.IMAGE_CAPTCHA_KEY + key, code.toLowerCase()));
} catch (CaptchaServiceException e) { } catch (CaptchaServiceException e) {
log.error("校验QG图形验证码:key:{}, code:{}", key, code, e); log.error("校验QG图形验证码:key:{}, code:{}", key, code, e);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment