Created
October 6, 2014 16:59
-
-
Save msomu/5d6d0d0dbd6b7021beb0 to your computer and use it in GitHub Desktop.
Java Authentication for urls protected with User Name and Passwords
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
public static class CustomAuthenticator extends Authenticator { | |
// Called when password authorization is needed | |
protected PasswordAuthentication getPasswordAuthentication() { | |
// Get information about the request | |
String prompt = getRequestingPrompt(); | |
String hostname = getRequestingHost(); | |
InetAddress ipaddr = getRequestingSite(); | |
int port = getRequestingPort(); | |
String username = "username"; | |
String password = "pass"; | |
// Return the information (a data holder that is used by Authenticator) | |
return new PasswordAuthentication(username, password.toCharArray()); | |
} | |
} |
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
class ClasName{ | |
Authenticator.setDefault(new CustomAuthenticator()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment