-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Description
Hi Spring Team,
String addr = "[email protected]";
String encodedAddr = URLEncoder.encode(addr, StandardCharsets.UTF_8);
UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://localhost:8080/emails/{email}")
.uriVariables(Map.of("email", encodedAddr))
.build();
System.out.println(uriComponents.toUriString());
System.out.println(uriComponents.toUri());
Above code will print
http://localhost:8080/emails/a%40b.com
http://localhost:8080/emails/a%2540b.com
This is because, the UriComponentsBuilder.buildInternal(EncodingHint hint)
was called with the hint was EncodingHint.NONE
. After that, HierarchicalUriComponents.expand(UriTemplateVariables uriVariables)
called and the a%40b.com
was placed to the {email}
placeholder without any encoding. Finally, uriComponents.toUriString()
did nothing, but just concaternate scheme, host, path, etc together to produced http://localhost:8080/emails/a%40b.com
.
In case of uriComponents.toUri()
, the constructor java.net.URI#URI(java.lang.String, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)
called, which encoded the a%40b.com
again, so we got http://localhost:8080/emails/a%2540b.com
Proposal: Consider if EncodingHint#NONE
provided, then call new URI(toUriString());
instead