Created
September 26, 2019 00:35
-
-
Save hakanai/d65c6ee361411c551d7939e7b9fb736e to your computer and use it in GitHub Desktop.
Demonstration of Java's HTTP client caching credentials when you don't want it
This file contains 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
import java.net.Authenticator; | |
import java.net.HttpURLConnection; | |
import java.net.PasswordAuthentication; | |
import java.net.URL; | |
public class AuthenticationLeakBugDemo | |
{ | |
public static void main(String[] args) throws Exception | |
{ | |
// User 1 | |
{ | |
System.out.println("Simulating user 1 coming in with proper authentication."); | |
System.out.println("Expecting: 200"); | |
Authenticator.setDefault(new Authenticator() | |
{ | |
@Override | |
protected PasswordAuthentication getPasswordAuthentication() | |
{ | |
return new PasswordAuthentication("a", "a".toCharArray()); | |
} | |
}); | |
try | |
{ | |
URL url = new URL("http://httpbin.org/basic-auth/a/a"); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
System.out.println("Got: " + connection.getResponseCode()); | |
} | |
finally | |
{ | |
Authenticator.setDefault(null); | |
} | |
} | |
// User 2 | |
{ | |
System.out.println("Simulating user 2 coming in with no authentication."); | |
System.out.println("Expecting: 401"); | |
URL url = new URL("http://httpbin.org/basic-auth/a/a"); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
System.out.println("Got: " + connection.getResponseCode()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment