Add support for rewriting URL with trailing slash (#7559)

pull/7489/head
John Niang 2025-06-17 11:29:54 +08:00 committed by GitHub
parent 8fd23e2c68
commit 7d560186e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 1 deletions

View File

@ -25,6 +25,7 @@ import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.lang.NonNull;
import org.springframework.web.filter.reactive.ServerWebExchangeContextFilter;
import org.springframework.web.filter.reactive.UrlHandlerFilter;
import org.springframework.web.reactive.config.ResourceHandlerRegistration;
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;
@ -267,4 +268,11 @@ public class WebFluxConfig implements WebFluxConfigurer {
return new ServerWebExchangeContextFilter();
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
UrlHandlerFilter urlHandlerFilter() {
return UrlHandlerFilter
.trailingSlashHandler("/**").mutateRequest()
.build();
}
}

View File

@ -42,7 +42,8 @@ import run.halo.app.extension.Metadata;
SpringBootTest.WebEnvironment.RANDOM_PORT)
@Import({
WebFluxConfigTest.WebSocketSupportTest.TestWebSocketConfiguration.class,
WebFluxConfigTest.ServerWebExchangeContextFilterTest.TestConfig.class
WebFluxConfigTest.ServerWebExchangeContextFilterTest.TestConfig.class,
WebFluxConfigTest.UrlHandlerFilterTest.TestConfig.class
})
@AutoConfigureWebTestClient
class WebFluxConfigTest {
@ -206,4 +207,29 @@ class WebFluxConfigTest {
}
}
@Nested
class UrlHandlerFilterTest {
@TestConfiguration
static class TestConfig {
@Bean
RouterFunction<ServerResponse> urlHandlerFilterTestRoute() {
return RouterFunctions.route()
.GET("/fake", request -> ServerResponse.ok().bodyValue("ok"))
.build();
}
}
@Test
void shouldHandleUrlWithTrailingSlash() {
webClient.get().uri("/fake/")
.exchange()
.expectStatus().isOk()
.expectBody(String.class).isEqualTo("ok");
}
}
}