Created
July 13, 2025 22:31
-
-
Save amir-khassaia/8de21e0f1383d23374cfdd856e97ca38 to your computer and use it in GitHub Desktop.
Jersey 2 client SSL Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main; | |
| import javax.net.ssl.HttpsURLConnection; | |
| import javax.net.ssl.SSLContext; | |
| import javax.net.ssl.SSLSocketFactory; | |
| import javax.net.ssl.TrustManager; | |
| import javax.net.ssl.X509TrustManager; | |
| import jakarta.ws.rs.client.Client; | |
| import jakarta.ws.rs.client.ClientBuilder; | |
| import jakarta.ws.rs.core.Response; | |
| import java.security.SecureRandom; | |
| import java.security.cert.X509Certificate; | |
| import java.util.Date; | |
| import java.util.concurrent.Executors; | |
| import java.util.concurrent.ScheduledExecutorService; | |
| import java.util.concurrent.TimeUnit; | |
| public class JerseySSLTester { | |
| private static final String TARGET_URL = "https://192.168.1.205/"; // Replace with your test endpoint | |
| public static void main(String[] args) throws Exception { | |
| // Create a trust manager that does not validate certificate chains | |
| TrustManager[] trustAllCerts = new TrustManager[] { | |
| new X509TrustManager() { | |
| @Override | |
| public X509Certificate[] getAcceptedIssuers() { | |
| return new X509Certificate[0]; | |
| } | |
| @Override | |
| public void checkClientTrusted(X509Certificate[] certs, String authType) { | |
| } | |
| @Override | |
| public void checkServerTrusted(X509Certificate[] certs, String authType) { | |
| } | |
| } | |
| }; | |
| // Create an SSL context that uses the trust-all manager | |
| SSLContext sslContext = SSLContext.getInstance("TLS"); | |
| sslContext.init(null, trustAllCerts, new SecureRandom()); | |
| // Build the Jersey client with the custom SSL context and hostname verifier | |
| Client client = ClientBuilder.newBuilder() | |
| .sslContext(sslContext) | |
| .hostnameVerifier((hostname, session) -> true) | |
| .build(); | |
| // Schedule periodic requests every 2 seconds | |
| ScheduledExecutorService executor = Executors.newScheduledThreadPool(4); | |
| executor.scheduleAtFixedRate(() -> { | |
| try { | |
| try (Response response = client.target(TARGET_URL) | |
| .request() | |
| .get()) { | |
| System.out.println("Response Status: " + response.getStatus() + " at " + new Date(System.currentTimeMillis())); | |
| } | |
| } catch (Exception e) { | |
| System.err.println("Request failed: " + e.getMessage()); | |
| } | |
| }, 0, 2, TimeUnit.SECONDS); | |
| // Schedule a task to reset the default SSLSocketFactory | |
| executor.schedule(() -> { | |
| try { | |
| SSLContext defaultContext = SSLContext.getInstance("TLS"); | |
| defaultContext.init(null, null, null); // Initialize with default trust managers | |
| SSLSocketFactory defaultFactory = defaultContext.getSocketFactory(); | |
| HttpsURLConnection.setDefaultSSLSocketFactory(defaultFactory); | |
| HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> false); | |
| System.out.println("Default SSLSocketFactory reset at " + new Date(System.currentTimeMillis())); | |
| } catch (Exception e) { | |
| System.err.println("Failed to reset default SSLSocketFactory: " + e.getMessage()); | |
| } | |
| }, 15, TimeUnit.SECONDS); | |
| // Keep the program running for a reasonable duration | |
| Thread.sleep(30000); | |
| executor.shutdown(); | |
| client.close(); | |
| System.out.println("Tests Completed."); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment