Spring RestTemplate Part1
- 1. 扩展 RestTemplate Get 方法, make GET with Request Body.
- 2. RestTemplate 代理(proxy)设置
- 3. RestTemplate OAuth
- 4. 完整Class example
1. 扩展 RestTemplate Get 方法, make GET with Request Body.
private static final class HttpComponentsClientHttpRequestWithBodyFactory extends HttpComponentsClientHttpRequestFactory
private static final class HttpGetRequestWithEntity extends HttpEntityEnclosingRequestBase
refer1: https://cleanprogrammer.net/making-get-request-with-body-using-spring-resttemplate/
refer2: https://mekaso.rocks/get-requests-with-a-request-body-spring-resttemplate-vs-apache-httpclient
2. RestTemplate 代理(proxy)设置
final String proxyUrl = "adc-opc-proxy.usdc1.oraclecloud.com";
//final String proxyUrl = "www-proxy-hqdc.us.oracle.com";
final int port = 80;
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxyUrl, port),
new UsernamePasswordCredentials("", ""));
HttpHost myProxy = new HttpHost(proxyUrl, port);
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.setProxy(myProxy).setDefaultCredentialsProvider(credsProvider).disableCookieManagement();
HttpClient httpClient = clientBuilder.build();
requestFactory.setHttpClient(httpClient);
RestTemplate.setRequestFactory(requestFactory);
3. RestTemplate OAuth
private String accessTokenUri = Configuration.getValue("accessTokenUri", "");
private String clientID = Configuration.getValue("clientID", "");
private String clientSecret = Configuration.getValue("clientSecret", "");
//private String scope = Configuration.getValue("scope", "");
//private String scope = Constants.esBaseUrl;
private String scope = Constants.scope;
public OAuth2ProtectedResourceDetails oscs() {
ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
details.setId("oscs");
details.setClientId(clientID);
details.setClientSecret(clientSecret);
details.setAccessTokenUri(accessTokenUri);
details.setTokenName("access_token");
details.setScope(Arrays.asList(scope));
return details;
}
public OAuth2RestTemplate oscsRestTemplate() {
OAuth2RestTemplate template = new OAuth2RestTemplate(oscs());
AccessTokenProvider accessTokenProvider = new AccessTokenProviderChain(
Arrays.<AccessTokenProvider>asList(new ClientCredentialsAccessTokenProvider()));
template.setAccessTokenProvider(accessTokenProvider);
template.setRequestFactory(requestFactory);
return template;
}
4. 完整Class example
package rest.api.util;
import java.net.URI;
import java.util.Arrays;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.client.token.AccessTokenProvider;
import org.springframework.security.oauth2.client.token.AccessTokenProviderChain;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
import org.springframework.web.client.RestTemplate;
@EnableOAuth2Client
public class ResourceConfiguration {
private String accessTokenUri = Configuration.getValue("accessTokenUri", "");
private String clientID = Configuration.getValue("clientID", "");
private String clientSecret = Configuration.getValue("clientSecret", "");
//private String scope = Configuration.getValue("scope", "");
//private String scope = Constants.esBaseUrl;
private String scope = Constants.scope;
public OAuth2ProtectedResourceDetails oscs() {
ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails();
details.setId("oscs");
details.setClientId(clientID);
details.setClientSecret(clientSecret);
details.setAccessTokenUri(accessTokenUri);
details.setTokenName("access_token");
details.setScope(Arrays.asList(scope));
return details;
}
public OAuth2RestTemplate oscsRestTemplate() {
OAuth2RestTemplate template = new OAuth2RestTemplate(oscs());
AccessTokenProvider accessTokenProvider = new AccessTokenProviderChain(
Arrays.<AccessTokenProvider>asList(new ClientCredentialsAccessTokenProvider()));
template.setAccessTokenProvider(accessTokenProvider);
template.setRequestFactory(setRequestFactory());
return template;
}
public RestTemplate oscsRestTemplateNoAuth() {
RestTemplate template = new RestTemplate();
template.setRequestFactory(setRequestFactory());
return template;
}
private HttpComponentsClientHttpRequestWithBodyFactory setRequestFactory() {
HttpComponentsClientHttpRequestWithBodyFactory requestFactory = new HttpComponentsClientHttpRequestWithBodyFactory();
if (Constants.CONFIG_FILE_NAME.contains("Prod")) {
final String proxyUrl = "adc-opc-proxy.usdc1.oraclecloud.com";
//final String proxyUrl = "www-proxy-hqdc.us.oracle.com";
final int port = 80;
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(proxyUrl, port),
new UsernamePasswordCredentials("", ""));
HttpHost myProxy = new HttpHost(proxyUrl, port);
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.setProxy(myProxy).setDefaultCredentialsProvider(credsProvider).disableCookieManagement();
HttpClient httpClient = clientBuilder.build();
requestFactory.setHttpClient(httpClient);
}
return requestFactory;
}
private static final class HttpComponentsClientHttpRequestWithBodyFactory extends HttpComponentsClientHttpRequestFactory {
@Override
protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
if (httpMethod == HttpMethod.GET) {
return new HttpGetRequestWithEntity(uri);
}
return super.createHttpUriRequest(httpMethod, uri);
}
}
private static final class HttpGetRequestWithEntity extends HttpEntityEnclosingRequestBase {
public HttpGetRequestWithEntity(final URI uri) {
super.setURI(uri);
}
@Override
public String getMethod() {
return HttpMethod.GET.name();
}
}
}