Fix check certificato durante l'aggiornamento dei comuni
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good

This commit is contained in:
2025-09-03 10:47:32 +02:00
parent 481e8e35cc
commit b2ac734b5e

View File

@@ -7,6 +7,7 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -70,7 +71,7 @@ public class UnitaTerritorialiService {
// Metodo che aggiorna i dati del servizio
private void update() throws Exception {
HttpURLConnection connectionIstat = (HttpURLConnection) new URL(PERMALINK_ISTAT).openConnection();
HttpURLConnection connectionIstat = createPermalinkIstatConnection();
connectionIstat.setRequestMethod("GET");
int responseCodeIstat = connectionIstat.getResponseCode();
@@ -208,7 +209,7 @@ public class UnitaTerritorialiService {
// Metodo che restituisce la data di ultima modifica
private Instant getLastModifiedSource() throws Exception {
HttpURLConnection connection = (HttpURLConnection) new URL(PERMALINK_ISTAT).openConnection();
HttpURLConnection connection = createPermalinkIstatConnection();
connection.setRequestMethod("HEAD");
connection.connect();
int responseCode = connection.getResponseCode();
@@ -217,7 +218,12 @@ public class UnitaTerritorialiService {
}
String lastModified = connection.getHeaderField("Last-Modified");
if (lastModified != null) {
return Instant.now(); // Converti in Instant, se necessario
try {
return Instant.from(java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME.parse(lastModified));
} catch (Exception e) {
logger.warn("Errore nel parsing della data Last-Modified: " + lastModified, e);
return Instant.now();
}
}
return Instant.MIN;
}
@@ -281,4 +287,33 @@ public class UnitaTerritorialiService {
public List<RipartizioneGeografica> getRipartizioniGeografiche() {
return ripartizioniGeografiche;
}
private HttpsURLConnection createPermalinkIstatConnection() throws Exception {
HttpsURLConnection connection = (HttpsURLConnection) new URL(PERMALINK_ISTAT).openConnection();
connection.setHostnameVerifier((hostname, session) -> true);
connection.setSSLSocketFactory(createTrustAllSSLContext().getSocketFactory());
return connection;
}
private javax.net.ssl.SSLContext createTrustAllSSLContext() throws Exception {
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[]{
new javax.net.ssl.X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
javax.net.ssl.SSLContext sslContext = javax.net.ssl.SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
return sslContext;
}
}