Package de.stefanfrings.utils
Class ThreadLocalAuthenticator
- java.lang.Object
-
- java.net.Authenticator
-
- de.stefanfrings.utils.ThreadLocalAuthenticator
-
public class ThreadLocalAuthenticator extends Authenticator
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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ThreadLocalAuthenticator
getInstance()
Get the single instance of this class.PasswordAuthentication
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.static void
setAsDefault()
Set this Authenticator as default.static void
setProxyAuth(String username, String password)
Set credentials for proxy server authentication.static void
setServerAuth(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 Detail
-
setServerAuth
public static void setServerAuth(String username, String password)
Set credentials for web server authentication.- Parameters:
username
- Usernamepassword
- Password
-
setProxyAuth
public static void setProxyAuth(String username, String password)
Set credentials for proxy server authentication.- Parameters:
username
- Usernamepassword
- Password
-
getInstance
public static ThreadLocalAuthenticator getInstance()
Get the single instance of this class.- Returns:
- The ThreadLocalAuthenticator
-
setAsDefault
public static void setAsDefault()
Set this Authenticator as default.
-
getPasswordAuthentication
public PasswordAuthentication 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:
getPasswordAuthentication
in 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)
-
-