Open In App
Related Articles

Java.net.Authenticator class in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Authenticator class is used in those cases where an authentication is required to visit some URL. Once it is known that authentication is required, it prompts the user for the same or uses some hard-coded username and password. 
To use this class, following steps are followed- 
 


  1. Create a class that extends the Authenticator. Lets name it customAuth.
  2. Override the getPasswordAuthentication() method. This method contains several methods for getting the details of the entity requesting for authentication. All those methods are discussed in detail later.
  3. Set the newly created subclass as the default authenticator to be used when a http server asks for authentication, with setDefault(Authenticator a) method of Authenticator class.
    1. setDefault(Authenticator a) : Sets the authenticator to be used when a HTTP server requires authentication. 
       
Syntax : public static void setDefault(Authenticator a)
                 throws SecurityException
Parameter :
a : authenticator to be set as default
Throws :
SecurityException : if security manager doesn't allow 
setting default authenticator

  1.  
  2. requestPasswordAuthentication() : Asks the authenticator registered with the system for password. Returns username/password or null if not found.
     
Syntax : 
public static PasswordAuthentication requestPasswordAuthentication(
                                                   InetAddress addr,
                                                   int port,
                                                   String protocol,
                                                   String prompt,
                                                   String scheme)
Parameter :
addr : Inet address of the site asking for authentication
port : port of requesting site
protocol : protocol used for connection
prompt : message for the user
scheme : authentication scheme
Throws :
SecurityException : if security manager doesn't allow
setting password authentication.

  1. Another overloaded method which can be used in situations where hostname can be used if inetaddress is not available. 
     
Syntax : 
public static PasswordAuthentication requestPasswordAuthentication(
                                                   String host,
                                                   InetAddress addr,
                                                   int port,
                                                   String protocol,
                                                   String prompt,
                                                   String scheme)
Parameter :
host : hostname of the site asking for authentication
addr : Inet address of the site asking for authentication
port : port of requesting site
protocol : protocol used for connection
prompt : message for the user
scheme : authentication scheme
Throws :
SecurityException : if security manager doesn't allow 
setting password authentication.

  1. Another overloaded method which can be used if URL of the site requesting authentication is only known and not inetaddress and hostname. 
     
Syntax : 
public static PasswordAuthentication requestPasswordAuthentication(
                                                   String host,
                                                   InetAddress addr,
                                                   int port,
                                                   String protocol,
                                                   String prompt,
                                                   URL url,
                                                   String scheme)
Parameter :
host : hostname of the site asking for authentication
addr : Inet address of the site asking for authentication
port : port of requesting site
protocol : protocol used for connection
prompt : message for the user
url : URL of the site requesting authentication
scheme : authentication scheme
Throws :
SecurityException : if security manager doesn't allow 
setting password authentication.

  1.  
  2. getRequestingHost() : returns the hostname of the site requesting authentication. 
     
Syntax : protected final String getRequestingHost()

  1.  
  2. getRequestingSite() : returns the inetaddress of the site requesting authentication. 
     
Syntax : protected final InetAddress getRequestingSite()

  1.  
  2. getRequestingPort() : returns the port of connection. 
     
Syntax : protected final int getRequestingPort()

  1.  
  2. getRequestingProtocol() : returns the protocol requesting the connection. 
     
Syntax : protected final String getRequestingProtocol()

  1.  
  2. getRequestingPrompt() : returns the message prompted by requester. 
     
Syntax : protected final String getRequestingPrompt()

  1.  
  2. getRequestingScheme() : returns the scheme of the of requesting site. 
     
Syntax : protected final String getRequestingScheme()

  1.  
  2. getPasswordAuthentication() : this method is called when password authentication is required. All subclasses must override this method as default method always returns null. 
     
Syntax : protected PasswordAuthentication getPasswordAuthentication()

  1.  
  2. getRequestingURL() : returns the url of the requester. 
     
Syntax : protected final URL getRequestingURL()

  1.  
  2. getRequestorType() : returns if the requestor is proxy or server. 
     
Syntax : protected Authenticator.RequestorType getRequestorType()

  1.  

Last Updated : 12 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads