From ed647c262d9f8ea1c30f2e6db12970668e4f1b0f Mon Sep 17 00:00:00 2001 From: Halo Dev Bot <87291978+halo-dev-bot@users.noreply.github.com> Date: Fri, 7 Apr 2023 17:38:15 +0800 Subject: [PATCH] [release-2.4] Set property server.forward-header-strategy to framework by default (#3710) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is an automated cherry-pick of #3709 /assign JohnNiang ```release-note 解决反向代理后无法正确获取当前请求 URI 的问题 ``` --- .../src/main/resources/application.yaml | 1 + .../java/run/halo/app/XForwardHeaderTest.java | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 application/src/test/java/run/halo/app/XForwardHeaderTest.java diff --git a/application/src/main/resources/application.yaml b/application/src/main/resources/application.yaml index 80ca209020..491033eb4f 100644 --- a/application/src/main/resources/application.yaml +++ b/application/src/main/resources/application.yaml @@ -1,5 +1,6 @@ server: port: 8090 + forward-headers-strategy: framework compression: enabled: true error: diff --git a/application/src/test/java/run/halo/app/XForwardHeaderTest.java b/application/src/test/java/run/halo/app/XForwardHeaderTest.java new file mode 100644 index 0000000000..e0e15e9be4 --- /dev/null +++ b/application/src/test/java/run/halo/app/XForwardHeaderTest.java @@ -0,0 +1,55 @@ +package run.halo.app; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; +import static org.springframework.web.reactive.function.server.RequestPredicates.GET; +import static org.springframework.web.reactive.function.server.RouterFunctions.route; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpStatus; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.reactive.function.server.RouterFunction; +import org.springframework.web.reactive.function.server.ServerResponse; +import reactor.test.StepVerifier; + +@SpringBootTest(webEnvironment = RANDOM_PORT, + properties = "server.forward-headers-strategy=framework") +class XForwardHeaderTest { + + @LocalServerPort + int port; + + @Test + void shouldGetCorrectProtoFromXForwardHeaders() { + var response = WebClient.create("http://localhost:" + port) + .get().uri("/print-uri") + .header("X-Forwarded-Proto", "https") + .header("X-Forwarded-Host", "halo.run") + .header("X-Forwarded-Port", "6666") + .retrieve() + .toEntity(String.class); + StepVerifier.create(response) + .assertNext(entity -> { + assertEquals(HttpStatus.OK, entity.getStatusCode()); + assertEquals("\"https://halo.run:6666/print-uri\"", entity.getBody()); + }) + .verifyComplete(); + } + + @TestConfiguration + static class Configuration { + + @Bean + RouterFunction printUri() { + return route(GET("/print-uri"), + request -> { + var uri = request.exchange().getRequest().getURI(); + return ServerResponse.ok().bodyValue(uri); + }); + } + } +}