Module org.htmlunit
Package org.htmlunit

Class DefaultCredentialsProvider

java.lang.Object
org.htmlunit.DefaultCredentialsProvider
All Implemented Interfaces:
Serializable, org.apache.http.client.CredentialsProvider

public class DefaultCredentialsProvider extends Object implements org.apache.http.client.CredentialsProvider, Serializable
Default HtmlUnit implementation of the CredentialsProvider interface. Provides credentials for both web servers and proxies. Supports Digest authentication, Socks authentication and Basic HTTP authentication.
Author:
Daniel Gredler, Vikram Shitole, Marc Guillemot, Ahmed Ashour, Nicolas Belisle, Ronald Brill
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The null value represents any host.
    static final int
    The -1 value represents any port.
    static final String
    The null value represents any realm.
    static final String
    The null value represents any authentication scheme.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addCredentials(String username, char[] password)
    Adds credentials for the specified username/password for any host/port/realm combination.
    void
    addCredentials(String username, char[] password, String host, int port, String realm)
    Adds credentials for the specified username/password on the specified host/port for the specified realm.
    void
    addNTLMCredentials(String username, char[] password, String host, int port, String workstation, String domain)
    Adds NTLM credentials for the specified username/password on the specified host/port.
    void
    addSocksCredentials(String username, char[] password, String host, int port)
    Adds Socks credentials for the specified username/password on the specified host/port.
    void
    org.apache.http.auth.Credentials
    getCredentials(org.apache.http.auth.AuthScope authscope)
    boolean
    removeCredentials(org.apache.http.auth.AuthScope authscope)
    Removes the credentials that best match the given AuthScope.
    void
    setCredentials(org.apache.http.auth.AuthScope authscope, org.apache.http.auth.Credentials credentials)

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • ANY_HOST

      public static final String ANY_HOST
      The null value represents any host.
    • ANY_PORT

      public static final int ANY_PORT
      The -1 value represents any port.
      See Also:
    • ANY_REALM

      public static final String ANY_REALM
      The null value represents any realm.
    • ANY_SCHEME

      public static final String ANY_SCHEME
      The null value represents any authentication scheme.
  • Constructor Details

    • DefaultCredentialsProvider

      public DefaultCredentialsProvider()
  • Method Details

    • addCredentials

      public void addCredentials(String username, char[] password)
      Adds credentials for the specified username/password for any host/port/realm combination. The credentials may be for any authentication scheme, including NTLM, digest and basic HTTP authentication. If you are using sensitive username/password information, please do NOT use this method. If you add credentials using this method, any server that requires authentication may receive the specified username and password.
      Parameters:
      username - the username for the new credentials
      password - the password for the new credentials
      See Also:
    • addCredentials

      public void addCredentials(String username, char[] password, String host, int port, String realm)
      Adds credentials for the specified username/password on the specified host/port for the specified realm. The credentials may be for any authentication scheme, including NTLM, digest and basic HTTP authentication.
      Parameters:
      username - the username for the new credentials
      password - the password for the new credentials
      host - the host to which to the new credentials apply (null if applicable to any host)
      port - the port to which to the new credentials apply (negative if applicable to any port)
      realm - the realm to which to the new credentials apply (null if applicable to any realm)
    • addNTLMCredentials

      public void addNTLMCredentials(String username, char[] password, String host, int port, String workstation, String domain)
      Adds NTLM credentials for the specified username/password on the specified host/port.

      Note: due to a limitation of the underlying NTCredentials class, the password is converted to a String internally and cannot be zeroed from memory after use.

      Parameters:
      username - the username for the new credentials; should not include the domain to authenticate with; for example: "user" is correct whereas "DOMAIN\\user" is not
      password - the password for the new credentials
      host - the host to which to the new credentials apply (null if applicable to any host)
      port - the port to which to the new credentials apply (negative if applicable to any port)
      workstation - the workstation the authentication request is originating from; essentially, the computer name for this machine
      domain - the domain to authenticate within
    • addSocksCredentials

      public void addSocksCredentials(String username, char[] password, String host, int port)
      Adds Socks credentials for the specified username/password on the specified host/port.

      Note: the SOCKS authenticator is a JVM-wide singleton. If multiple DefaultCredentialsProvider instances add SOCKS credentials, only the first instance's credentials will be used for SOCKS authentication.

      Parameters:
      username - the username for the new credentials
      password - the password for the new credentials
      host - the host to which to the new credentials apply (null if applicable to any host)
      port - the port to which to the new credentials apply (negative if applicable to any port)
    • setCredentials

      public void setCredentials(org.apache.http.auth.AuthScope authscope, org.apache.http.auth.Credentials credentials)
      Specified by:
      setCredentials in interface org.apache.http.client.CredentialsProvider
      Throws:
      IllegalArgumentException - if authscope is null, if credentials is null, or if the credentials type is not one of UsernamePasswordCredentials, HtmlUnitUsernamePasswordCredentials, or NTCredentials
    • getCredentials

      public org.apache.http.auth.Credentials getCredentials(org.apache.http.auth.AuthScope authscope)
      Specified by:
      getCredentials in interface org.apache.http.client.CredentialsProvider
    • removeCredentials

      public boolean removeCredentials(org.apache.http.auth.AuthScope authscope)
      Removes the credentials that best match the given AuthScope. An exact scope match is preferred over a fuzzy best-match, consistent with the resolution strategy used by getCredentials(AuthScope).
      Parameters:
      authscope - the AuthScope whose credentials should be removed; must not be null
      Returns:
      true if credentials were removed; false if no match was found
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • clear

      public void clear()
      Specified by:
      clear in interface org.apache.http.client.CredentialsProvider