Package de.stefanfrings.utils
Class ThreadLocalAuthenticator
java.lang.Object
java.net.Authenticator
de.stefanfrings.utils.ThreadLocalAuthenticator
This is a thread local implementation of an Authenticator for java.net classes.
Each concurrent thread can use different credentials.
Example usage:
ThreadLocalAuthenticator.setAsDefault();
ThreadLocalAuthenticator.setServerAuth(username,password);
URL url=new URL("http://webmailer.mycompany.de");
InputStream stream=url.openConnection().getInputStream();
- Author:
- Stefan Frings, http://stefanfrings.de/javautils
-
Nested Class Summary
Nested classes/interfaces inherited from class java.net.Authenticator
Authenticator.RequestorType -
Method Summary
Modifier and TypeMethodDescriptionstatic ThreadLocalAuthenticatorGet the single instance of this class.This method is called by the java.net classes, when connecting to a URL while the affected web server or proxy requests username and password.static voidSet this Authenticator as default.static voidsetProxyAuth(String username, String password) Set credentials for proxy server authentication.static voidsetServerAuth(String username, String password) Set credentials for web server authentication.Methods inherited from class java.net.Authenticator
getDefault, getRequestingHost, getRequestingPort, getRequestingPrompt, getRequestingProtocol, getRequestingScheme, getRequestingSite, getRequestingURL, getRequestorType, requestPasswordAuthentication, requestPasswordAuthentication, requestPasswordAuthentication, requestPasswordAuthentication, requestPasswordAuthenticationInstance, setDefault
-
Method Details
-
setServerAuth
Set credentials for web server authentication.- Parameters:
username- Usernamepassword- Password
-
setProxyAuth
Set credentials for proxy server authentication.- Parameters:
username- Usernamepassword- Password
-
getInstance
Get the single instance of this class.- Returns:
- The ThreadLocalAuthenticator
-
setAsDefault
public static void setAsDefault()Set this Authenticator as default. -
getPasswordAuthentication
This method is called by the java.net classes, when connecting to a URL while the affected web server or proxy requests username and password.- Overrides:
getPasswordAuthenticationin classAuthenticator- Returns:
- An authentication with username and password, as previously set
by
setProxyAuth(java.lang.String, java.lang.String)and/orsetServerAuth(java.lang.String, java.lang.String)
-