import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext; import java.lang.reflect.Field; import java.nio.charset.StandardCharsets; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate;
@Slf4j public class RestTemplateUtils
{
static final int connectTimeOut = 1000 * 10;
public static RestTemplate restTemplate() throws KeyStoreException, NoSuchAlgorithmException,
KeyManagementException {
TrustStrategy
acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build(); // SSLConnectionSocketFactory csf = new
SSLConnectionSocketFactory(sslContext);
//默认允许所有的域名 , 如果不指定域名的话某些域名无法访问
SSLConnectionSocketFactory
csf = new SSLConnectionSocketFactory(sslContext,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpClient
= HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
requestFactory.setConnectTimeout(connectTimeOut);
RestTemplate restTemplate = new RestTemplate(requestFactory);
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
return restTemplate;
}
/**
* 执行调用restful接口
*
* @param url 资源地址
* @param methodType
调用方法类型HttpMethod.GET...
* @param httpBody httpBody类型
* @param returnType
返回类型
* @param <T>
* @param <R>
* @return
*/
public static <T, R> R restfulInvoke(String url, HttpMethod methodType,
HttpHeaders headers, T httpBody, Class<R>
returnType) {
HttpEntity entity = new HttpEntity(httpBody, headers);
ResponseEntity<R>
exchange = null;
try {
exchange = RestTemplateUtils.restTemplate().exchange(url,
methodType, entity, returnType);
return exchange.getBody();
} catch (KeyStoreException | NoSuchAlgorithmException |
KeyManagementException e) {
log.info("https验证异常", e);
}
return null;
}
public static void main(String[] args) throws NoSuchAlgorithmException,
KeyStoreException, KeyManagementException {
String url = "http://180.166.132.100/mock/128/data/entryDeclportSta/cen043/v1";
RestTemplate restTemplate = restTemplate();
JSONObject body = JSONObject.parseObject("{ \n" +
" \"auth\": { \n" +
" \"identity\": { \n" +
" \"methods\": [\"password\"], \n" +
" \"password\": { \n" +
" \"user\": { \n" +
" \"name\": \"qy_admin\", \n" +
" \"password\": \"Huawei@123\", \n" +
" \"domain\": { \n" +
"
\"name\": \"qy_tenant\" \n" +
" }
\n" +
" } \n" +
" } \n" +
" }, \n" +
" \"scope\": { \n" +
" \"project\": { \n" +
" \"id\": \"5f4d3cf5d4824b98b4164c7c58f0076a\" \n" +
" } \n" +
" } \n" +
" } \n" +
"}");
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
HttpEntity entity = new HttpEntity(body, headers);
ResponseEntity<JSONObject> response = restTemplate.exchange(url,
HttpMethod.POST, null,
JSONObject.class);
System.out.println(response.toString());
}
}
|