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

完成验证码重构,未联调

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