diff --git a/application/src/main/java/run/halo/app/infra/config/WebFluxConfig.java b/application/src/main/java/run/halo/app/infra/config/WebFluxConfig.java index 4acfecc70..6e75e4f06 100644 --- a/application/src/main/java/run/halo/app/infra/config/WebFluxConfig.java +++ b/application/src/main/java/run/halo/app/infra/config/WebFluxConfig.java @@ -14,6 +14,7 @@ import org.springframework.boot.autoconfigure.web.reactive.WebFluxRegistrations; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.http.CacheControl; import org.springframework.http.MediaType; @@ -218,6 +219,7 @@ public class WebFluxConfig implements WebFluxConfigurer { @ConditionalOnProperty(name = "halo.console.proxy.enabled", havingValue = "true") @Bean + @Order(Ordered.HIGHEST_PRECEDENCE) ProxyFilter consoleProxyFilter() { return new ProxyFilter("/console/**", haloProp.getConsole().getProxy()); } @@ -225,6 +227,7 @@ public class WebFluxConfig implements WebFluxConfigurer { @ConditionalOnProperty(name = "halo.uc.proxy.enabled", havingValue = "true") @Bean + @Order(Ordered.HIGHEST_PRECEDENCE) ProxyFilter ucProxyFilter() { return new ProxyFilter("/uc/**", haloProp.getUc().getProxy()); } diff --git a/application/src/main/java/run/halo/app/infra/console/ProxyFilter.java b/application/src/main/java/run/halo/app/infra/console/ProxyFilter.java index 707be01b1..9554c0720 100644 --- a/application/src/main/java/run/halo/app/infra/console/ProxyFilter.java +++ b/application/src/main/java/run/halo/app/infra/console/ProxyFilter.java @@ -3,6 +3,7 @@ package run.halo.app.infra.console; import lombok.extern.slf4j.Slf4j; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; import org.springframework.security.web.server.util.matcher.AndServerWebExchangeMatcher; import org.springframework.security.web.server.util.matcher.NegatedServerWebExchangeMatcher; import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher; @@ -58,14 +59,22 @@ public class ProxyFilter implements WebFilter { .headers(httpHeaders -> httpHeaders.addAll(exchange.getRequest().getHeaders())) .exchangeToMono(clientResponse -> { var response = exchange.getResponse(); + var httpStatusCode = clientResponse.statusCode(); // set headers - response.getHeaders().putAll(clientResponse.headers().asHttpHeaders()); + var httpHeaders = clientResponse.headers().asHttpHeaders(); + response.getHeaders().putAll(httpHeaders); // set cookies response.getCookies().putAll(clientResponse.cookies()); // set status code - response.setStatusCode(clientResponse.statusCode()); + response.setStatusCode(httpStatusCode); + var contentLength = clientResponse.headers().contentLength().orElse(0L); + if (httpStatusCode.is3xxRedirection() + || httpStatusCode.equals(HttpStatus.NO_CONTENT) + || contentLength == 0) { + return Mono.empty(); + } var body = clientResponse.bodyToFlux(DataBuffer.class); - return exchange.getResponse().writeWith(body); + return response.writeWith(body); })); } }