【captcha】整理代码

pull/3/head
fengshuonan 2021-01-16 10:27:23 +08:00
parent d3e6943810
commit d413d70bb3
13 changed files with 51 additions and 110 deletions

View File

@ -61,7 +61,6 @@
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>

View File

@ -32,7 +32,6 @@
<version>1.0.0</version>
</dependency>
</dependencies>
<build>

View File

@ -67,18 +67,7 @@ public enum AuthExceptionEnum implements AbstractExceptionEnum {
/**
*
*/
ONLY_SUPER_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "11", "权限校验失败,只有超级管理员可以授权所有数据"),
/**
*
*/
KAPTCHA_EMPTY(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "12", "验证码不能为空"),
/**
*
*/
KAPTCHA_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "13", "验证码错误");
ONLY_SUPER_ERROR(RuleConstants.BUSINESS_ERROR_TYPE_CODE + AuthConstants.AUTH_EXCEPTION_STEP_CODE + "11", "权限校验失败,只有超级管理员可以授权所有数据");
/**
*

View File

@ -34,12 +34,13 @@ public class LoginRequest extends BaseRequest {
private Boolean rememberMe = false;
/**
*
* key
*/
private String verKey;
/**
*
*/
private String verCode;
/**
* key
*/
private String verKey;
}

View File

@ -23,6 +23,7 @@ import cn.stylefeng.roses.kernel.system.enums.UserStatusEnum;
import cn.stylefeng.roses.kernel.system.expander.SystemConfigExpander;
import cn.stylefeng.roses.kernel.system.pojo.user.UserLoginInfoDTO;
import cn.stylefeng.roses.kernel.validator.CaptchaApi;
import cn.stylefeng.roses.kernel.validator.exception.enums.ValidatorExceptionEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -172,13 +173,14 @@ public class AuthServiceImpl implements AuthServiceApi {
// 2. 如果开启了验证码校验,则验证当前请求的验证码是否正确
if (SystemConfigExpander.getCaptchaOpen()) {
String verCode = loginRequest.getVerCode();
String verKey = loginRequest.getVerKey();
if (StrUtil.isEmpty(verCode) || StrUtil.isEmpty(verKey)) {
throw new AuthException(AuthExceptionEnum.KAPTCHA_EMPTY);
String verCode = loginRequest.getVerCode();
if (StrUtil.isEmpty(verKey) || StrUtil.isEmpty(verCode)) {
throw new AuthException(ValidatorExceptionEnum.CAPTCHA_EMPTY);
}
if (!captchaApi.validate(verCode, verKey)) {
throw new AuthException(AuthExceptionEnum.KAPTCHA_ERROR);
if (captchaApi.validateCaptcha(verKey, verCode)) {
throw new AuthException(ValidatorExceptionEnum.CAPTCHA_EMPTY);
}
}

View File

@ -23,13 +23,13 @@ public interface CaptchaApi {
/**
*
*
* @param verCode
* @param verKey key
* @return
* @param verCode
* @return true-false-
* @author chenjinlong
* @date 2021/1/15 12:38
*/
boolean validate(String verCode, String verKey);
boolean validateCaptcha(String verKey, String verCode);
/**
* key
@ -40,5 +40,4 @@ public interface CaptchaApi {
*/
String getVerCode(String verKey);
}

View File

@ -49,7 +49,17 @@ public enum ValidatorExceptionEnum implements AbstractExceptionEnum {
/**
*
*/
TABLE_UNIQUE_VALIDATE_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "07", "数据库字段值唯一性校验出错,具体信息:{}");
TABLE_UNIQUE_VALIDATE_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "07", "数据库字段值唯一性校验出错,具体信息:{}"),
/**
*
*/
CAPTCHA_EMPTY(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "08", "验证码参数不能为空"),
/**
*
*/
CAPTCHA_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + ValidatorConstants.VALIDATOR_EXCEPTION_STEP_CODE + "09", "验证码错误");
/**
*

View File

@ -22,4 +22,5 @@ public class EasyCaptcha {
* Base64
*/
private String verImage;
}

View File

@ -31,7 +31,7 @@ public class CaptchaService implements CaptchaApi {
}
@Override
public boolean validate(String verCode, String verKey) {
public boolean validateCaptcha(String verKey, String verCode) {
if (StrUtil.isAllEmpty(verKey, verCode)) {
return false;
}
@ -48,5 +48,4 @@ public class CaptchaService implements CaptchaApi {
return cacheOperatorApi.get(verKey);
}
}

View File

@ -1,66 +0,0 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
GunsAPACHE LICENSE 2.0使
1.LICENSE
2.Guns
3.
4. https://gitee.com/stylefeng/guns-separation
5. https://gitee.com/stylefeng/guns-separation
6.Guns https://www.stylefeng.cn
*/
package cn.stylefeng.roses.kernel.system.exception.enums;
import cn.stylefeng.roses.kernel.rule.abstracts.AbstractExceptionEnum;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import cn.stylefeng.roses.kernel.system.constants.SystemConstants;
import lombok.Getter;
/**
*
*
* @author majianguo
* @date 2020/11/5 11:06
*/
@Getter
public enum SysSmsExceptionEnum implements AbstractExceptionEnum {
/**
*
*/
KAPTCHA_EMPTY(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "101", "验证码不能为空"),
/**
*
*/
KAPTCHA_ERROR(RuleConstants.USER_OPERATION_ERROR_TYPE_CODE + SystemConstants.SYSTEM_EXCEPTION_STEP_CODE + "102", "验证码错误");
/**
*
*/
private final String errorCode;
/**
*
*/
private final String userTip;
SysSmsExceptionEnum(String errorCode, String userTip) {
this.errorCode = errorCode;
this.userTip = userTip;
}
}

View File

@ -28,16 +28,16 @@ public class SysSmsSendParam {
@NotBlank(message = "模板号为空请检查templateCode参数")
private String templateCode;
/**
*
*/
private String verCode;
/**
* key
*/
private String verKey;
/**
*
*/
private String verCode;
/**
*
*/

View File

@ -19,8 +19,8 @@ import cn.stylefeng.roses.kernel.sms.modular.param.SysSmsSendParam;
import cn.stylefeng.roses.kernel.sms.modular.param.SysSmsVerifyParam;
import cn.stylefeng.roses.kernel.sms.modular.service.SysSmsInfoService;
import cn.stylefeng.roses.kernel.system.exception.SystemModularException;
import cn.stylefeng.roses.kernel.system.exception.enums.SysSmsExceptionEnum;
import cn.stylefeng.roses.kernel.validator.CaptchaApi;
import cn.stylefeng.roses.kernel.validator.exception.enums.ValidatorExceptionEnum;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@ -56,13 +56,13 @@ public class SysSmsInfoServiceImpl extends ServiceImpl<SysSmsMapper, SysSms> imp
@Transactional(rollbackFor = Exception.class)
@Override
public boolean sendShortMessage(SysSmsSendParam sysSmsSendParam) {
String verCode = sysSmsSendParam.getVerCode();
String verKey = sysSmsSendParam.getVerKey();
if (StrUtil.isEmpty(verCode) || StrUtil.isEmpty(verKey)) {
throw new SystemModularException(SysSmsExceptionEnum.KAPTCHA_EMPTY);
String verCode = sysSmsSendParam.getVerCode();
if (StrUtil.isEmpty(verKey) || StrUtil.isEmpty(verCode)) {
throw new SystemModularException(ValidatorExceptionEnum.CAPTCHA_EMPTY);
}
if (!captchaApi.validate(verCode, verKey)) {
throw new SystemModularException(SysSmsExceptionEnum.KAPTCHA_ERROR);
if (captchaApi.validateCaptcha(verKey, verCode)) {
throw new SystemModularException(ValidatorExceptionEnum.CAPTCHA_ERROR);
}
Map<String, Object> params = sysSmsSendParam.getParams();

View File

@ -103,6 +103,7 @@
<aws.sdk.version>1.11.106</aws.sdk.version>
<minio.version>3.0.10</minio.version>
<rocketmq.version>4.5.2</rocketmq.version>
<easy.captcha>1.6.2</easy.captcha>
</properties>
<dependencyManagement>
@ -240,6 +241,13 @@
<version>${minio.version}</version>
</dependency>
<!-- EasyCaptcha图形验证码 -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>${easy.captcha}</version>
</dependency>
</dependencies>
</dependencyManagement>