修改h5验证码文案

parent d8e03d20
...@@ -5,6 +5,7 @@ import cn.quantgroup.xyqb.controller.IBaseController; ...@@ -5,6 +5,7 @@ import cn.quantgroup.xyqb.controller.IBaseController;
import cn.quantgroup.xyqb.model.JsonResult; import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.thirdparty.jcaptcha.AbstractManageableImageCaptchaService; import cn.quantgroup.xyqb.thirdparty.jcaptcha.AbstractManageableImageCaptchaService;
import com.octo.captcha.service.CaptchaServiceException; import com.octo.captcha.service.CaptchaServiceException;
import java.io.PipedReader; import java.io.PipedReader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.security.PrivateKey; import java.security.PrivateKey;
...@@ -12,6 +13,7 @@ import java.util.Optional; ...@@ -12,6 +13,7 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
...@@ -38,175 +40,178 @@ import org.springframework.web.context.request.ServletRequestAttributes; ...@@ -38,175 +40,178 @@ import org.springframework.web.context.request.ServletRequestAttributes;
@Component @Component
public class CaptchaNewValidateAdvisor { public class CaptchaNewValidateAdvisor {
private static final Logger LOGGER = LoggerFactory.getLogger(CaptchaNewValidateAdvisor.class); private static final Logger LOGGER = LoggerFactory.getLogger(CaptchaNewValidateAdvisor.class);
private static final String SUPER_CAPTCHA_ID = UUID.nameUUIDFromBytes("__QG_APPCLIENT_AGENT__".getBytes(Charset.forName("UTF-8"))).toString(); private static final String SUPER_CAPTCHA_ID = UUID.nameUUIDFromBytes("__QG_APPCLIENT_AGENT__".getBytes(Charset.forName("UTF-8"))).toString();
private static final String SUPER_CAPTCHA = "__SUPERQG__"; private static final String SUPER_CAPTCHA = "__SUPERQG__";
@Autowired @Autowired
@Qualifier("stringRedisTemplate") @Qualifier("stringRedisTemplate")
private RedisTemplate<String, String> redisTemplate; private RedisTemplate<String, String> redisTemplate;
@Autowired @Autowired
@Qualifier("customCaptchaService") @Qualifier("customCaptchaService")
private AbstractManageableImageCaptchaService imageCaptchaService; private AbstractManageableImageCaptchaService imageCaptchaService;
/** /**
* 自动化测试忽略验证码 * 自动化测试忽略验证码
*/ */
@Value("${xyqb.auth.captcha.autotest.enable:false}") @Value("${xyqb.auth.captcha.autotest.enable:false}")
private boolean autoTestCaptchaEnabled; private boolean autoTestCaptchaEnabled;
/** /**
* 图形验证码切面 * 图形验证码切面
*/ */
@Pointcut("@annotation(cn.quantgroup.xyqb.aspect.captcha.CaptchaNewValidator)") @Pointcut("@annotation(cn.quantgroup.xyqb.aspect.captcha.CaptchaNewValidator)")
private void needNewCaptchaValidate() { private void needNewCaptchaValidate() {
} }
private static final String IMAGE_IP_COUNT = "image:ip"; private static final String IMAGE_IP_COUNT = "image:ip";
private static final String IMAGE_PHONE_COUNT = "image:phone"; private static final String IMAGE_PHONE_COUNT = "image:phone";
private static final String IMAGE_DEVICEID_COUNT = "image:deviceId:"; private static final String IMAGE_DEVICEID_COUNT = "image:deviceId:";
private static final Long FIVE_MIN = 24 * 5L; private static final Long FIVE_MIN = 24 * 5L;
/** /**
* 在受图形验证码保护的接口方法执行前, 执行图形验证码校验 * 在受图形验证码保护的接口方法执行前, 执行图形验证码校验
* *
* @throws Throwable * @throws Throwable
*/ */
@Around("needNewCaptchaValidate()") @Around("needNewCaptchaValidate()")
private Object doCapchaValidate(ProceedingJoinPoint pjp) throws Throwable { private Object doCapchaValidate(ProceedingJoinPoint pjp) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String registerFrom = Optional.ofNullable(request.getParameter("registerFrom")).orElse(""); String registerFrom = Optional.ofNullable(request.getParameter("registerFrom")).orElse("");
String captchaId = Optional.ofNullable(request.getParameter("captchaId")).orElse(""); String captchaId = Optional.ofNullable(request.getParameter("captchaId")).orElse("");
Object captchaValue = request.getParameter("captchaValue"); Object captchaValue = request.getParameter("captchaValue");
String phoneNo = request.getParameter("phoneNo"); String phoneNo = request.getParameter("phoneNo");
String deviceId = Optional.ofNullable(request.getParameter("deviceId")).orElse(""); String deviceId = Optional.ofNullable(request.getParameter("deviceId")).orElse("");
String clientIp = getIp(); String clientIp = getIp();
Long countIP = countIP(clientIp); Long countIP = countIP(clientIp);
Long countPhone = countPhone(phoneNo); Long countPhone = countPhone(phoneNo);
Long countDeviceId = countDeviceId(deviceId); Long countDeviceId = countDeviceId(deviceId);
if (countIP > Constants.Image_Need_Count || countPhone > Constants.Image_Need_Count || countDeviceId > Constants.Image_Need_Count) { if (countIP > Constants.Image_Need_Count || countPhone > Constants.Image_Need_Count || countDeviceId > Constants.Image_Need_Count) {
if (shouldSkipCaptchaValidate(registerFrom, captchaId, captchaValue)) { if (shouldSkipCaptchaValidate(registerFrom, captchaId, captchaValue)) {
LOGGER.info("使用超级图形验证码校验, registerFrom={}, clientIp={}", registerFrom, request.getRemoteAddr()); LOGGER.info("使用超级图形验证码校验, registerFrom={}, clientIp={}", registerFrom, request.getRemoteAddr());
return pjp.proceed(); return pjp.proceed();
} }
JsonResult result = JsonResult.buildSuccessResult("图形验证码不正确", ""); JsonResult result = JsonResult.buildSuccessResult("图形验证码不正确", "");
result.setBusinessCode("0002"); result.setBusinessCode("0002");
if (captchaValue != null) { if (captchaValue != null && StringUtils.isNotEmpty(String.valueOf(captchaValue))) {
String captcha = String.valueOf(captchaValue); String captcha = String.valueOf(captchaValue);
// 忽略用户输入的大小写 // 忽略用户输入的大小写
captcha = StringUtils.lowerCase(captcha); captcha = StringUtils.lowerCase(captcha);
// 验证码校验 // 验证码校验
Boolean validCaptcha = false; Boolean validCaptcha = false;
try { try {
validCaptcha = imageCaptchaService.validateResponseForID(Constants.IMAGE_CAPTCHA_KEY + captchaId, captcha); validCaptcha = imageCaptchaService.validateResponseForID(Constants.IMAGE_CAPTCHA_KEY + captchaId, captcha);
} catch (CaptchaServiceException ex) { } catch (CaptchaServiceException ex) {
LOGGER.error("验证码校验异常, {}, {}", ex.getMessage(), ex); LOGGER.error("验证码校验异常, {}, {}", ex.getMessage(), ex);
} }
if (validCaptcha) {
return pjp.proceed();
}
return result;
}
result.setMsg("请输入图形验证码");
return result;
if (validCaptcha) {
return pjp.proceed();
} }
}
return result; return pjp.proceed();
} }
return pjp.proceed(); private boolean shouldSkipCaptchaValidate(String registerFrom, String captchaId, Object captchaValue) {
}
private boolean shouldSkipCaptchaValidate(String registerFrom, String captchaId, Object captchaValue) { // 如果启用了超级验证码功能, 检查超级验证码, 超级验证码区分大小写
if (autoTestCaptchaEnabled) {
return true;
}
// 如果启用了超级验证码功能, 检查超级验证码, 超级验证码区分大小写 return StringUtils.equals(SUPER_CAPTCHA_ID, String.valueOf(captchaId)) && StringUtils.equals(SUPER_CAPTCHA, String.valueOf(captchaValue));
if (autoTestCaptchaEnabled) {
return true;
} }
return StringUtils.equals(SUPER_CAPTCHA_ID, String.valueOf(captchaId)) && StringUtils.equals(SUPER_CAPTCHA, String.valueOf(captchaValue)); private Long countIP(String clientIp) {
} Long count = 1L;
if (StringUtils.isBlank(clientIp)) {
private Long countIP(String clientIp) { return count;
Long count = 1L; } else {
if (StringUtils.isBlank(clientIp)) { String countString = redisTemplate.opsForValue().get(IMAGE_IP_COUNT + clientIp);
return count; if (StringUtils.isBlank(countString)) {
} else { redisTemplate.opsForValue().set(IMAGE_IP_COUNT + clientIp, String.valueOf(count),
String countString = redisTemplate.opsForValue().get(IMAGE_IP_COUNT + clientIp); FIVE_MIN, TimeUnit.SECONDS);
if (StringUtils.isBlank(countString)) { } else {
redisTemplate.opsForValue().set(IMAGE_IP_COUNT + clientIp, String.valueOf(count), count = Long.valueOf(countString) + 1L;
FIVE_MIN, TimeUnit.SECONDS); redisTemplate.opsForValue().set(IMAGE_IP_COUNT + clientIp, String.valueOf(count),
} else { FIVE_MIN, TimeUnit.SECONDS);
count = Long.valueOf(countString) + 1L; }
redisTemplate.opsForValue().set(IMAGE_IP_COUNT + clientIp, String.valueOf(count), return count;
FIVE_MIN, TimeUnit.SECONDS); }
}
return count;
} }
}
private Long countPhone(String phoneNo) {
private Long countPhone(String phoneNo) { Long count = 1L;
Long count = 1L; String countString = redisTemplate.opsForValue().get(IMAGE_PHONE_COUNT + phoneNo);
String countString = redisTemplate.opsForValue().get(IMAGE_PHONE_COUNT + phoneNo); if (StringUtils.isBlank(countString)) {
if (StringUtils.isBlank(countString)) { redisTemplate.opsForValue().set(IMAGE_PHONE_COUNT + phoneNo, String.valueOf(count),
redisTemplate.opsForValue().set(IMAGE_PHONE_COUNT + phoneNo, String.valueOf(count), FIVE_MIN, TimeUnit.SECONDS);
FIVE_MIN, TimeUnit.SECONDS); } else {
} else { count = Long.valueOf(countString) + 1L;
count = Long.valueOf(countString) + 1L; redisTemplate.opsForValue().set(IMAGE_PHONE_COUNT + phoneNo, String.valueOf(count),
redisTemplate.opsForValue().set(IMAGE_PHONE_COUNT + phoneNo, String.valueOf(count), FIVE_MIN, TimeUnit.SECONDS);
FIVE_MIN, TimeUnit.SECONDS); }
return count;
} }
return count;
} /**
* 短信发送设备限制
/** */
* 短信发送设备限制 private Long countDeviceId(String deviceId) {
*/ Long count = 1L;
private Long countDeviceId(String deviceId) { if (StringUtils.isBlank(deviceId)) {
Long count = 1L; return count;
if (StringUtils.isBlank(deviceId)) { } else {
return count; String countString = redisTemplate.opsForValue().get(IMAGE_DEVICEID_COUNT + deviceId);
} else { if (StringUtils.isBlank(countString)) {
String countString = redisTemplate.opsForValue().get(IMAGE_DEVICEID_COUNT + deviceId); redisTemplate.opsForValue().set(IMAGE_DEVICEID_COUNT + deviceId, String.valueOf(count),
if (StringUtils.isBlank(countString)) { FIVE_MIN, TimeUnit.SECONDS);
redisTemplate.opsForValue().set(IMAGE_DEVICEID_COUNT + deviceId, String.valueOf(count), } else {
FIVE_MIN, TimeUnit.SECONDS); count = Long.valueOf(countString) + 1L;
} else { redisTemplate.opsForValue().set(IMAGE_DEVICEID_COUNT + deviceId, String.valueOf(count),
count = Long.valueOf(countString) + 1L; FIVE_MIN, TimeUnit.SECONDS);
redisTemplate.opsForValue().set(IMAGE_DEVICEID_COUNT + deviceId, String.valueOf(count), }
FIVE_MIN, TimeUnit.SECONDS); return count;
} }
return count;
} }
}
private String getIp() { private String getIp() {
HttpServletRequest request = getRequest(); HttpServletRequest request = getRequest();
String ip = request.getHeader("x-real-ip"); String ip = request.getHeader("x-real-ip");
if (StringUtils.isEmpty(ip)) { if (StringUtils.isEmpty(ip)) {
ip = request.getRemoteAddr(); ip = request.getRemoteAddr();
} }
//过滤反向代理的ip //过滤反向代理的ip
String[] stemps = ip.split(","); String[] stemps = ip.split(",");
if (stemps.length >= 1) { if (stemps.length >= 1) {
//得到第一个IP,即客户端真实IP //得到第一个IP,即客户端真实IP
ip = stemps[0]; ip = stemps[0];
} }
ip = ip.trim(); ip = ip.trim();
if (ip.length() > 23) { if (ip.length() > 23) {
ip = ip.substring(0, 23); ip = ip.substring(0, 23);
} }
return ip; return ip;
} }
private HttpServletRequest getRequest() { private HttpServletRequest getRequest() {
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes(); .getRequestAttributes();
return attrs.getRequest(); return attrs.getRequest();
} }
} }
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