2
A
回答
4
請參閱使用示例insecure TrustManagerFactory,它信任所有X.509證書(包括自簽名),而不進行任何驗證。來自文檔的重要注意事項:
切勿在生產中使用此TrustManagerFactory。這純粹是爲了測試目的,因此它非常不安全。
@Bean
public WebClient createWebClient() throws SSLException {
SslContext sslContext = SslContextBuilder
.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.build();
ClientHttpConnector httpConnector = new ReactorClientHttpConnector(opt -> opt.sslContext(sslContext));
return WebClient.builder(httpConnector).build();
}
0
另一種方式,如果你想利用的信任和密鑰存儲在彈簧引導服務器的設置,節目製作代碼,創建一個Spring bean像這樣,可修改注入的WebClient是。在客戶端中,如果您使用2-way-ssl,則只需提供密鑰庫。不確定,爲什麼ssl-stuff沒有預配置並且容易注入,類似於非常酷的spring-boot服務器設置。
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
.
.
.
@Bean
WebClientCustomizer configureWebclient(@Value("${server.ssl.trust-store}") String trustStorePath, @Value("${server.ssl.trust-store-password}") String trustStorePass,
@Value("${server.ssl.key-store}") String keyStorePath, @Value("${server.ssl.key-store-password}") String keyStorePass, @Value("${server.ssl.key-alias}") String keyAlias) {
return new WebClientCustomizer() {
@Override
public void customize(Builder webClientBuilder) {
SslContext sslContext;
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(new FileInputStream(ResourceUtils.getFile(trustStorePath)), trustStorePass.toCharArray());
List<Certificate> certificateCollcetion = Collections.list(trustStore.aliases()).stream().filter(t -> {
try {
return trustStore.isCertificateEntry(t);
} catch (KeyStoreException e1) {
throw new RuntimeException("Error reading truststore", e1);
}
}).map(t -> {
try {
return trustStore.getCertificate(t);
} catch (KeyStoreException e2) {
throw new RuntimeException("Error reading truststore", e2);
}
}).collect(Collectors.toList());
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(ResourceUtils.getFile(keyStorePath)), keyStorePass.toCharArray());
sslContext = SslContextBuilder.forClient()
.keyManager((PrivateKey) keyStore.getKey(keyAlias, keyStorePass.toCharArray()))
.trustManager((X509Certificate[]) certificateCollcetion.toArray(new X509Certificate[certificateCollcetion.size()]))
.build();
} catch (Exception e) {
log.error("Error creating web client", e);
throw new RuntimeException(e);
}
ClientHttpConnector connector = new ReactorClientHttpConnector((opt) -> {
opt.sslContext(sslContext);
});
webClientBuilder.clientConnector(connector);
}
};
}
這裏的一部分,在您使用的Web客戶端: 進口org.springframework.web.reactive.function.client.WebClient;
@Component
public class ClientComponent {
public ClientComponent(WebClient.Builder webClientBuilder, @Value("${url}") String url) {
this.client = webClientBuilder.baseUrl(solrUrl).build();
}
}
相關問題
- 1. Spring Reactive WebClient
- 2. Spring 5 Web Reactive - 我們如何使用WebClient在Flux中檢索流數據?
- 3. Spring 5如何使用log4j
- 4. 使用WebClient c#
- 5. 設置Spring 4 RestTemplate以使用SSL(JKS)
- 6. MVC 5 - 使用WebClient在PartialView中呈現網頁的問題
- 7. Laravel 5和Cloudflare SSL
- 8. 如何通過WebClient使用反應式Spring Rest API
- 9. SSL與Spring和Jetty
- 10. Tomcat/Spring SSL配置
- 11. 使用WebClient啓用壓縮
- 12. Spring 5 Web反應式編程 - WebClient ClassCastException從Spring反應式控制器解編流式數據時的JSON
- 13. 如何在Spring webflux應用程序中使用Spring WebSessionIdResolver和Spring Security 5?
- 14. 在Windows Ce上使用Webclient;
- 15. 使用WebClient或HttpWebRequest登錄
- 16. 如何讓WebClient使用Cookie?
- 17. 使用WebClient取消上傳?
- 18. 使用webclient下載文件
- 19. 上使用Silverlight 2的WebClient
- 20. 如何使用WebClient執行零拷貝上載和下載?
- 21. Spring HTTP調用者的TSL/SSL配置
- 22. Spring AsyncResttemplate HTTPs(SSL)服務調用
- 23. Spring Boot中的SSL證書
- 24. Websocket(spring)ssl連接(空鏈)
- 25. Android Spring和SSL的java.lang.StackOverflowError
- 26. Spring Boot 1.4.1 SSL trustAnors異常
- 27. Spring Boot 2&Spring 5容器混淆
- 28. 使用SSL將laravel 5連接到AWS RDS
- 29. 使用WebClient上傳序列化數據
- 30. 將Spring添加到Spring Boot以啓用SSL
感謝您的回答,我還需要在讀取和連接時設置超時,我該如何實現這一目標? – Seb