Merge pull request #15678 from anujgaud/add-ssl-debug-logs

[BAEL-6064] Add code for enabling SSL Debug Logs
This commit is contained in:
Andrea Giulio Cerasoni 2024-01-20 12:13:39 +00:00 committed by GitHub
commit 8fa22f186f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package com.baeldung.enablessldebug;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SSLDebugLogger {
private static final Logger logger = LoggerFactory.getLogger(SSLDebugLogger.class);
public static void enableSSLDebugUsingSystemProperties() {
System.setProperty("javax.net.debug", "ssl");
}
public static void makeHttpsRequest() throws Exception {
String url = "https://github.com/eugenp/tutorials";
URL httpsUrl = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection) httpsUrl.openConnection();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
logger.info("Response from " + url + ":");
while ((line = reader.readLine()) != null) {
logger.info(line);
}
}
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.enablessldebug;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import org.junit.jupiter.api.Test;
public class SSLDebugLoggerUnitTest {
@Test
void givenSSLDebuggingEnabled_whenUsingSystemProperties_thenEnableSSLDebugLogging() throws Exception {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setErr(new PrintStream(outContent));
SSLDebugLogger.enableSSLDebugUsingSystemProperties();
assertEquals("ssl", System.getProperty("javax.net.debug"));
SSLDebugLogger.makeHttpsRequest();
assertTrue(outContent.toString().contains("javax.net.ssl|DEBUG|"));
outContent.reset();
System.clearProperty("javax.net.debug");
assertNull(System.getProperty("javax.net.debug"));
SSLDebugLogger.makeHttpsRequest();
assertEquals(outContent.toString(),"");
}
@Test
void givenSSLDebuggingEnabled_whenUsingConfigurationFile_thenEnableSSLDebugLogging() throws IOException {
InputStream configFile = SSLDebugLoggerUnitTest.class.getClassLoader().getResourceAsStream("logging.properties");
LogManager.getLogManager().readConfiguration(configFile);
Logger sslLogger = Logger.getLogger("javax.net.ssl");
ConsoleHandler consoleHandler = (ConsoleHandler) sslLogger.getHandlers()[0];
Level consoleHandlerLevel = consoleHandler.getLevel();
assertEquals(Level.ALL, consoleHandlerLevel, "SSL ConsoleHandler level should be ALL");
}
}

View File

@ -0,0 +1,3 @@
java.util.logging.ConsoleHandler.level=ALL
javax.net.ssl.handlers=java.util.logging.ConsoleHandler
javax.net.ssl.level=ALL