18
18
import java .util .Arrays ;
19
19
import java .util .function .Supplier ;
20
20
21
+ import javax .net .ssl .KeyManagerFactory ;
22
+ import javax .net .ssl .TrustManagerFactory ;
23
+
24
+ import io .netty .handler .ssl .SslContext ;
25
+ import io .netty .handler .ssl .SslContextBuilder ;
26
+ import reactor .netty .http .client .HttpClient ;
27
+ import reactor .netty .tcp .SslProvider ;
21
28
import sample .authorization .DeviceCodeOAuth2AuthorizedClientProvider ;
22
29
23
30
import org .springframework .beans .factory .annotation .Qualifier ;
31
+ import org .springframework .boot .ssl .SslBundle ;
32
+ import org .springframework .boot .ssl .SslBundles ;
24
33
import org .springframework .boot .web .client .RestTemplateBuilder ;
25
34
import org .springframework .context .annotation .Bean ;
26
35
import org .springframework .context .annotation .Configuration ;
27
36
import org .springframework .http .client .ClientHttpRequestFactory ;
37
+ import org .springframework .http .client .reactive .ClientHttpConnector ;
38
+ import org .springframework .http .client .reactive .ReactorClientHttpConnector ;
28
39
import org .springframework .http .converter .FormHttpMessageConverter ;
29
40
import org .springframework .security .oauth2 .client .OAuth2AuthorizedClientManager ;
30
41
import org .springframework .security .oauth2 .client .OAuth2AuthorizedClientProvider ;
54
65
public class WebClientConfig {
55
66
56
67
@ Bean ("default-client-web-client" )
57
- public WebClient defaultClientWebClient (OAuth2AuthorizedClientManager authorizedClientManager ) {
68
+ public WebClient defaultClientWebClient (
69
+ OAuth2AuthorizedClientManager authorizedClientManager ,
70
+ SslBundles sslBundles ) throws Exception {
71
+
58
72
ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
59
73
new ServletOAuth2AuthorizedClientExchangeFilterFunction (authorizedClientManager );
60
74
// @formatter:off
61
75
return WebClient .builder ()
76
+ .clientConnector (createClientConnector (sslBundles .getBundle ("demo-client" )))
62
77
.apply (oauth2Client .oauth2Configuration ())
63
78
.build ();
64
79
// @formatter:on
@@ -69,7 +84,8 @@ public WebClient selfSignedDemoClientWebClient(
69
84
ClientRegistrationRepository clientRegistrationRepository ,
70
85
OAuth2AuthorizedClientRepository authorizedClientRepository ,
71
86
RestTemplateBuilder restTemplateBuilder ,
72
- @ Qualifier ("self-signed-demo-client-http-request-factory" ) Supplier <ClientHttpRequestFactory > clientHttpRequestFactory ) {
87
+ @ Qualifier ("self-signed-demo-client-http-request-factory" ) Supplier <ClientHttpRequestFactory > clientHttpRequestFactory ,
88
+ SslBundles sslBundles ) throws Exception {
73
89
74
90
// @formatter:off
75
91
RestTemplate restTemplate = restTemplateBuilder
@@ -98,6 +114,7 @@ public WebClient selfSignedDemoClientWebClient(
98
114
new ServletOAuth2AuthorizedClientExchangeFilterFunction (authorizedClientManager );
99
115
// @formatter:off
100
116
return WebClient .builder ()
117
+ .clientConnector (createClientConnector (sslBundles .getBundle ("self-signed-demo-client" )))
101
118
.apply (oauth2Client .oauth2Configuration ())
102
119
.build ();
103
120
// @formatter:on
@@ -143,6 +160,22 @@ public OAuth2AuthorizedClientManager authorizedClientManager(
143
160
return authorizedClientManager ;
144
161
}
145
162
163
+ private static ClientHttpConnector createClientConnector (SslBundle sslBundle ) throws Exception {
164
+ KeyManagerFactory keyManagerFactory = sslBundle .getManagers ().getKeyManagerFactory ();
165
+ TrustManagerFactory trustManagerFactory = sslBundle .getManagers ().getTrustManagerFactory ();
166
+
167
+ // @formatter:off
168
+ SslContext sslContext = SslContextBuilder .forClient ()
169
+ .keyManager (keyManagerFactory )
170
+ .trustManager (trustManagerFactory )
171
+ .build ();
172
+ // @formatter:on
173
+
174
+ SslProvider sslProvider = SslProvider .builder ().sslContext (sslContext ).build ();
175
+ HttpClient httpClient = HttpClient .create ().secure (sslProvider );
176
+ return new ReactorClientHttpConnector (httpClient );
177
+ }
178
+
146
179
private static OAuth2AccessTokenResponseClient <OAuth2ClientCredentialsGrantRequest > createClientCredentialsTokenResponseClient (
147
180
RestTemplate restTemplate ) {
148
181
DefaultClientCredentialsTokenResponseClient clientCredentialsTokenResponseClient =
0 commit comments