@Component
public class AuthGlobalFilterFilter implements GlobalFilter, Ordered {
private static final String HEADER_KEY= "KEY";
private static final String HEADER_LKEY = "LKEY";
@Autowired
ILoginInfoService loginInfoServiceImpl;
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
if(不满足一定条件){
ServerHttpResponse serverHttpResponse = exchange.getResponse(); serverHttpResponse.setStatusCode(HttpStatus.UNAUTHORIZED); byte[] bytes = "{\"status\":\"-1\",\"msg\":\"error\"}".getBytes(StandardCharsets.UTF_8); DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes); return exchange.getResponse().writeWith(Flux.just(buffer));
}
参考:https://stackoverflow.com/questions/48491098/how-to-add-some-data-in-body-of-response-for-cloud-api-gateway