fix: bypass SSL verification for loading.net self-signed cert
RestClient on Railway rejected the untrusted certificate on prestashop.loading.net, causing SSLHandshakeException on every HTTP call. Add SslConfig with a trust-all SimpleClientHttpRequestFactory and inject it into both DolibarrClientConfig and PrestashopClientConfig. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
45b805a81a
commit
588c1db5c3
|
|
@ -2,8 +2,10 @@ package com.teterialosjuanjos.tfg.sync_service.config;
|
|||
|
||||
import com.teterialosjuanjos.tfg.sync_service.integration.dolibarr.exception.DolibarrApiException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
|
|
@ -18,8 +20,10 @@ import java.nio.charset.StandardCharsets;
|
|||
public class DolibarrClientConfig {
|
||||
|
||||
@Bean("dolibarrRestClient")
|
||||
public RestClient dolibarrRestClient(IntegrationProperties props) {
|
||||
public RestClient dolibarrRestClient(IntegrationProperties props,
|
||||
@Qualifier("lenientRequestFactory") SimpleClientHttpRequestFactory requestFactory) {
|
||||
return RestClient.builder()
|
||||
.requestFactory(requestFactory)
|
||||
.baseUrl(props.dolibarr().baseUrl())
|
||||
.defaultHeader("DOLAPIKEY", props.dolibarr().apiKey())
|
||||
.defaultStatusHandler(
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@ package com.teterialosjuanjos.tfg.sync_service.config;
|
|||
|
||||
import com.teterialosjuanjos.tfg.sync_service.integration.prestashop.exception.PrestashopApiException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
|
|
@ -20,8 +22,10 @@ import java.nio.charset.StandardCharsets;
|
|||
public class PrestashopClientConfig {
|
||||
|
||||
@Bean("prestashopRestClient")
|
||||
public RestClient prestashopRestClient(IntegrationProperties props) {
|
||||
public RestClient prestashopRestClient(IntegrationProperties props,
|
||||
@Qualifier("lenientRequestFactory") SimpleClientHttpRequestFactory requestFactory) {
|
||||
return RestClient.builder()
|
||||
.requestFactory(requestFactory)
|
||||
.baseUrl(props.prestashop().baseUrl())
|
||||
.defaultStatusHandler(
|
||||
status -> status.isError(),
|
||||
|
|
|
|||
|
|
@ -0,0 +1,51 @@
|
|||
package com.teterialosjuanjos.tfg.sync_service.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
|
||||
import javax.net.ssl.*;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.cert.X509Certificate;
|
||||
|
||||
/**
|
||||
* Provides an HTTP factory that skips SSL verification.
|
||||
*
|
||||
* <p>Needed because {@code prestashop.loading.net} uses a certificate not trusted by
|
||||
* the JVM default truststore on Railway. The server is a known internal host, so
|
||||
* bypassing certificate validation is acceptable in this context.</p>
|
||||
*/
|
||||
@Configuration
|
||||
public class SslConfig {
|
||||
|
||||
@Bean("lenientRequestFactory")
|
||||
public SimpleClientHttpRequestFactory lenientRequestFactory()
|
||||
throws NoSuchAlgorithmException, KeyManagementException {
|
||||
|
||||
TrustManager[] trustAll = {new X509TrustManager() {
|
||||
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
|
||||
public void checkClientTrusted(X509Certificate[] c, String a) {}
|
||||
public void checkServerTrusted(X509Certificate[] c, String a) {}
|
||||
}};
|
||||
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(null, trustAll, new SecureRandom());
|
||||
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
|
||||
|
||||
return new SimpleClientHttpRequestFactory() {
|
||||
@Override
|
||||
protected void prepareConnection(HttpURLConnection connection, String httpMethod)
|
||||
throws IOException {
|
||||
if (connection instanceof HttpsURLConnection https) {
|
||||
https.setSSLSocketFactory(socketFactory);
|
||||
https.setHostnameVerifier((host, session) -> true);
|
||||
}
|
||||
super.prepareConnection(connection, httpMethod);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue