Skip to content

Commit 10e2b24

Browse files
authored
feat: add whitelist filters and related resources (#22)
2 parents 4da95f7 + a6c345e commit 10e2b24

File tree

5 files changed

+110
-7
lines changed

5 files changed

+110
-7
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.achobeta.www.gateway.config;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import org.springframework.boot.context.properties.ConfigurationProperties;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.util.List;
9+
10+
/**
11+
* <span>
12+
* Whitelist request URL list
13+
* </span>
14+
*
15+
* @author jettcc in 2023/10/6
16+
* @version 1.0
17+
*/
18+
19+
@Getter
20+
@Setter
21+
@Component
22+
@ConfigurationProperties(prefix = "gateway.whitelist")
23+
public class WhitelistProperties {
24+
List<String> urls;
25+
}

achobeta-infra-gateway/src/main/java/com/achobeta/www/gateway/filter/GlobalRequestInterceptor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.text.ParseException;
1515

1616
import static com.achobeta.www.common.util.GlobalServiceStatusCode.*;
17+
import static com.achobeta.www.gateway.utils.GatewayConstants.AUTHORIZATION_HEADER;
18+
import static com.achobeta.www.gateway.utils.GatewayConstants.BEARER_PREFIX;
1719

1820
/**
1921
* <span>
@@ -29,8 +31,7 @@
2931
*/
3032
@Component
3133
public class GlobalRequestInterceptor implements GlobalFilter {
32-
private static final String AUTHORIZATION_HEADER = "Authorization";
33-
private static final String BEARER_PREFIX = "Bearer ";
34+
3435

3536
@Override
3637
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.achobeta.www.gateway.filter;
2+
3+
import com.achobeta.www.gateway.config.WhitelistProperties;
4+
import lombok.NonNull;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.util.AntPathMatcher;
8+
import org.springframework.web.server.ServerWebExchange;
9+
import org.springframework.web.server.WebFilter;
10+
import org.springframework.web.server.WebFilterChain;
11+
import reactor.core.publisher.Mono;
12+
13+
import static com.achobeta.www.gateway.utils.GatewayConstants.AUTHORIZATION_HEADER;
14+
import static com.achobeta.www.gateway.utils.GatewayConstants.REQUEST_TYPE_GET;
15+
/**
16+
* <span>
17+
* Whitelist filter
18+
* </span>
19+
*
20+
* @author jettcc in 2023/10/6
21+
* @version 1.0
22+
*/
23+
@Configuration
24+
@Slf4j
25+
public class WhitelistRequestFilter implements WebFilter {
26+
private final WhitelistProperties whitelist;
27+
28+
public WhitelistRequestFilter(WhitelistProperties whitelist) {
29+
this.whitelist = whitelist;
30+
}
31+
32+
@NonNull
33+
@Override
34+
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
35+
var request = exchange.getRequest();
36+
var path = request.getURI().getPath();
37+
var urls = whitelist.getUrls();
38+
var requestMethod = request.getMethod().name();
39+
var pathMatcher = new AntPathMatcher();
40+
// whitelist that only allows GET requests
41+
if (!REQUEST_TYPE_GET.equalsIgnoreCase(requestMethod)) return chain.filter(exchange);
42+
for (var u : urls) {
43+
if (pathMatcher.match(u, path)) {
44+
request = exchange.getRequest()
45+
.mutate().header(AUTHORIZATION_HEADER, "")
46+
.build();
47+
return chain.filter(exchange.mutate().request(request).build());
48+
}
49+
}
50+
51+
return chain.filter(exchange);
52+
}
53+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.achobeta.www.gateway.utils;
2+
/**
3+
* <span>
4+
* Define and store gateway constants
5+
* </span>
6+
*
7+
* @author jettcc in 2023/10/6
8+
* @version 1.0
9+
*/
10+
11+
public class GatewayConstants {
12+
public static final String AUTHORIZATION_HEADER = "Authorization";
13+
public static final String BEARER_PREFIX = "Bearer ";
14+
15+
public static final String REQUEST_TYPE_GET = "GET";
16+
17+
public static final String REQUEST_TYPE_POST = "POST";
18+
}

achobeta-infra-gateway/src/main/resources/application.yaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ spring:
1010
application:
1111
name: achobeta-infra-gateway
1212

13-
14-
15-
16-
17-
13+
# Whitelist
14+
gateway:
15+
whitelist:
16+
urls:
17+
- "/v0/**"
18+
- "/auth/oauth/token"
19+
- "/swagger**/**"
20+
- "/webjars/**"
21+
- "/doc.html"
22+
- "/*/*/api-docs"
1823

1924

2025
# ---------------------------- Try not to modify the following configurations ----------------------------
@@ -47,3 +52,4 @@ logging:
4752
achobeta:
4853
www:
4954
gateway: debug
55+

0 commit comments

Comments
 (0)