diff --git a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java index 82b2004609b4..5bca8932100a 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java @@ -471,6 +471,11 @@ public String toUriString() { */ public UriComponentsBuilder uri(URI uri) { Assert.notNull(uri, "URI must not be null"); + if (uri.getHost() == null && uri.getAuthority() != null) { + // see gh-27774 + uriComponents(UriComponentsBuilder.fromUriString(uri.toString()).build()); + return this; + } this.scheme = uri.getScheme(); if (uri.isOpaque()) { this.ssp = uri.getRawSchemeSpecificPart(); diff --git a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java index a5face72d171..388ce084bf72 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java @@ -160,6 +160,17 @@ void fromUriEncodedQuery() { assertThat(fromUriString).isEqualTo(fromUri); } + @Test // see gh-27774 + void fromUriRegistryBasedAuthority() { + URI uri = URI.create("http://elated_sutherland:8080/auth/realms/my-realm"); + UriComponents result = UriComponentsBuilder.fromUri(uri).build(); + + assertThat(result.getScheme()).isEqualTo("http"); + assertThat(result.getHost()).isEqualTo("elated_sutherland"); + assertThat(result.getPort()).isEqualTo(8080); + assertThat(result.getPath()).isEqualTo("/auth/realms/my-realm"); + } + @Test void fromUriString() { UriComponents result = UriComponentsBuilder.fromUriString("https://www.ietf.org/rfc/rfc3986.txt").build();