Fix the pending problem of requesting console and uc pages in dev mode (#7281)

#### 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 <http://localhost:8090/uc> and <http://localhost:8090/console>.
- Try to refresh page by hand and see the result

#### Does this PR introduce a user-facing change?

```release-note
修复开发模式下无法正常进入管理和个人中心页面的问题
```
pull/7288/head
John Niang 2025-03-10 16:19:01 +08:00 committed by GitHub
parent 2c4c876ef2
commit ddbbe09c2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 3 deletions

View File

@ -14,6 +14,7 @@ import org.springframework.boot.autoconfigure.web.reactive.WebFluxRegistrations;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.http.CacheControl; import org.springframework.http.CacheControl;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -218,6 +219,7 @@ public class WebFluxConfig implements WebFluxConfigurer {
@ConditionalOnProperty(name = "halo.console.proxy.enabled", havingValue = "true") @ConditionalOnProperty(name = "halo.console.proxy.enabled", havingValue = "true")
@Bean @Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
ProxyFilter consoleProxyFilter() { ProxyFilter consoleProxyFilter() {
return new ProxyFilter("/console/**", haloProp.getConsole().getProxy()); return new ProxyFilter("/console/**", haloProp.getConsole().getProxy());
} }
@ -225,6 +227,7 @@ public class WebFluxConfig implements WebFluxConfigurer {
@ConditionalOnProperty(name = "halo.uc.proxy.enabled", havingValue = "true") @ConditionalOnProperty(name = "halo.uc.proxy.enabled", havingValue = "true")
@Bean @Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
ProxyFilter ucProxyFilter() { ProxyFilter ucProxyFilter() {
return new ProxyFilter("/uc/**", haloProp.getUc().getProxy()); return new ProxyFilter("/uc/**", haloProp.getUc().getProxy());
} }

View File

@ -3,6 +3,7 @@ package run.halo.app.infra.console;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpMethod; 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.AndServerWebExchangeMatcher;
import org.springframework.security.web.server.util.matcher.NegatedServerWebExchangeMatcher; import org.springframework.security.web.server.util.matcher.NegatedServerWebExchangeMatcher;
import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher; 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())) .headers(httpHeaders -> httpHeaders.addAll(exchange.getRequest().getHeaders()))
.exchangeToMono(clientResponse -> { .exchangeToMono(clientResponse -> {
var response = exchange.getResponse(); var response = exchange.getResponse();
var httpStatusCode = clientResponse.statusCode();
// set headers // set headers
response.getHeaders().putAll(clientResponse.headers().asHttpHeaders()); var httpHeaders = clientResponse.headers().asHttpHeaders();
response.getHeaders().putAll(httpHeaders);
// set cookies // set cookies
response.getCookies().putAll(clientResponse.cookies()); response.getCookies().putAll(clientResponse.cookies());
// set status code // 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); var body = clientResponse.bodyToFlux(DataBuffer.class);
return exchange.getResponse().writeWith(body); return response.writeWith(body);
})); }));
} }
} }