Skip to content

Instantly share code, notes, and snippets.

@msomu
Created October 6, 2014 16:59
Show Gist options
  • Save msomu/5d6d0d0dbd6b7021beb0 to your computer and use it in GitHub Desktop.
Save msomu/5d6d0d0dbd6b7021beb0 to your computer and use it in GitHub Desktop.
Java Authentication for urls protected with User Name and Passwords
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());
}
}
class ClasName{
Authenticator.setDefault(new CustomAuthenticator());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment