Skip to content

Commit a62e54d

Browse files
committed
feat: 网关新增黑白名单支持
1 parent be78460 commit a62e54d

File tree

10 files changed

+202
-4
lines changed

10 files changed

+202
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.smartframework.cloud.examples.support.gateway.configure;
2+
3+
import org.smartframework.cloud.examples.support.gateway.properties.BlackWhiteListProperties;
4+
import org.springframework.cloud.context.config.annotation.RefreshScope;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
public class SmartGatewayConfiguration {
10+
11+
@Bean
12+
@RefreshScope
13+
public BlackWhiteListProperties blackWhiteListProperties() {
14+
return new BlackWhiteListProperties();
15+
}
16+
17+
}

application-module/support-module/support-service-gateway/src/main/java/org/smartframework/cloud/examples/support/gateway/constants/GatewayReturnCodes.java

+8
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,13 @@ public interface GatewayReturnCodes {
7373
* AES key获取失败
7474
*/
7575
String AES_KEY_NOT_FOUND = "400014";
76+
/**
77+
* 命中黑名单列表,禁止访问
78+
*/
79+
String BLACK_LIST_FORBIDDEN_ACCSS = "400015";
80+
/**
81+
* 不在白名单中,禁止访问
82+
*/
83+
String NOT_IN_WHITE_LIST = "400016";
7684

7785
}

application-module/support-module/support-service-gateway/src/main/java/org/smartframework/cloud/examples/support/gateway/constants/Order.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ public interface Order {
3232
*/
3333
int REQUEST_LOG = REWRITE_HTTP + 1;
3434

35+
/**
36+
* 黑白名单
37+
*/
38+
int BLACK_WHITE_LIST = REQUEST_LOG + 1;
39+
3540
/**
3641
* api access注解全局过滤器order
3742
*/
38-
int API_ACCESS = REQUEST_LOG + 1;
43+
int API_ACCESS = BLACK_WHITE_LIST + 1;
3944

4045
/**
4146
* api access注解全局过滤器order
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.smartframework.cloud.examples.support.gateway.exception;
2+
3+
import io.github.smart.cloud.exception.BaseException;
4+
import org.smartframework.cloud.examples.support.gateway.constants.GatewayReturnCodes;
5+
6+
/**
7+
* 黑名单异常
8+
*
9+
* @author collin
10+
* @date 2024-03-26
11+
*/
12+
public class BlackListException extends BaseException {
13+
14+
public BlackListException() {
15+
super(GatewayReturnCodes.BLACK_LIST_FORBIDDEN_ACCSS);
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.smartframework.cloud.examples.support.gateway.exception;
2+
3+
import io.github.smart.cloud.exception.BaseException;
4+
import org.smartframework.cloud.examples.support.gateway.constants.GatewayReturnCodes;
5+
6+
/**
7+
* 白名单异常
8+
*
9+
* @author collin
10+
* @date 2024-03-26
11+
*/
12+
public class WhiteListException extends BaseException {
13+
14+
public WhiteListException() {
15+
super(GatewayReturnCodes.NOT_IN_WHITE_LIST);
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.smartframework.cloud.examples.support.gateway.filter.access;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.apache.commons.collections4.CollectionUtils;
6+
import org.smartframework.cloud.examples.support.gateway.constants.Order;
7+
import org.smartframework.cloud.examples.support.gateway.exception.BlackListException;
8+
import org.smartframework.cloud.examples.support.gateway.exception.WhiteListException;
9+
import org.smartframework.cloud.examples.support.gateway.properties.BlackWhiteListProperties;
10+
import org.springframework.core.Ordered;
11+
import org.springframework.stereotype.Component;
12+
import org.springframework.web.server.ServerWebExchange;
13+
import org.springframework.web.server.WebFilter;
14+
import org.springframework.web.server.WebFilterChain;
15+
import reactor.core.publisher.Mono;
16+
17+
import java.util.Map;
18+
import java.util.Set;
19+
20+
/**
21+
* 黑、白名单校验
22+
*
23+
* @author collin
24+
* @date 2024-03-26
25+
*/
26+
@Slf4j
27+
@Component
28+
@RequiredArgsConstructor
29+
public class BlackWhiteListFilter implements WebFilter, Ordered {
30+
31+
private final BlackWhiteListProperties blackWhiteListProperties;
32+
33+
@Override
34+
public int getOrder() {
35+
return Order.BLACK_WHITE_LIST;
36+
}
37+
38+
@Override
39+
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
40+
String url = exchange.getRequest().getURI().getPath();
41+
String ipAddress = exchange.getRequest().getRemoteAddress().getAddress().getHostAddress();
42+
43+
checkBlackList(url, ipAddress, blackWhiteListProperties.getBlackList());
44+
checkWhiteList(url, ipAddress, blackWhiteListProperties.getWhiteList());
45+
46+
return chain.filter(exchange);
47+
}
48+
49+
/**
50+
* 检查黑名单
51+
*
52+
* @param url
53+
* @param ipAddress
54+
* @param blackList
55+
*/
56+
private void checkBlackList(String url, String ipAddress, Map<String, Set<String>> blackList) {
57+
Set<String> blackIps = blackList.get(url);
58+
if (CollectionUtils.isEmpty(blackIps)) {
59+
return;
60+
}
61+
62+
for (String blackIp : blackIps) {
63+
if (ipAddress.startsWith(blackIp)) {
64+
throw new BlackListException();
65+
}
66+
}
67+
}
68+
69+
/**
70+
* 检查白名单
71+
*
72+
* @param url
73+
* @param ipAddress
74+
* @param whiteList
75+
*/
76+
private void checkWhiteList(String url, String ipAddress, Map<String, Set<String>> whiteList) {
77+
Set<String> whiteIps = whiteList.get(url);
78+
if (CollectionUtils.isEmpty(whiteIps)) {
79+
return;
80+
}
81+
82+
boolean meetWhiteList = false;
83+
for (String whiteIp : whiteIps) {
84+
if (ipAddress.startsWith(whiteIp)) {
85+
meetWhiteList = true;
86+
break;
87+
}
88+
}
89+
90+
if (!meetWhiteList) {
91+
throw new WhiteListException();
92+
}
93+
}
94+
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.smartframework.cloud.examples.support.gateway.properties;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import lombok.ToString;
6+
7+
import java.util.LinkedHashMap;
8+
import java.util.Map;
9+
import java.util.Set;
10+
11+
/**
12+
* 黑白名单配置
13+
*
14+
* @author collin
15+
* @date 2024-03-27
16+
*/
17+
@Getter
18+
@Setter
19+
@ToString
20+
public class BlackWhiteListProperties {
21+
22+
/**
23+
* 黑名单<url, 黑名单集合>
24+
*/
25+
private Map<String, Set<String>> blackList = new LinkedHashMap<>();
26+
/**
27+
* 白名单<url, 白名单集合>
28+
*/
29+
private Map<String, Set<String>> whiteList = new LinkedHashMap<>();
30+
31+
}

application-module/support-module/support-service-gateway/src/main/resources/i18n/gateway_messages.properties

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@
1111
400011=\u8BF7\u6C42\u65F6\u95F4\u6233\u683C\u5F0F\u9519\u8BEF\uFF01
1212
400012=\u8BF7\u6C42\u65F6\u95F4\u6233\u975E\u6CD5\uFF01
1313
400013=security key\u8FC7\u671F\uFF01
14-
400014=AES key\u83B7\u53D6\u5931\u8D25\uFF01
14+
400014=AES key\u83B7\u53D6\u5931\u8D25\uFF01
15+
400015=\u547D\u4E2D\u9ED1\u540D\u5355\u5217\u8868\uFF0C\u7981\u6B62\u8BBF\u95EE
16+
400016=\u4E0D\u5728\u767D\u540D\u5355\u4E2D\uFF0C\u7981\u6B62\u8BBF\u95EE

application-module/support-module/support-service-gateway/src/main/resources/i18n/gateway_messages_en_US.properties

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@
1111
400011=Request timestamp format error!
1212
400012=Illegal request timestamp!
1313
400013=Security key expired!
14-
400014=AES key is not found!
14+
400014=AES key is not found!
15+
400015=Matches the blacklist list, and the access is prohibited
16+
400016=The access is not in the whitelist

application-module/support-module/support-service-gateway/src/main/resources/i18n/gateway_messages_zh_CN.properties

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@
1111
400011=\u8BF7\u6C42\u65F6\u95F4\u6233\u683C\u5F0F\u9519\u8BEF\uFF01
1212
400012=\u8BF7\u6C42\u65F6\u95F4\u6233\u975E\u6CD5\uFF01
1313
400013=security key\u8FC7\u671F\uFF01
14-
400014=AES key\u83B7\u53D6\u5931\u8D25\uFF01
14+
400014=AES key\u83B7\u53D6\u5931\u8D25\uFF01
15+
400015=\u547D\u4E2D\u9ED1\u540D\u5355\u5217\u8868\uFF0C\u7981\u6B62\u8BBF\u95EE
16+
400016=\u4E0D\u5728\u767D\u540D\u5355\u4E2D\uFF0C\u7981\u6B62\u8BBF\u95EE

0 commit comments

Comments
 (0)