我正在尝试利用 Spring 5 Webclient 和内置的 oauth2 安全功能。
@Bean("oauth2WebClient")
public WebClient oauth2WebClient(final ReactiveOAuth2AuthorizedClientManager reactiveOAuth2AuthorizedClientManager) {
final ServerOAuth2AuthorizedClientExchangeFilterFunction serverOAuth2AuthorizedClientExchangeFilterFunction =
new ServerOAuth2AuthorizedClientExchangeFilterFunction(reactiveOAuth2AuthorizedClientManager);
serverOAuth2AuthorizedClientExchangeFilterFunction.setDefaultClientRegistrationId("app");
try {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(new ClassPathResource("app.jks").getFile()), "password".toCharArray());
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(new FileInputStream(new ClassPathResource("app.jks").getFile()), "password".toCharArray());
// Set up key manager factory to use our key store
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, "password".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
SslContext sslContext = SslContextBuilder.forClient()
.keyManager(keyManagerFactory)
.trustManager(trustManagerFactory)
.build();
//
//
// List<Certificate> certificateList = 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());
//
// X509Certificate[] certificates = certificateList.toArray(new X509Certificate[certificateList.size()]);
// //PrivateKey trustedCerts = (PrivateKey) keyStore.getKey("certs", "password".toCharArray());
// //PrivateKey privateKey = (PrivateKey) keyStore.getKey("certs", "password".toCharArray());
// //Certificate[] certChain = keyStore.getCertificateChain("certs");
//// X509Certificate[] x509CertificateChain = Arrays.stream(certChain)
//// .map(certificate -> (X509Certificate) certificate)
//// .collect(Collectors.toList())
//// .toArray(new X509Certificate[certChain.length]);
//
// SslContext sslContext = SslContextBuilder.forClient()
// .keyManager(keyManagerFactory)
// .trustManager(certificates)
// .build();
HttpClient httpClient = HttpClient.create()
.secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
return WebClient.builder()
.filter(serverOAuth2AuthorizedClientExchangeFilterFunction)
.clientConnector(connector)
.build();
当我使用 Webclient 获取访问令牌并使用该令牌启动服务调用时,我已经确认 SSL 证书设置正确并且可以正常工作。
当我使用内置的 oauth2 库时,出现以下错误:
org.springframework.security.oauth2.client.ClientAuthorizationException: [server_error]
我在堆栈跟踪中看到以下内容
reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ Request to POST <url>[DefaultWebClient]
Stack trace:
java.lang.Exception: #block terminated with an error
我在这里做错了什么?