forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update
RestTemplateBuilder
and TestRestTemplate
to use a custom r…
…equest factory to add authentication headers. Prior to this commit, the `RestTemplateBuilder` and `TestRestTemplate` used the `BasicAuthenticationInterceptor` interceptor to add headers. Unfortunately, adding any interceptor causes the entire message body to be read into a byte array. This causes an `OutOfMemoryError` whenever a large file is uploaded. Closes spring-projectsgh-15078
- Loading branch information
Showing
7 changed files
with
331 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...ct/spring-boot/src/main/java/org/springframework/boot/web/client/BasicAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2012-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.web.client; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Basic authentication properties which are used by | ||
* {@link BasicAuthenticationClientHttpRequestFactory}. | ||
* | ||
* @author Dmytro Nosan | ||
* @since 2.2.0 | ||
* @see BasicAuthenticationClientHttpRequestFactory | ||
*/ | ||
public class BasicAuthentication { | ||
|
||
private final String username; | ||
|
||
private final String password; | ||
|
||
private final Charset charset; | ||
|
||
/** | ||
* Create a new {@link BasicAuthentication}. | ||
* @param username the username to use | ||
* @param password the password to use | ||
*/ | ||
public BasicAuthentication(String username, String password) { | ||
this(username, password, null); | ||
} | ||
|
||
/** | ||
* Create a new {@link BasicAuthentication}. | ||
* @param username the username to use | ||
* @param password the password to use | ||
* @param charset the charset to use | ||
*/ | ||
public BasicAuthentication(String username, String password, Charset charset) { | ||
Assert.notNull(username, "Username must not be null"); | ||
Assert.notNull(password, "Password must not be null"); | ||
this.username = username; | ||
this.password = password; | ||
this.charset = charset; | ||
} | ||
|
||
/** | ||
* The username to use. | ||
* @return the username, never {@code null} or {@code empty}. | ||
*/ | ||
public String getUsername() { | ||
return this.username; | ||
} | ||
|
||
/** | ||
* The password to use. | ||
* @return the password, never {@code null} or {@code empty}. | ||
*/ | ||
public String getPassword() { | ||
return this.password; | ||
} | ||
|
||
/** | ||
* The charset to use. | ||
* @return the charset, or {@code null}. | ||
*/ | ||
public Charset getCharset() { | ||
return this.charset; | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...java/org/springframework/boot/web/client/BasicAuthenticationClientHttpRequestFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2012-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.web.client; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.client.AbstractClientHttpRequestFactoryWrapper; | ||
import org.springframework.http.client.ClientHttpRequest; | ||
import org.springframework.http.client.ClientHttpRequestFactory; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* {@link ClientHttpRequestFactory} to apply a given HTTP Basic Authentication | ||
* username/password pair, unless a custom Authorization header has been set before. | ||
* | ||
* @author Dmytro Nosan | ||
* @since 2.2.0 | ||
*/ | ||
public class BasicAuthenticationClientHttpRequestFactory | ||
extends AbstractClientHttpRequestFactoryWrapper { | ||
|
||
private final BasicAuthentication authentication; | ||
|
||
/** | ||
* Create a new {@link BasicAuthenticationClientHttpRequestFactory} which adds | ||
* {@link HttpHeaders#AUTHORIZATION} header for the given authentication. | ||
* @param authentication the authentication to use | ||
* @param clientHttpRequestFactory the factory to use | ||
*/ | ||
public BasicAuthenticationClientHttpRequestFactory(BasicAuthentication authentication, | ||
ClientHttpRequestFactory clientHttpRequestFactory) { | ||
super(clientHttpRequestFactory); | ||
Assert.notNull(authentication, "Authentication must not be null"); | ||
this.authentication = authentication; | ||
} | ||
|
||
@Override | ||
protected ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod, | ||
ClientHttpRequestFactory requestFactory) throws IOException { | ||
BasicAuthentication authentication = this.authentication; | ||
ClientHttpRequest request = requestFactory.createRequest(uri, httpMethod); | ||
HttpHeaders headers = request.getHeaders(); | ||
if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) { | ||
headers.setBasicAuth(authentication.getUsername(), | ||
authentication.getPassword(), authentication.getCharset()); | ||
} | ||
return request; | ||
} | ||
|
||
} |
Oops, something went wrong.