java - Cannot finish dynamic SSL context with Apache HTTP client 5 and Spring Boot 3.4.4 - Stack Overflow

I am using JDK 23, Apache HTTP Component 5.4.1, Spring Boot 3.4.4<properties><java.version>

I am using JDK 23, Apache HTTP Component 5.4.1, Spring Boot 3.4.4

<properties>
    <java.version>23</java.version>
    <mavenpiler.source>23</mavenpiler.source>
    <mavenpiler.target>23</mavenpiler.target>
</properties>

<!-- ... -->

<parent>
    <groupId>.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.4.4</version>
    <relativePath/>
</parent>

<!-- ... -->

<dependency>
    <groupId>.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.4.2</version>
</dependency>

File application.yml

server:
    port: 8555
    ssl:
        enabled: true
        bundle:
            pem:
                aceserver:
                    keystore:
                        certificate: C:\\foo\\bar\\aceserver.crt
                        private-key: C:\\foo\\bar\\aceserver.key
                    truststore:
                        certificate: C:\\foo\\bar\\aceserver-truststore.p12
                    reload-on-update: true
                    server042:
                        keystore:
                            certificate: C:\\foo\\bar\\server042.crt
                            private-key: C:\\foo\\bar\\server042.key
                        truststore:
                            certificate: C:\\foo\\bar\\xxx042.crt
                        reload-on-update: true
                    server044:
                        keystore:
                            certificate: C:\\foo\\bar\\server043.crt
                            private-key: C:\\foo\\bar\\server043.key
                        truststore:
                            certificate: C:\\foo\\bar\\xxx043.crt
                        reload-on-update: true

I have about 250 different SSLBundle items what declared inside application.yml . I cannot config 1 SSLBundle for global project as a normal way. I need RESTtempalte with dynamic ability of switching SSLBundle, therferefore, I must do few internal steps manually.

Due to few (many) changes in SSLBundle from Spring Boot 3.3.x to 3.4.x , additional, many change from HTTP Client 4 to HTTP Client 5. I struggled.

SslBundle sslBundle = sslBundles.getBundle("serverName042"); // Hundred of bundle, get from database, for demonstration, it is a hard-coded string.
if (sslBundle == null) {
    throw new IllegalArgumentException("SSL Bundle not found: " + "serverName042");
}

// Get KeyStore and TrustStore from SSLBundle
KeyStore keyStore = sslBundle.getStores().getKeyStore();
KeyStore trustStore = sslBundle.getStores().getTrustStore();

// .4.x/migration-guide/migration-to-classic.html
// .4.x/httpclient5/src/test/java//apache/hc/client5/http/examples/AsyncClientCustomSSL.java

// Build SSLContext manually
SSLContext sslContext = SSLContexts.custom()
        .loadKeyMaterial(keyStore, "secret123".toCharArray()) // Replace with actual password. I want a better way get password of key material from SSLBundle in bundle item of application.yml .
        .loadTrustMaterial(trustStore, null)
        .build();
// Manually create an SSLSocketFactory
//SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

// Create an HTTP Client with the SSLContext.
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setDefaultSocketConfig(null); // I cannot finish it, quite hard.

// I tried this but syntax error
// connectionManager.setDefaultSocketConfig(sslSocketFactory);

CloseableHttpClient httpClient = HttpClients.custom().build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);

//...

String url = baseURL + "/xxx/yyy/zzz/" + dummyID + "/ggg/" + fuuBarID + "/detail";
log.info(">>> url = " + url);
ResponseEntity<Object> response = restTemplate.getForEntity(url, Object.class);

Related

  • .html

  • .4.x/current/httpclient5/apidocs/

Please help me complete this code snippet with HTPT Client 5.4.x and Spring Boot 3.4.4 and JDK 23. (ChatGPT with knowledge cut off on October 2023, I cannot get valuable information from chatbot, because these changes with HTTP Client 5 and Spring Boot 3.4.x SSLBundle methods are new).

I am using JDK 23, Apache HTTP Component 5.4.1, Spring Boot 3.4.4

<properties>
    <java.version>23</java.version>
    <mavenpiler.source>23</mavenpiler.source>
    <mavenpiler.target>23</mavenpiler.target>
</properties>

<!-- ... -->

<parent>
    <groupId>.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.4.4</version>
    <relativePath/>
</parent>

<!-- ... -->

<dependency>
    <groupId>.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.4.2</version>
</dependency>

File application.yml

server:
    port: 8555
    ssl:
        enabled: true
        bundle:
            pem:
                aceserver:
                    keystore:
                        certificate: C:\\foo\\bar\\aceserver.crt
                        private-key: C:\\foo\\bar\\aceserver.key
                    truststore:
                        certificate: C:\\foo\\bar\\aceserver-truststore.p12
                    reload-on-update: true
                    server042:
                        keystore:
                            certificate: C:\\foo\\bar\\server042.crt
                            private-key: C:\\foo\\bar\\server042.key
                        truststore:
                            certificate: C:\\foo\\bar\\xxx042.crt
                        reload-on-update: true
                    server044:
                        keystore:
                            certificate: C:\\foo\\bar\\server043.crt
                            private-key: C:\\foo\\bar\\server043.key
                        truststore:
                            certificate: C:\\foo\\bar\\xxx043.crt
                        reload-on-update: true

I have about 250 different SSLBundle items what declared inside application.yml . I cannot config 1 SSLBundle for global project as a normal way. I need RESTtempalte with dynamic ability of switching SSLBundle, therferefore, I must do few internal steps manually.

Due to few (many) changes in SSLBundle from Spring Boot 3.3.x to 3.4.x , additional, many change from HTTP Client 4 to HTTP Client 5. I struggled.

SslBundle sslBundle = sslBundles.getBundle("serverName042"); // Hundred of bundle, get from database, for demonstration, it is a hard-coded string.
if (sslBundle == null) {
    throw new IllegalArgumentException("SSL Bundle not found: " + "serverName042");
}

// Get KeyStore and TrustStore from SSLBundle
KeyStore keyStore = sslBundle.getStores().getKeyStore();
KeyStore trustStore = sslBundle.getStores().getTrustStore();

// https://hc.apache./httpcomponents-client-5.4.x/migration-guide/migration-to-classic.html
// https://github/apache/httpcomponents-client/blob/5.4.x/httpclient5/src/test/java//apache/hc/client5/http/examples/AsyncClientCustomSSL.java

// Build SSLContext manually
SSLContext sslContext = SSLContexts.custom()
        .loadKeyMaterial(keyStore, "secret123".toCharArray()) // Replace with actual password. I want a better way get password of key material from SSLBundle in bundle item of application.yml .
        .loadTrustMaterial(trustStore, null)
        .build();
// Manually create an SSLSocketFactory
//SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

// Create an HTTP Client with the SSLContext.
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setDefaultSocketConfig(null); // I cannot finish it, quite hard.

// I tried this but syntax error
// connectionManager.setDefaultSocketConfig(sslSocketFactory);

CloseableHttpClient httpClient = HttpClients.custom().build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);

//...

String url = baseURL + "/xxx/yyy/zzz/" + dummyID + "/ggg/" + fuuBarID + "/detail";
log.info(">>> url = " + url);
ResponseEntity<Object> response = restTemplate.getForEntity(url, Object.class);

Related

  • https://docs.spring.io/spring-boot/api/java//springframework/boot/ssl/package-summary.html

  • https://hc.apache./httpcomponents-client-5.4.x/current/httpclient5/apidocs/

Please help me complete this code snippet with HTPT Client 5.4.x and Spring Boot 3.4.4 and JDK 23. (ChatGPT with knowledge cut off on October 2023, I cannot get valuable information from chatbot, because these changes with HTTP Client 5 and Spring Boot 3.4.x SSLBundle methods are new).

Share Improve this question edited Mar 27 at 4:16 Vy Do asked Mar 27 at 3:27 Vy DoVy Do 52.9k69 gold badges256 silver badges387 bronze badges 1
  • 1 This server.ssl.bundle property will not work the way you are trying to use it here. That property only takes the name of a bundle that was defined using spring.ssl.bundle.pem.<name>. – Scott Frederick Commented Mar 27 at 15:30
Add a comment  | 

1 Answer 1

Reset to default 0
SslBundle sslBundle = sslBundles.getBundle("foo");
SSLContext sslContext = sslBundle.createSslContext();
final HttpClient client = HttpClients.custom().setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create().setTlsSocketStrategy(new DefaultClientTlsStrategy(sslContext, () -> true)).build()).build();
final HttpGet httpget = new HttpGet("http://example");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744114447a4559104.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信