728x90
사용자 정의 필터를 등록할 수 있다.
JWT와 같은 해당 토큰이 존재하는지를 체크하여 보안을 적용할 수도 있다.
CustomFilter
@Component
@Slf4j
public class CustomFilter extends AbstractGatewayFilterFactory<CustomFilter.Config> { // 해당 Factory를 구현해야한다.
public CustomFilter(){
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return ((exchange, chain) ->{
//Custom Pre Filter
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
log.info("Custom PRE Filter : request id -> {}", request.getId());
//Custom Post Filter
return chain.filter(exchange).then(Mono.fromRunnable(()->{ //비동기 방식을 단일값을 리턴할떄 쓴다(MONO)
log.info("Custom POST filter: response code -> {} ", response.getStatusCode());
}));
});
}
public static class Config{
//Configure 정보를 입력한다.
}
}
- @Component를 통해 Spring Bean으로 등록한다.
- apply 메소드를 재정의 하여 어떠한 동작을 할 것인지 작성해 주면 된다.
- 비동기 방식을 서버를 사용하므로 ServerHttpRequest, ServerHttpResponse를 사용하여야 한다.
- 동기 방식을 톰켓에서 사용하는 ServletHttpRequest, ServletResponse를 사용하지 않는다.
- AbstractGatewayFilterFactory를 상속받아 <커스텀 필터, 설정>을 넣어주어야 한다.
application.yml
yml에 파일에 작성한 CustomFilter를 넣어준다.

FirstService , SecondService 확인 Test
Check 메서드를 추가하여 확인한다.

728x90
'Spring Cloud로 개발하는 MSA > API Gateway Service' 카테고리의 다른 글
| Spring Cloud Gateway - Logging Filter (0) | 2022.05.03 |
|---|---|
| Spring Cloud Gateway - Global Filter (0) | 2022.05.03 |
| Spring Cloud Gateway - Filter 적용 (0) | 2022.05.03 |
| Spring Cloud Gateway - 프로젝트 생성 (0) | 2022.05.03 |
| Netflix Zuul - Filter 적용 (0) | 2022.05.03 |