From ddbbe09c2d940f62aba4fd19bf22b883a8a6fb6f Mon Sep 17 00:00:00 2001 From: John Niang Date: Mon, 10 Mar 2025 16:19:01 +0800 Subject: [PATCH] Fix the pending problem of requesting console and uc pages in dev mode (#7281) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind bug /area core /milestone 2.20.x #### What this PR does / why we need it: This PR fixes the pending problem of requesting console and ui pages in dev mode. #### Which issue(s) this PR fixes: Fixes https://github.com/halo-dev/halo/issues/7191 #### Special notes for your reviewer: Steps to reproduce: - Start ui projects with dev mode by executing command `make -C ui dev`. - Run Halo instance in dev mode by executing command `./gradlew bootRun --args="--spring.profiles.active=dev"`. - Try to request and . - Try to refresh page by hand and see the result #### Does this PR introduce a user-facing change? ```release-note 修复开发模式下无法正常进入管理和个人中心页面的问题 ``` --- .../run/halo/app/infra/config/WebFluxConfig.java | 3 +++ .../run/halo/app/infra/console/ProxyFilter.java | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) 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); })); } }