我们有一个用例,我们需要访问受 OAuth2 + mTLS 保护的 HTTPS 端点。此端点在 APIGEE 中发布。当我尝试访问此端点时,总是收到错误消息“400 Bad Request,No required SSL certificate was sent”。任何人请帮助我?
请找到我遵循的以下步骤。
- 获得客户端证书+私钥。这也是由服务器授权的。即,能够在邮递员中使用客户端证书和私钥获得有效响应。
- 但是当我尝试通过 OAuth2RestTemplate 访问时,总是出现上述错误。
这是示例代码:
public OAuth2RestTemplate oAuth2RestTemplate() {
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
resourceDetails.setAccessTokenUri("<>/oauth2/v1/token");
resourceDetails.setClientId("client");
resourceDetails.setClientSecret("secret");
resourceDetails.setGrantType("client_credentials");
OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
oAuth2RestTemplate.setMessageConverters(asList(new MappingJackson2HttpMessageConverter()));
KeyStore keyStore;
HttpComponentsClientHttpRequestFactory requestFactory = null;
try {
// load keystore
keyStore = KeyStore.getInstance("JKS"); // keyStore = KeyStore.getInstance("PKCS12");
InputStream inputStream = classPathResource.getInputStream();
File file = new File("client.jks"); //File file = new File("client.p12");
InputStream inputStream = new FileInputStream(file);
keyStore.load(inputStream, "client".toCharArray());
// create SSLContextBuilder using keystore
SSLContext sslContext = new SSLContextBuilder()
.setProtocol("TLSv1.2")
.loadTrustMaterial(null, new TrustAllStrategy())
.loadKeyMaterial(keyStore, "password".toCharArray(), (map, socket) -> "1").build();
// create SSLConnectionSocketFactory using SSLContextBuilder
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);
// create HTTP client using SSLConnectionSocketFactory
HttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(socketFactory)
.setMaxConnTotal(5)
.setMaxConnPerRoute(5)
.build();
// create HttpComponentsClientHttpRequestFactory using httpclient
requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setReadTimeout(10000);
requestFactory.setConnectTimeout(10000);
// set requestFactory in Resttemplate
oAuth2RestTemplate.setRequestFactory(requestFactory);
} catch (Exception e) {
System.out.println("Error occurred while creating Resttemplate with JKS");
e.printStackTrace();
}
return oAuth2RestTemplate;
}```
Can any one please help me? I tried with both jks and p12 format