Spring Cloud Gateway 如何定义pre filter 和 post filter?

背景:
自定义两个filter修改请求的body和返回的body,如何区分哪个filter只拦截请求,哪个只拦截返回

对于Filter来说,没得前置,后置的分别。取决于你的代码。。

	import org.springframework.cloud.gateway.filter.GatewayFilter;
	import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
	import org.springframework.http.server.reactive.ServerHttpRequest;
	import org.springframework.stereotype.Component;

	@Component
	public class DemoFilter extends AbstractGatewayFilterFactory<DemoFilter.Config> {

		public DemoFilter(){
			// 通过构造函数设置配置类
			super(Config.class);
		}

		@Override
		public GatewayFilter apply(DemoFilter.Config config) {
			return (exchange, chain) -> {

				// 前置执行
				ServerHttpRequest request = exchange.getRequest().mutate().headers(httpHeaders -> {
					httpHeaders.set(config.getHeaderName(), config.getHeaderValue());
				}).build();

				return chain.filter(exchange.mutate().request(request).build());
				//  return chain.filter(exchange.mutate().request(request).build()).then(Mono.fromRunnable(() -> {
				   // 后置执行
				// }));
			};
		}

		// Filter名字,如果命名规范的情况下,可以省略不写
		@Override
		public String name() {
			return "Demo";
		}

		/**
		 * 配置类,属性可以通过配置args注入
		 */
		public static class Config {
			private String headerName;
			private String headerValue;
			public String getHeaderName() {
				return headerName;
			}

			public void setHeaderName(String headerName) {
				this.headerName = headerName;
			}

			public String getHeaderValue() {
				return headerValue;
			}

			public void setHeaderValue(String headerValue) {
				this.headerValue = headerValue;
			}
		}
	}