mirror of https://gitee.com/stylefeng/roses
【validator】整理validator模块
parent
7e349a8328
commit
2988cde0eb
|
@ -0,0 +1,65 @@
|
|||
package cn.stylefeng.roses.kernel.validator.context;
|
||||
|
||||
import cn.hutool.core.lang.Dict;
|
||||
|
||||
/**
|
||||
* 临时保存http请求的参数
|
||||
* <p>
|
||||
* 可以保存@RequestBody的可以保存parameter方式传参的
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/8/20
|
||||
*/
|
||||
public class RequestParamContext {
|
||||
|
||||
private static final ThreadLocal<Dict> CONTEXT_HOLDER = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 保存请求参数
|
||||
*
|
||||
* @author stylefeng
|
||||
* @date 2020/6/21 20:17
|
||||
*/
|
||||
public static void set(Dict requestParam) {
|
||||
CONTEXT_HOLDER.set(requestParam);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存请求参数
|
||||
*
|
||||
* @author stylefeng
|
||||
* @date 2020/6/21 20:17
|
||||
*/
|
||||
public static void setObject(Object requestParam) {
|
||||
|
||||
if (requestParam == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (requestParam instanceof Dict) {
|
||||
CONTEXT_HOLDER.set((Dict) requestParam);
|
||||
} else {
|
||||
CONTEXT_HOLDER.set(Dict.parse(requestParam));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求参数
|
||||
*
|
||||
* @author stylefeng
|
||||
* @date 2020/6/21 20:17
|
||||
*/
|
||||
public static Dict get() {
|
||||
return CONTEXT_HOLDER.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除请求参数
|
||||
*
|
||||
* @author stylefeng
|
||||
* @date 2020/6/21 20:17
|
||||
*/
|
||||
public static void clear() {
|
||||
CONTEXT_HOLDER.remove();
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
package cn.stylefeng.roses.kernel.validator.context;
|
||||
|
||||
/**
|
||||
* 临时保存参数id字段值,用于唯一性校验
|
||||
* <p>
|
||||
* 注意:如果要用@TableUniqueValue这个校验,必须得主键的字段名是id,否则会校验失败
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/11/4 14:34
|
||||
*/
|
||||
public class RequestParamIdContext {
|
||||
|
||||
private static final ThreadLocal<Long> PARAM_ID_HOLDER = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 设置临时缓存的id
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/11/4 14:35
|
||||
*/
|
||||
public static void set(Long id) {
|
||||
PARAM_ID_HOLDER.set(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取临时缓存的id
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/11/4 14:35
|
||||
*/
|
||||
public static Long get() {
|
||||
return PARAM_ID_HOLDER.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除临时缓存的id
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/11/4 14:35
|
||||
*/
|
||||
public static void clear() {
|
||||
PARAM_ID_HOLDER.remove();
|
||||
}
|
||||
|
||||
}
|
|
@ -33,6 +33,11 @@ public class UniqueValidateParam {
|
|||
*/
|
||||
Boolean excludeCurrentRecord;
|
||||
|
||||
/**
|
||||
* 主键id的字段名
|
||||
*/
|
||||
String idFieldName;
|
||||
|
||||
/**
|
||||
* 当前记录的主键id
|
||||
*/
|
||||
|
|
|
@ -38,6 +38,11 @@ public @interface TableUniqueValue {
|
|||
*/
|
||||
String columnName();
|
||||
|
||||
/**
|
||||
* 主键id的字段名,默认为字段名为:id
|
||||
*/
|
||||
String idFieldName() default "id";
|
||||
|
||||
/**
|
||||
* 是否开启逻辑删除校验,默认是关闭的
|
||||
* <p>
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
package cn.stylefeng.roses.kernel.validator.validators.unique;
|
||||
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
|
||||
import cn.stylefeng.roses.kernel.validator.validators.unique.service.TableUniqueValueService;
|
||||
import cn.stylefeng.roses.kernel.validator.context.RequestGroupContext;
|
||||
import cn.stylefeng.roses.kernel.validator.context.RequestParamIdContext;
|
||||
import cn.stylefeng.roses.kernel.validator.context.RequestParamContext;
|
||||
import cn.stylefeng.roses.kernel.validator.pojo.UniqueValidateParam;
|
||||
import cn.stylefeng.roses.kernel.validator.validators.unique.service.TableUniqueValueService;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
@ -28,6 +29,11 @@ public class TableUniqueValueValidator implements ConstraintValidator<TableUniqu
|
|||
*/
|
||||
private String columnName;
|
||||
|
||||
/**
|
||||
* id字段的名称
|
||||
*/
|
||||
private String idFieldName;
|
||||
|
||||
/**
|
||||
* 是否开启逻辑删除校验,默认是关闭的
|
||||
* <p>
|
||||
|
@ -54,6 +60,7 @@ public class TableUniqueValueValidator implements ConstraintValidator<TableUniqu
|
|||
this.excludeLogicDeleteItems = constraintAnnotation.excludeLogicDeleteItems();
|
||||
this.logicDeleteFieldName = constraintAnnotation.logicDeleteFieldName();
|
||||
this.logicDeleteValue = constraintAnnotation.logicDeleteValue();
|
||||
this.idFieldName = constraintAnnotation.idFieldName();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -65,18 +72,18 @@ public class TableUniqueValueValidator implements ConstraintValidator<TableUniqu
|
|||
|
||||
Class<?> validateGroupClass = RequestGroupContext.get();
|
||||
|
||||
// 如果属于add group,则校验库中所有行
|
||||
if (BaseRequest.add.class.equals(validateGroupClass)) {
|
||||
UniqueValidateParam addParam = createAddParam(fieldValue);
|
||||
return TableUniqueValueService.getFiledUniqueFlag(addParam);
|
||||
}
|
||||
|
||||
// 如果属于edit group,校验时需要排除当前修改的这条记录
|
||||
if (BaseRequest.edit.class.equals(validateGroupClass)) {
|
||||
UniqueValidateParam editParam = createEditParam(fieldValue);
|
||||
return TableUniqueValueService.getFiledUniqueFlag(editParam);
|
||||
}
|
||||
|
||||
// 如果属于add group,则校验库中所有行
|
||||
if (BaseRequest.add.class.equals(validateGroupClass)) {
|
||||
UniqueValidateParam addParam = createAddParam(fieldValue);
|
||||
return TableUniqueValueService.getFiledUniqueFlag(addParam);
|
||||
}
|
||||
|
||||
// 默认校验所有的行
|
||||
UniqueValidateParam addParam = createAddParam(fieldValue);
|
||||
return TableUniqueValueService.getFiledUniqueFlag(addParam);
|
||||
|
@ -106,12 +113,17 @@ public class TableUniqueValueValidator implements ConstraintValidator<TableUniqu
|
|||
* @date 2020/8/17 21:56
|
||||
*/
|
||||
private UniqueValidateParam createEditParam(String fieldValue) {
|
||||
|
||||
// 获取请求字段中id的值
|
||||
Dict requestParam = RequestParamContext.get();
|
||||
|
||||
return UniqueValidateParam.builder()
|
||||
.tableName(tableName)
|
||||
.columnName(columnName)
|
||||
.value(fieldValue)
|
||||
.idFieldName(idFieldName)
|
||||
.excludeCurrentRecord(Boolean.TRUE)
|
||||
.id(RequestParamIdContext.get())
|
||||
.id(requestParam.getLong(idFieldName))
|
||||
.excludeLogicDeleteItems(excludeLogicDeleteItems)
|
||||
.logicDeleteFieldName(logicDeleteFieldName)
|
||||
.logicDeleteValue(logicDeleteValue).build();
|
||||
|
|
|
@ -34,21 +34,22 @@ public class TableUniqueValueService {
|
|||
// 不排除当前记录,不排除逻辑删除的内容
|
||||
if (!uniqueValidateParam.getExcludeCurrentRecord()
|
||||
&& !uniqueValidateParam.getExcludeLogicDeleteItems()) {
|
||||
resultCount = dbOperatorApi.selectCount(
|
||||
"select count(*) from " + uniqueValidateParam.getTableName() + " where " + uniqueValidateParam.getColumnName() + " = {0}",
|
||||
uniqueValidateParam.getValue());
|
||||
String sqlTemplate = "select count(*) from {} where {} = {0}";
|
||||
String finalSql = StrUtil.format(sqlTemplate, uniqueValidateParam.getTableName(), uniqueValidateParam.getColumnName());
|
||||
resultCount = dbOperatorApi.selectCount(finalSql, uniqueValidateParam.getValue());
|
||||
}
|
||||
|
||||
// 不排除当前记录,排除逻辑删除的内容
|
||||
if (!uniqueValidateParam.getExcludeCurrentRecord()
|
||||
&& uniqueValidateParam.getExcludeLogicDeleteItems()) {
|
||||
resultCount = dbOperatorApi.selectCount(
|
||||
"select count(*) from " + uniqueValidateParam.getTableName()
|
||||
+ " where " + uniqueValidateParam.getColumnName() + " = {0} "
|
||||
+ " and "
|
||||
+ "(" + uniqueValidateParam.getLogicDeleteFieldName() + " is null || "
|
||||
+ uniqueValidateParam.getLogicDeleteFieldName() + " <> " + uniqueValidateParam.getLogicDeleteValue() + ")",
|
||||
uniqueValidateParam.getValue());
|
||||
String sqlTemplate = "select count(*) from {} where {} = {0} and ({} is null || {} <> {})";
|
||||
String finalSql = StrUtil.format(sqlTemplate,
|
||||
uniqueValidateParam.getTableName(),
|
||||
uniqueValidateParam.getColumnName(),
|
||||
uniqueValidateParam.getLogicDeleteFieldName(),
|
||||
uniqueValidateParam.getLogicDeleteFieldName(),
|
||||
uniqueValidateParam.getLogicDeleteValue());
|
||||
resultCount = dbOperatorApi.selectCount(finalSql, uniqueValidateParam.getValue());
|
||||
}
|
||||
|
||||
// 排除当前记录,不排除逻辑删除的内容
|
||||
|
@ -58,11 +59,9 @@ public class TableUniqueValueService {
|
|||
// id判空
|
||||
paramIdValidate(uniqueValidateParam);
|
||||
|
||||
resultCount = dbOperatorApi.selectCount(
|
||||
"select count(*) from " + uniqueValidateParam.getTableName()
|
||||
+ " where " + uniqueValidateParam.getColumnName() + " = {0} "
|
||||
+ " and id <> {1}",
|
||||
uniqueValidateParam.getValue(), uniqueValidateParam.getId());
|
||||
String sqlTemplate = "select count(*) from {} where {} = {0} and {} <> {1}";
|
||||
String finalSql = StrUtil.format(sqlTemplate, uniqueValidateParam.getTableName(), uniqueValidateParam.getColumnName(), uniqueValidateParam.getIdFieldName());
|
||||
resultCount = dbOperatorApi.selectCount(finalSql, uniqueValidateParam.getValue(), uniqueValidateParam.getId());
|
||||
}
|
||||
|
||||
// 排除当前记录,排除逻辑删除的内容
|
||||
|
@ -72,14 +71,15 @@ public class TableUniqueValueService {
|
|||
// id判空
|
||||
paramIdValidate(uniqueValidateParam);
|
||||
|
||||
resultCount = dbOperatorApi.selectCount(
|
||||
"select count(*) from " + uniqueValidateParam.getTableName()
|
||||
+ " where " + uniqueValidateParam.getColumnName() + " = {0} "
|
||||
+ " and id <> {1} "
|
||||
+ " and "
|
||||
+ "(" + uniqueValidateParam.getLogicDeleteFieldName() + " is null || "
|
||||
+ uniqueValidateParam.getLogicDeleteFieldName() + " <> " + uniqueValidateParam.getLogicDeleteValue() + ")",
|
||||
uniqueValidateParam.getValue(), uniqueValidateParam.getId());
|
||||
String sqlTemplate = "select count(*) from {} where {} = {0} and {} <> {1} and ({} is null || {} <> {})";
|
||||
String finalSql = StrUtil.format(sqlTemplate,
|
||||
uniqueValidateParam.getTableName(),
|
||||
uniqueValidateParam.getColumnName(),
|
||||
uniqueValidateParam.getIdFieldName(),
|
||||
uniqueValidateParam.getLogicDeleteFieldName(),
|
||||
uniqueValidateParam.getLogicDeleteFieldName(),
|
||||
uniqueValidateParam.getLogicDeleteValue());
|
||||
resultCount = dbOperatorApi.selectCount(finalSql, uniqueValidateParam.getValue(), uniqueValidateParam.getId());
|
||||
}
|
||||
|
||||
// 如果大于0,代表不是唯一的当前校验的值
|
||||
|
|
|
@ -22,7 +22,7 @@ import org.springframework.context.annotation.Configuration;
|
|||
* @date 2020/12/1 21:44
|
||||
*/
|
||||
@Configuration
|
||||
public class GunsValidatorAutoConfiguration {
|
||||
public class ValidatorAutoConfiguration {
|
||||
|
||||
/**
|
||||
* 黑名单校验
|
|
@ -0,0 +1,53 @@
|
|||
package cn.stylefeng.roses.kernel.validator.starter.resolver;
|
||||
|
||||
import cn.stylefeng.roses.kernel.validator.starter.web.GunsValidator;
|
||||
import cn.stylefeng.roses.kernel.validator.starter.web.ValidatorRequestResponseBodyMethodProcessor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 自定义的GunsRequestResponseBodyMethodProcessor,放在所有resolvers之前
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/8/21 21:09
|
||||
*/
|
||||
@Configuration
|
||||
public class MethodArgumentResolver {
|
||||
|
||||
@Resource
|
||||
private RequestMappingHandlerAdapter adapter;
|
||||
|
||||
/**
|
||||
* 自定义的spring参数校验器,重写主要为了保存一些在自定义validator中读不到的属性
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/8/12 20:18
|
||||
*/
|
||||
@Bean
|
||||
public GunsValidator gunsValidator() {
|
||||
return new GunsValidator();
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义的GunsRequestResponseBodyMethodProcessor,放在所有resolvers之前
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/12/16 18:34
|
||||
*/
|
||||
@PostConstruct
|
||||
public void injectSelfMethodArgumentResolver() {
|
||||
List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>();
|
||||
argumentResolvers.add(new ValidatorRequestResponseBodyMethodProcessor(adapter.getMessageConverters()));
|
||||
argumentResolvers.addAll(Objects.requireNonNull(adapter.getArgumentResolvers()));
|
||||
adapter.setArgumentResolvers(argumentResolvers);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package cn.stylefeng.roses.kernel.validator.starter.web;
|
||||
|
||||
import cn.stylefeng.roses.kernel.validator.context.RequestGroupContext;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
|
||||
/**
|
||||
* 用于真正校验参数之前缓存一下group的class类型
|
||||
* <p>
|
||||
* 因为ConstraintValidator的自定义校验中获取不到当前进行的group
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/8/12 20:07
|
||||
*/
|
||||
@Slf4j
|
||||
public class GunsValidator extends LocalValidatorFactoryBean {
|
||||
|
||||
@Override
|
||||
public void validate(Object target, Errors errors, Object... validationHints) {
|
||||
|
||||
try {
|
||||
if (validationHints.length > 0) {
|
||||
|
||||
// 如果是class类型,利用ThreadLocal缓存一下class类型
|
||||
if (validationHints[0] instanceof Class) {
|
||||
|
||||
// 临时保存group的class值
|
||||
RequestGroupContext.set((Class<?>) validationHints[0]);
|
||||
}
|
||||
}
|
||||
super.validate(target, errors, validationHints);
|
||||
} finally {
|
||||
RequestGroupContext.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package cn.stylefeng.roses.kernel.validator.starter.web;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.stylefeng.roses.kernel.validator.context.RequestParamContext;
|
||||
import cn.stylefeng.roses.kernel.validator.exception.ParamValidateException;
|
||||
import org.springframework.core.Conventions;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.ObjectError;
|
||||
import org.springframework.web.bind.WebDataBinder;
|
||||
import org.springframework.web.bind.support.WebDataBinderFactory;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static cn.stylefeng.roses.kernel.validator.exception.enums.ValidatorExceptionEnum.PARAM_VALIDATE_ERROR;
|
||||
|
||||
/**
|
||||
* 拓展原有RequestResponseBodyMethodProcessor,只为缓存临时参数
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/8/21 20:51
|
||||
*/
|
||||
public class ValidatorRequestResponseBodyMethodProcessor extends RequestResponseBodyMethodProcessor {
|
||||
|
||||
public ValidatorRequestResponseBodyMethodProcessor(List<HttpMessageConverter<?>> converters) {
|
||||
super(converters);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
|
||||
|
||||
parameter = parameter.nestedIfOptional();
|
||||
Object arg = readWithMessageConverters(webRequest, parameter, parameter.getNestedGenericParameterType());
|
||||
|
||||
// 临时缓存一下@RequestBody注解上的参数
|
||||
RequestParamContext.setObject(arg);
|
||||
|
||||
String name = Conventions.getVariableNameForParameter(parameter);
|
||||
|
||||
if (binderFactory != null) {
|
||||
WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);
|
||||
if (arg != null) {
|
||||
validateIfApplicable(binder, parameter);
|
||||
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
|
||||
List<ObjectError> allErrors = binder.getBindingResult().getAllErrors();
|
||||
StringBuilder errTips = new StringBuilder();
|
||||
int index = 1;
|
||||
for (ObjectError error : allErrors) {
|
||||
errTips.append(index++);
|
||||
errTips.append(".");
|
||||
errTips.append(error.getDefaultMessage());
|
||||
errTips.append(";");
|
||||
}
|
||||
String userTip = StrUtil.format(PARAM_VALIDATE_ERROR.getUserTip(), errTips.toString());
|
||||
throw new ParamValidateException(PARAM_VALIDATE_ERROR, userTip);
|
||||
}
|
||||
}
|
||||
if (mavContainer != null) {
|
||||
mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
|
||||
}
|
||||
}
|
||||
|
||||
return adaptArgumentIfNecessary(arg, parameter);
|
||||
}
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
cn.stylefeng.roses.kernel.validator.starter.GunsValidatorAutoConfiguration
|
||||
cn.stylefeng.roses.kernel.validator.starter.ValidatorAutoConfiguration,\
|
||||
cn.stylefeng.roses.kernel.validator.starter.resolver.MethodArgumentResolver
|
Loading…
Reference in New Issue