1
zj
2024-06-13 4680141b47cf95358b7edfb4000564ba0e973612
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.yami.trading.security.common.util;
 
import cn.hutool.core.util.StrUtil;
import com.yami.trading.bean.model.RiskClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
 
import java.time.Instant;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
 
@Slf4j
public class RiskClientUtil {
 
    private static Map<String, RiskClient> riskClientMap = new ConcurrentHashMap<>();
 
    private static AntPathMatcher ipMatcher = new AntPathMatcher();
 
    public static void initCache(Map<String, RiskClient> newRiskClientMap) {
        riskClientMap = newRiskClientMap;
    }
 
    public static Map<String, RiskClient> getRiskClientMap() {
        return Collections.unmodifiableMap(riskClientMap);
    }
 
    public static void saveRiskConfig(RiskClient config) {
        if (config == null) {
            return;
        }
 
        String cacheKey = config.getType() + ":" + config.getClientType() + ":" + config.getClientKey();
        riskClientMap.put(cacheKey, config);
    }
 
    public static void disableRiskConfig(String type, String clientType, String clientKey) {
        String cacheKey = type + ":" + clientType + ":" + clientKey;
        riskClientMap.remove(cacheKey);
    }
 
    /**
     * 基于指定用户ID值,提取特定风控类型的配置信息;
     * userId 参数必填,如果未设置 type 参数,则提取相关所有配置信息
     *
     * @param userCode
     * @param type
     * @return
     */
    public static List<RiskClient> getRiskInfoByUserCode(String userCode, String type) {
        if (StrUtil.isBlank(userCode)) {
            throw new RuntimeException("userCode参数必填");
        }
 
        List<RiskClient> riskList = new ArrayList<>();
        if (riskClientMap.isEmpty()) {
            return riskList;
        }
 
        if (StrUtil.isBlank(type)) {
            String fieldKey1 = "white:userCode:" + userCode.trim();
            String fieldKey2 = "black:userCode:" + userCode.trim();
            String fieldKey3 = "badnetwork:userCode:" + userCode.trim();
 
            RiskClient config1 = riskClientMap.get(fieldKey1);
            if (config1 != null) {
                riskList.add(config1);
            }
 
            RiskClient config2 = riskClientMap.get(fieldKey2);
            if (config2 != null) {
                riskList.add(config2);
            }
 
            RiskClient config3 = riskClientMap.get(fieldKey3);
            if (config3 != null) {
                riskList.add(config3);
            }
        } else {
            String fieldKey1 = type + ":userCode:" + userCode.trim();
            RiskClient config1 = riskClientMap.get(fieldKey1);
            if (config1 != null) {
                riskList.add(config1);
            }
        }
 
        long currentTimeMillis = Instant.now().atZone(ZoneId.of("Asia/Shanghai")).toInstant().toEpochMilli();
        List<RiskClient> validRiskList = new ArrayList<>();
        for (RiskClient oneRisk : riskList) {
            if (oneRisk.getStatus() != 1) {
                continue;
            }
            if (oneRisk.getBeginTimeTs().longValue() == 0 && oneRisk.getEndTimeTs().longValue() == 0) {
                // 时间不做限制
                validRiskList.add(oneRisk);
                continue;
            }
            if (oneRisk.getBeginTimeTs() > currentTimeMillis) {
                // 还没开始
                continue;
            }
            if (oneRisk.getEndTimeTs() > 0 && oneRisk.getEndTimeTs() < currentTimeMillis) {
                // 已经结束
                continue;
            }
 
            // 当前时间在起止时间中间
            validRiskList.add(oneRisk);
        }
 
        return validRiskList;
    }
 
    /**
     * 基于指定 IP 值,提取特定风控类型的配置信息;
     * ip 参数必填,如果未设置 type 参数,则提取相关所有配置信息
     *
     * @param clientIp
     * @param type
     * @return
     */
    public static List<RiskClient> getRiskInfoByIp(String clientIp, String type) {
        if (StrUtil.isBlank(clientIp)) {
            return new ArrayList<>();
            //throw new RuntimeException("clientIp参数必填");
        }
 
        List<RiskClient> riskList = new ArrayList<>();
        if (riskClientMap.isEmpty()) {
            return riskList;
        }
 
        List<String> keyList = new ArrayList<>();
        if (clientIp.contains("*")) {
            Set<String> cacheKeys = new HashSet<>(riskClientMap.keySet());
            Set<String> optionIps = new HashSet<>();
            for (String oneCacheKey : cacheKeys) {
                if (!oneCacheKey.contains(":ip:")) {
                    continue;
                }
 
                int idx = oneCacheKey.indexOf(":ip:");
                String ipPatternValue = oneCacheKey.substring(idx + ":ip:".length());
                if (ipMatcher.match(ipPatternValue, clientIp)) {
                    optionIps.add(ipPatternValue);
                    RiskClient curRiskClient = riskClientMap.get(oneCacheKey);
                    if (curRiskClient != null) {
                        riskList.add(curRiskClient);
                    }
                }
            }
        } else {
            if (StrUtil.isBlank(type)) {
                String fieldKey1 = "white:ip:" + clientIp.trim();
                String fieldKey2 = "black:ip:" + clientIp.trim();
                String fieldKey3 = "badnetwork:ip:" + clientIp.trim();
 
                RiskClient config1 = riskClientMap.get(fieldKey1);
                if (config1 != null) {
                    riskList.add(config1);
                }
 
                RiskClient config2 = riskClientMap.get(fieldKey2);
                if (config2 != null) {
                    riskList.add(config2);
                }
 
                RiskClient config3 = riskClientMap.get(fieldKey3);
                if (config3 != null) {
                    riskList.add(config3);
                }
            } else {
                String fieldKey1 = type + ":ip:" + clientIp.trim();
                RiskClient config1 = riskClientMap.get(fieldKey1);
                if (config1 != null) {
                    riskList.add(config1);
                }
            }
        }
 
        Date now = new Date(); // 注意时区问题
        List<RiskClient> validRiskList = new ArrayList<>();
        for (RiskClient oneRisk : riskList) {
            if (oneRisk.getStatus() != 1) {
                continue;
            }
            if (oneRisk.getBeginTimeTs().longValue() == 0 && oneRisk.getEndTimeTs().longValue() == 0) {
                // 时间不做限制
                validRiskList.add(oneRisk);
                continue;
            }
            if (oneRisk.getBeginTimeTs() > now.getTime()) {
                // 还没开始
                continue;
            }
            if (oneRisk.getEndTimeTs() > 0 && oneRisk.getEndTimeTs() < now.getTime()) {
                // 已经结束
                continue;
            }
 
            // 当前时间在起止时间中间
            validRiskList.add(oneRisk);
        }
 
        return validRiskList;
    }
 
}