Merge pull request #15678 from anujgaud/add-ssl-debug-logs
[BAEL-6064] Add code for enabling SSL Debug Logs
This commit is contained in:
commit
8fa22f186f
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
java.util.logging.ConsoleHandler.level=ALL
|
||||
javax.net.ssl.handlers=java.util.logging.ConsoleHandler
|
||||
javax.net.ssl.level=ALL
|
Loading…
Reference in New Issue