Skip to content

Commit 9bc5f42

Browse files
committed
add some utils
1 parent 83e3e58 commit 9bc5f42

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

springboot-starter/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
<artifactId>commons-io</artifactId>
4242
</dependency>
4343

44+
<dependency>
45+
<groupId>org.apache.httpcomponents</groupId>
46+
<artifactId>httpclient</artifactId>
47+
</dependency>
48+
4449
<dependency>
4550
<groupId>org.springframework.boot</groupId>
4651
<artifactId>spring-boot-starter-web</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.codingapi.springboot.framework.utils;
2+
3+
4+
import lombok.SneakyThrows;
5+
import org.apache.http.client.HttpClient;
6+
import org.apache.http.conn.ssl.NoopHostnameVerifier;
7+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
8+
import org.apache.http.impl.client.HttpClients;
9+
10+
import javax.net.ssl.SSLContext;
11+
import javax.net.ssl.TrustManager;
12+
import javax.net.ssl.X509TrustManager;
13+
import java.security.cert.CertificateException;
14+
import java.security.cert.X509Certificate;
15+
16+
public class TrustAnyHttpClientFactory {
17+
18+
private TrustAnyHttpClientFactory(){
19+
20+
}
21+
22+
private static class TrustAnyTrustManager implements X509TrustManager {
23+
24+
@Override
25+
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}
26+
27+
@Override
28+
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {}
29+
30+
@Override
31+
public X509Certificate[] getAcceptedIssuers() {
32+
return new X509Certificate[0];
33+
}
34+
}
35+
36+
@SneakyThrows
37+
public static HttpClient createTrustAnyHttpClient() {
38+
SSLContext sslContext = SSLContext.getInstance("TLS");
39+
TrustAnyTrustManager trustAnyTrustManager = new TrustAnyTrustManager();
40+
sslContext.init(null, new TrustManager[] {trustAnyTrustManager}, null);
41+
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
42+
return HttpClients.custom()
43+
.setSSLSocketFactory(sslConnectionSocketFactory)
44+
.build();
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.codingapi.springboot.framework.utils;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.http.client.ClientHttpRequestFactory;
5+
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
6+
import org.springframework.web.client.RestTemplate;
7+
8+
import static org.junit.jupiter.api.Assertions.assertNotNull;
9+
10+
class TrustAnyHttpClientFactoryTest {
11+
12+
@Test
13+
void createTrustAnyHttpClient() {
14+
ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(TrustAnyHttpClientFactory.createTrustAnyHttpClient());
15+
RestTemplate restTemplate = new RestTemplate(factory);
16+
String response = restTemplate.getForObject("http://github.com",String.class);
17+
assertNotNull(response);
18+
}
19+
}

0 commit comments

Comments
 (0)