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
  • Method Details

    • setServerAuth

      public static void setServerAuth(String username, String password)
      Set credentials for web server authentication.
      Parameters:
      username - Username
      password - Password
    • setProxyAuth

      public static void setProxyAuth(String username, String password)
      Set credentials for proxy server authentication.
      Parameters:
      username - Username
      password - 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 class Authenticator
      Returns:
      An authentication with username and password, as previously set by setProxyAuth(java.lang.String, java.lang.String) and/or setServerAuth(java.lang.String, java.lang.String)