Новая функция Spring Boot 3.4: как настроить SSLBundle для работы с RestClient?JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Новая функция Spring Boot 3.4: как настроить SSLBundle для работы с RestClient?

Сообщение Anonymous »

Я использую Spring Boot 3.4.1, JDK 23, MySQL 9. Меня очень радует новая функция RestClient в новой версии Spring Framework. Я могу без каких-либо проблем выполнить взаимный TLS с RestTemplate, теперь я хочу испытать совершенно новую функцию RestClient в контексте SSLBundle (или, если это невозможно, используйте SSLContext).
Файл application.yml

Код: Выделить всё

server:
port: 8001
ssl:
bundle: bar
client-auth: NEED
enabled: true
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/fuu001?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
username: ggAABB
password: XXxxYY
ssl:
bundle:
jks:
foo:
keystore:
location: C:\\ssl\\server-identity.p12
password: fdfd
type: PKCS12
truststore:
location: C:\\ssl\\server-truststore.p12
password: dfsdfsd
type: PKCS12
reload-on-update: true
bar:
keystore:
location: C:\\ssl\\client-identity.p12
password: dfdsfs
type: PKCS12
truststore:
location: C:\\ssl\\client-truststore.p12
password: fdsfd
type: PKCS12
reload-on-update: true
jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
format_sql: true
show-sql: true
#    security:
#      user:
#          name: user
#          password: user
Файл CallFooService.java содержит содержимое

Код: Выделить всё

package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Service
public class CallFooService {

@Autowired
private RestTemplate restTemplate;

public void testMethod(){
String response = restTemplate.getForObject("https://localhost:8001/user", String.class);
System.out.println("response = " + response);

String response2 = restTemplate.getForObject("https://localhost:8002/user", String.class);
System.out.println("response2 = " + response2);

RestClient defaultClient = RestClient.create();

RestClient customClient = RestClient.builder()
.requestFactory(new HttpComponentsClientHttpRequestFactory()) // I want inject SSLBundle or SSL Context at here or something else.
.messageConverters(converters -> converters.add(null))
.baseUrl("https://example.com")
.defaultUriVariables(Map.of("variable", "foo"))
.defaultHeader("My-Header", "Foo")
.defaultCookie("My-Cookie", "Bar")
.requestInterceptor(null)
.requestInitializer(null)
// I want inject SSLBundle or SSL Context at here or something else.
.build();
}

}

Spring Boot 3.4.1: как настроить SSLBundle для работы с RestClient?
Связано:

Подробнее здесь: https://stackoverflow.com/questions/793 ... restclient
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»