inleft
2022-02-27 2782f3e576b56a9ca078055026394646d57440f8
commit | author | age
9bcb19 1 /*
I 2 Copyright [2020] [https://www.xiaonuo.vip]
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
17
18 1.请不要删除和修改根目录下的LICENSE文件。
19 2.请不要删除和修改Snowy源码头部的版权声明。
20 3.请保留源码和相关描述文件的项目出处,作者声明等。
21 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
22 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
23 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
24  */
25
26 /*
27 Copyright [2020] [https://www.xiaonuo.vip]
28
29 Licensed under the Apache License, Version 2.0 (the "License");
30 you may not use this file except in compliance with the License.
31 You may obtain a copy of the License at
32
33   http://www.apache.org/licenses/LICENSE-2.0
34
35 Unless required by applicable law or agreed to in writing, software
36 distributed under the License is distributed on an "AS IS" BASIS,
37 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38 See the License for the specific language governing permissions and
39 limitations under the License.
40
41 Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
42
43 1.请不要删除和修改根目录下的LICENSE文件。
44 2.请不要删除和修改Snowy源码头部的版权声明。
45 3.请保留源码和相关描述文件的项目出处,作者声明等。
46 4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy
47 5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy
48 6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip
49  */
50 package vip.xiaonuo.sys.core.validator;
51
52 import cn.hutool.log.Log;
53 import org.springframework.validation.Errors;
54 import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
55 import vip.xiaonuo.core.consts.CommonConstant;
56 import vip.xiaonuo.core.context.group.RequestGroupContext;
57 import vip.xiaonuo.core.context.group.RequestParamIdContext;
58
59 import java.beans.PropertyDescriptor;
60
61 /**
62  * 用于真正校验参数之前缓存一下group的class类型
63  * <p>
64  * 因为ConstraintValidator的自定义校验中获取不到当前进行的group
65  *
66  * @author xuyuxiang
67  * @date 2020/8/12 20:07
68  */
69 public class SnowyValidator extends LocalValidatorFactoryBean {
70
71     private static final Log log = Log.get();
72
73     @Override
74     public void validate(Object target, Errors errors, Object... validationHints) {
75
76         try {
77             if (validationHints.length > 0) {
78
79                 // 如果是class类型,利用ThreadLocal缓存一下class类型
80                 if (validationHints[0] instanceof Class) {
81
82                     // 临时保存group的class值
83                     RequestGroupContext.set((Class<?>) validationHints[0]);
84
85                     // 临时保存字段为id的值
86                     RequestParamIdContext.set(getParamIdValue(target));
87                 }
88             }
89             super.validate(target, errors, validationHints);
90         } finally {
91             RequestGroupContext.clear();
92             RequestParamIdContext.clear();
93         }
94     }
95
96     /**
97      * 获取参数中的id的值,如果没有id字段就返回null
98      *
99      * @author xuyuxiang
100      * @date 2020/8/12 21:24
101      */
102     private Long getParamIdValue(Object target) {
103
104         try {
105             PropertyDescriptor prop = new PropertyDescriptor(CommonConstant.ID, target.getClass());
106             Object paramId = prop.getReadMethod().invoke(target);
107             if (paramId != null) {
108                 if (paramId instanceof Long) {
109                     return (Long) paramId;
110                 }
111             }
112         } catch (Exception e) {
113             log.error(">>> 获取参数的id值时候出错:{}", e.getMessage());
114         }
115
116         return null;
117     }
118 }