com.xingyuv.captcha.service.impl.DefaultCaptchaServiceImpl Maven / Gradle / Ivy
The newest version!
/*
*Copyright © 2018 anji-plus
*安吉加加信息技术有限公司
*http://www.anji-plus.com
*All rights reserved.
*/
package com.xingyuv.captcha.service.impl;
import com.xingyuv.captcha.model.common.RepCodeEnum;
import com.xingyuv.captcha.model.common.ResponseModel;
import com.xingyuv.captcha.model.vo.CaptchaVO;
import com.xingyuv.captcha.service.CaptchaService;
import com.xingyuv.captcha.util.StringUtils;
import java.util.Properties;
/**
* Created by raodeming on 2019/12/25.
*/
public class DefaultCaptchaServiceImpl extends AbstractCaptchaService {
@Override
public String captchaType() {
return "default";
}
@Override
public void init(Properties config) {
for (String s : CaptchaServiceFactory.instances.keySet()) {
if (captchaType().equals(s)) {
continue;
}
getService(s).init(config);
}
}
@Override
public void destroy(Properties config) {
for (String s : CaptchaServiceFactory.instances.keySet()) {
if (captchaType().equals(s)) {
continue;
}
getService(s).destroy(config);
}
}
private CaptchaService getService(String captchaType) {
return CaptchaServiceFactory.instances.get(captchaType);
}
@Override
public ResponseModel get(CaptchaVO captchaVO) {
if (captchaVO == null) {
return RepCodeEnum.NULL_ERROR.parseError("captchaVO");
}
if (StringUtils.isEmpty(captchaVO.getCaptchaType())) {
return RepCodeEnum.NULL_ERROR.parseError("类型");
}
return getService(captchaVO.getCaptchaType()).get(captchaVO);
}
@Override
public ResponseModel check(CaptchaVO captchaVO) {
if (captchaVO == null) {
return RepCodeEnum.NULL_ERROR.parseError("captchaVO");
}
if (StringUtils.isEmpty(captchaVO.getCaptchaType())) {
return RepCodeEnum.NULL_ERROR.parseError("类型");
}
if (StringUtils.isEmpty(captchaVO.getToken())) {
return RepCodeEnum.NULL_ERROR.parseError("token");
}
return getService(captchaVO.getCaptchaType()).check(captchaVO);
}
@Override
public ResponseModel verification(CaptchaVO captchaVO) {
if (captchaVO == null) {
return RepCodeEnum.NULL_ERROR.parseError("captchaVO");
}
if (StringUtils.isEmpty(captchaVO.getCaptchaVerification())) {
return RepCodeEnum.NULL_ERROR.parseError("二次校验参数");
}
try {
String codeKey = String.format(REDIS_SECOND_CAPTCHA_KEY, captchaVO.getCaptchaVerification());
if (!CaptchaServiceFactory.getCache(cacheType).exists(codeKey)) {
return ResponseModel.errorMsg(RepCodeEnum.API_CAPTCHA_INVALID);
}
//二次校验取值后,即刻失效
CaptchaServiceFactory.getCache(cacheType).delete(codeKey);
} catch (Exception e) {
logger.error("验证码坐标解析失败", e);
return ResponseModel.errorMsg(e.getMessage());
}
return ResponseModel.success();
}
}