访问受 OAuth2 和 mTLS 保护的 HTTPS 端点

问题描述

我们有一个用例,需要访问受 OAuth2 + mTLS 保护的 HTTPS 端点。此端点在 APIGEE 中发布。当我尝试访问此端点时,总是收到错误消息“400 错误请求,未发送所需的 SSL 证书”。任何人请帮助我?

请找到我遵循的以下步骤。

  1. 获得客户端证书 + 私钥。这也是由服务器授权的。即,能够在 postman 中使用客户端证书和私钥获得有效响应。
  2. 但是当我尝试通过 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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...