Polish "Add Basic Authorization for UrlResource"

See gh-1822
This commit is contained in:
Stephane Nicoll
2023-08-22 16:38:00 +02:00
parent ac9ca412c8
commit f95a1f49df
2 changed files with 27 additions and 7 deletions

View File

@@ -28,10 +28,10 @@ import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.Base64Utils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;
@@ -42,12 +42,13 @@ import org.springframework.util.StringUtils;
*
* @author Juergen Hoeller
* @author Sam Brannen
* @author Denis Kostin
* @since 28.12.2003
* @see java.net.URL
*/
public class UrlResource extends AbstractFileResolvingResource {
private static final String AUTHORIZATION = "Authorization";
/**
* Original URI, if available; used for URI and File access.
*/
@@ -227,11 +228,6 @@ public class UrlResource extends AbstractFileResolvingResource {
public InputStream getInputStream() throws IOException {
URLConnection con = this.url.openConnection();
customizeConnection(con);
if (this.url.getUserInfo() != null) {
String basicAuth = "Basic " + Base64Utils.encodeToString(url.getUserInfo().getBytes());
con.setRequestProperty("Authorization", basicAuth);
}
try {
return con.getInputStream();
}
@@ -244,6 +240,16 @@ public class UrlResource extends AbstractFileResolvingResource {
}
}
@Override
protected void customizeConnection(URLConnection con) throws IOException {
super.customizeConnection(con);
String userInfo = this.url.getUserInfo();
if (userInfo != null) {
String encodedCredentials = Base64.getUrlEncoder().encodeToString(userInfo.getBytes());
con.setRequestProperty(AUTHORIZATION, "Basic " + encodedCredentials);
}
}
/**
* This implementation returns the underlying URL reference.
*/

View File

@@ -33,6 +33,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.stream.Stream;
import okhttp3.mockwebserver.Dispatcher;
@@ -377,6 +378,19 @@ class ResourceTests {
assertThat(request.getHeader("Framework-Name")).isEqualTo("Spring");
}
@Test
void useUserInfoToSetBasicAuth() throws Exception {
startServer();
UrlResource resource = new UrlResource("http://alice:secret@localhost:"
+ this.server.getPort() + "/resource");
assertThat(resource.getInputStream()).hasContent("Spring");
RecordedRequest request = this.server.takeRequest();
String authorization = request.getHeader("Authorization");
assertThat(authorization).isNotNull().startsWith("Basic ");
assertThat(new String(Base64.getDecoder().decode(
authorization.substring(6)), StandardCharsets.ISO_8859_1)).isEqualTo("alice:secret");
}
@AfterEach
void shutdown() throws Exception {
this.server.shutdown();