Springboot SLL API消耗

时间:2019-04-01 10:44:36

标签: java api spring-boot ssl sslhandshakeexception

我正在尝试从第三方服务器使用API​​。第三方发送给我一个名为SSL.p12的SSL证书,这是我用来进行握手的证书文件。我使用SSL创建了一个自定义RestTemplate,如下所示:

@Configuration
public class CustomRestTemplate {
    private static final String PASSWORD = "fake_password";
    private static final String RESOURCE_PATH = "keystore/certificate.p12";
    private static final String KEY_TYPE = "PKCS12";

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception {
        char[] password = PASSWORD.toCharArray();

        SSLContext sslContext = SSLContextBuilder
                .create()
//                .loadKeyMaterial(keyStore(RESOURCE_PATH, password), password)
                .loadKeyMaterial(keyStore(getClass().getClassLoader().getResource(RESOURCE_PATH).getFile(), password), password)
                .loadTrustMaterial(null, new TrustSelfSignedStrategy())
                .build();

        HttpClient client = HttpClients
                .custom()
                .setSSLContext(sslContext)
                .build();

        return builder
                .requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client))
                .build();
    }

    private KeyStore keyStore(String file, char[] password) throws Exception {
        KeyStore keyStore = KeyStore.getInstance(KEY_TYPE);

        File key = ResourceUtils.getFile(file);
        try (InputStream in = new FileInputStream(key)) {
            keyStore.load(in, password);
        }

        return keyStore;
    }
}

然后我使用以下代码调用端点:

@Component
@Service
public class TransactionService implements TransactionInterface {
    @Autowired
    private CustomRestTemplate restTemplate = new CustomRestTemplate();

    private static final String BASE_URL = "https://41.x.x.x:xxxx/";

    @Override
    public List<Transaction> getUnsentTransactions(int connectionId) throws Exception {
        HttpEntity<?> httpEntity = new HttpEntity<>(null, new HttpHeaders());

        ResponseEntity<Transaction[]> resp = restTemplate
            .restTemplate(new RestTemplateBuilder())
            .exchange(BASE_URL + "path/end_point/" + connectionId, HttpMethod.GET, httpEntity, Transaction[].class);

        return Arrays.asList(resp.getBody());
    }
}

当尝试使用api时,得到以下堆栈跟踪:

org.springframework.web.client.ResourceAccessException: I/O error on GET request for \"https://41.x.x.x:xxxx/path/endpoint/parameters\": Certificate for <41.x.x.x> doesn't match any of the subject alternative names: [some_name_here, some_name_here];

我对TLS或SSL证书没有太多经验。我现在真的很困,希望能在这里得到一些帮助。第三方为我提供了一个测试站点,可以在其中测试端点,并将certificate.p12文件导入浏览器后,我可以使用它们的测试站点到达端点,但是我的Springboot应用程序仍然无法到达端点。

我需要将证书复制到特定文件夹吗?似乎情况并非如此,因为如果更改路径或文件名,则会收到FileNotFoundException;如果为certificate.p12文件输入错误的密码,则会收到密码错误的错误。我尝试使用Postman对其进行测试,但Postman返回与我的Web应用程序相同的stacktrace。

看看上面的信息,我有什么遗漏吗?是否在运行时未创建密钥库?我需要将证书绑定到JVM还是我的传出请求?

任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试连接到证书中没有有效名称的服务器。例如,如果您要连接到“ stackoverflow.com”,则证书需要在“主题”或“主题替代名称”字段中使用该域。

即使测试站点也应该具有有效的证书,但是如果这不可能(因为它是第三方站点并且您不能自己更改),则可以使用this question

禁用验证

当然,这仅应用于测试。