Skip to content

Commit 029a96f

Browse files
committed
Feat: 필터 동작 순서 수정
1 parent 926a2a0 commit 029a96f

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

src/main/java/flipnote/apigateway/filter/AuthenticationFilter.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import flipnote.apigateway.client.TokenValidationClient;
44
import lombok.extern.slf4j.Slf4j;
55
import org.springframework.cloud.gateway.filter.GatewayFilter;
6-
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
76
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
8-
import org.springframework.core.Ordered;
97
import org.springframework.http.HttpCookie;
108
import org.springframework.http.HttpStatus;
119
import org.springframework.stereotype.Component;
@@ -27,7 +25,7 @@ public AuthenticationFilter(TokenValidationClient tokenValidationClient) {
2725

2826
@Override
2927
public GatewayFilter apply(Config config) {
30-
GatewayFilter filter = (exchange, chain) -> {
28+
return (exchange, chain) -> {
3129
HttpCookie cookie = exchange.getRequest().getCookies().getFirst(ACCESS_TOKEN_COOKIE);
3230

3331
if (cookie == null) {
@@ -56,7 +54,6 @@ public GatewayFilter apply(Config config) {
5654
return onError(exchange, HttpStatus.UNAUTHORIZED);
5755
});
5856
};
59-
return new OrderedGatewayFilter(filter, Ordered.LOWEST_PRECEDENCE);
6057
}
6158

6259
private Mono<Void> onError(ServerWebExchange exchange, HttpStatus status) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package flipnote.apigateway.filter;
2+
3+
import java.util.List;
4+
5+
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
6+
import org.springframework.cloud.gateway.filter.GlobalFilter;
7+
import org.springframework.core.Ordered;
8+
import org.springframework.http.server.reactive.ServerHttpRequest;
9+
import org.springframework.stereotype.Component;
10+
import org.springframework.web.server.ServerWebExchange;
11+
12+
import reactor.core.publisher.Mono;
13+
14+
@Component
15+
public class HeaderCleanupGlobalFilter implements GlobalFilter, Ordered {
16+
17+
private static final List<String> HEADERS_TO_REMOVE = List.of(
18+
"X-User-Id",
19+
"X-User-Email",
20+
"X-User-Role"
21+
);
22+
23+
@Override
24+
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
25+
ServerHttpRequest request = exchange.getRequest().mutate()
26+
.headers(headers -> HEADERS_TO_REMOVE.forEach(headers::remove))
27+
.build();
28+
29+
return chain.filter(exchange.mutate().request(request).build());
30+
}
31+
32+
@Override
33+
public int getOrder() {
34+
return Ordered.HIGHEST_PRECEDENCE;
35+
}
36+
}

src/main/resources/application.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ spring:
77

88
cloud:
99
gateway:
10-
default-filters:
11-
- RemoveRequestHeader=X-User-Id
12-
- RemoveRequestHeader=X-User-Email
13-
- RemoveRequestHeader=X-User-Role
1410
routes:
1511
- id: user-private
1612
uri: http://user-service:8081

0 commit comments

Comments
 (0)