浏览代码

【升级】完善TableUniqueValueValidator,可自动识别add或edit进行相关的校验

fengshuonan 4 年之前
父节点
当前提交
99a2bf6049

+ 67 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/context/group/RequestGroupContext.java

@ -0,0 +1,67 @@
/*
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.
Guns采用APACHE 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.guns.core.context.group;
/**
 * 保存控制器的方法上的校验group class
 *
 * @author stylefeng
 * @date 2020/6/21 20:17
 */
public class RequestGroupContext {
    private static final ThreadLocal<Class<?>> GROUP_CLASS_HOLDER = new ThreadLocal<>();
    /**
     * 设置临时的group class
     *
     * @author stylefeng
     * @date 2020/6/21 20:17
     */
    public static void set(Class<?> groupValue) {
        GROUP_CLASS_HOLDER.set(groupValue);
    }
    /**
     * 获取临时缓存的group class
     *
     * @author stylefeng
     * @date 2020/6/21 20:17
     */
    public static Class<?> get() {
        return GROUP_CLASS_HOLDER.get();
    }
    /**
     * 清除临时缓存的group class
     *
     * @author stylefeng
     * @date 2020/6/21 20:17
     */
    public static void clear() {
        GROUP_CLASS_HOLDER.remove();
    }
}

+ 67 - 0
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/context/group/RequestParamIdContext.java

@ -0,0 +1,67 @@
/*
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.
Guns采用APACHE 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.guns.core.context.group;
/**
 * 临时保存参数id字段值,用于唯一性校验
 *
 * @author stylefeng
 * @date 2020/6/21 20:17
 */
public class RequestParamIdContext {
    private static final ThreadLocal<Long> PARAM_ID_HOLDER = new ThreadLocal<>();
    /**
     * 设置id
     *
     * @author stylefeng
     * @date 2020/6/21 20:17
     */
    public static void set(Long id) {
        PARAM_ID_HOLDER.set(id);
    }
    /**
     * 获取id
     *
     * @author stylefeng
     * @date 2020/6/21 20:17
     */
    public static Long get() {
        return PARAM_ID_HOLDER.get();
    }
    /**
     * 清除缓存id
     *
     * @author stylefeng
     * @date 2020/6/21 20:17
     */
    public static void clear() {
        PARAM_ID_HOLDER.remove();
    }
}

+ 11 - 4
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/context/system/SystemContext.java

@ -121,12 +121,19 @@ public interface SystemContext {
     * <p>
     * 一般用于唯一code校验
     *
     * @param tableName 表名
     * @param columnName 列名
     * @param value 值
     * @param tableName            表名
     * @param columnName           列名
     * @param value                值
     * @param excludeCurrentRecord 排除当前的值
     * @param id                   id字段的值
     * @return true-是唯一的值,false-不是唯一的
     * @author fengshuonan
     * @date 2020/8/9 21:41
     */
    boolean tableUniValueFlag(String tableName, String columnName, String value);
    boolean tableUniValueFlag(String tableName,
                              String columnName,
                              String value,
                              Boolean excludeCurrentRecord,
                              Long id);
}

+ 17 - 1
guns-base-support/guns-core/src/main/java/cn/stylefeng/guns/core/validation/unique/TableUniqueValueValidator.java

@ -24,7 +24,10 @@ Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意
 */
package cn.stylefeng.guns.core.validation.unique;
import cn.stylefeng.guns.core.context.group.RequestGroupContext;
import cn.stylefeng.guns.core.context.group.RequestParamIdContext;
import cn.stylefeng.guns.core.context.system.SystemContextHolder;
import cn.stylefeng.guns.core.pojo.base.param.BaseParam;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
@ -49,6 +52,19 @@ public class TableUniqueValueValidator implements ConstraintValidator<TableUniqu
    @Override
    public boolean isValid(String fieldValue, ConstraintValidatorContext context) {
        return SystemContextHolder.me().tableUniValueFlag(tableName, columnName, fieldValue);
        Class<?> validateGroupClass = RequestGroupContext.get();
        // 如果属于add group,则校验库中所有行
        if (BaseParam.add.class.equals(validateGroupClass)) {
            return SystemContextHolder.me().tableUniValueFlag(tableName, columnName, fieldValue, Boolean.FALSE, null);
        }
        // 如果属于edit group,校验时需要排除当前修改的这条记录
        if (BaseParam.edit.class.equals(validateGroupClass)) {
            return SystemContextHolder.me().tableUniValueFlag(tableName, columnName, fieldValue, Boolean.TRUE, RequestParamIdContext.get());
        }
        // 默认校验所有的行
        return SystemContextHolder.me().tableUniValueFlag(tableName, columnName, fieldValue, Boolean.FALSE, null);
    }
}

+ 12 - 0
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/config/WebMvcConfig.java

@ -27,6 +27,7 @@ package cn.stylefeng.guns.sys.config;
import cn.stylefeng.guns.sys.core.error.GunsErrorAttributes;
import cn.stylefeng.guns.sys.core.filter.RequestNoFilter;
import cn.stylefeng.guns.sys.core.filter.xss.XssFilter;
import cn.stylefeng.guns.sys.core.validator.GunsValidator;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
@ -110,4 +111,15 @@ public class WebMvcConfig implements WebMvcConfigurer {
                        .serializerByType(Long.TYPE, ToStringSerializer.instance);
    }
    /**
     * 自定义的spring参数校验器,重写主要为了保存一些在自定义validator中读不到的属性
     *
     * @author fengshuonan
     * @date 2020/8/12 20:18
     */
    @Bean
    public GunsValidator gunsValidator() {
        return new GunsValidator();
    }
}

+ 19 - 7
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/core/context/SystemContextImpl.java

@ -42,7 +42,6 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
 * 系统相关上下文接口实现类
@ -118,14 +117,27 @@ public class SystemContextImpl implements SystemContext {
    }
    @Override
    public boolean tableUniValueFlag(String tableName, String columnName, String value) {
        List<Map<String, Object>> maps = SqlRunner.db().selectList(
                "select " + columnName + " from " + tableName + " where " + columnName + " = {0}", value);
        if (maps != null && maps.size() > 0) {
            return false;
    public boolean tableUniValueFlag(String tableName,
                                     String columnName,
                                     String value,
                                     Boolean excludeCurrentRecord,
                                     Long id) {
        int resultCount;
        // 如果排除当前这条记录
        if (excludeCurrentRecord) {
            if (id == null) {
                throw new IllegalArgumentException("当前table字段值唯一性校验失败,id为null");
            }
            resultCount = SqlRunner.db().selectCount(
                    "select count(*) from " + tableName + " where " + columnName + " = {0} and id <> {1}", value, id);
        } else {
            return true;
            resultCount = SqlRunner.db().selectCount(
                    "select count(*) from " + tableName + " where " + columnName + " = {0}", value);
        }
        // 如果大于0,代表不是唯一的当前校验的值
        return resultCount <= 0;
    }
}

+ 93 - 0
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/core/validator/GunsValidator.java

@ -0,0 +1,93 @@
/*
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.
Guns采用APACHE 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.guns.sys.core.validator;
import cn.stylefeng.guns.core.context.group.RequestGroupContext;
import cn.stylefeng.guns.core.context.group.RequestParamIdContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.Errors;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import java.beans.PropertyDescriptor;
import static cn.stylefeng.guns.core.consts.CommonConstant.ID;
/**
 * 用于真正校验参数之前缓存一下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]);
                    // 临时保存字段为id的值
                    RequestParamIdContext.set(getParamIdValue(target));
                }
            }
            super.validate(target, errors, validationHints);
        } finally {
            RequestGroupContext.clear();
        }
    }
    /**
     * 获取参数中的id的值,如果没有id字段就返回null
     *
     * @author fengshuonan
     * @date 2020/8/12 21:24
     */
    private Long getParamIdValue(Object target) {
        try {
            PropertyDescriptor prop = new PropertyDescriptor(ID, target.getClass());
            Object paramId = prop.getReadMethod().invoke(target);
            if (paramId != null) {
                if (paramId instanceof Long) {
                    RequestParamIdContext.set((Long) paramId);
                }
            }
        } catch (Exception e) {
            log.error("获取参数的id值时候出错!", e);
        }
        return null;
    }
}