Java.net.Authenticator class in Java

12 Jan, 2022
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-  
Create a class that extends the Authenticator. Lets name it customAuth.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.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.setDefault(Authenticator a) : Sets the authenticator to be used when a HTTP server requires authentication.  
Create a class that extends the Authenticator. Lets name it customAuth.
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.
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.setDefault(Authenticator a) : Sets the authenticator to be used when a HTTP server requires authentication.  
setDefault(Authenticator a) : Sets the authenticator to be used when a HTTP server requires authentication.  
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
 requestPasswordAuthentication() : Asks the authenticator registered with the system for password. Returns username/password or null if not found. 
 
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.
Another overloaded method which can be used in situations where hostname can be used if inetaddress is not available.  
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.
Another overloaded method which can be used if URL of the site requesting authentication is only known and not inetaddress and hostname.  
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.
 getRequestingHost() : returns the hostname of the site requesting authentication.  
 
getRequestingHost() : returns the hostname of the site requesting authentication.  
Syntax : protected final String getRequestingHost()
 getRequestingSite() : returns the inetaddress of the site requesting authentication.  
 
getRequestingSite() : returns the inetaddress of the site requesting authentication.  
Syntax : protected final InetAddress getRequestingSite()
 getRequestingPort() : returns the port of connection.  
 
getRequestingPort() : returns the port of connection.  
Syntax : protected final int getRequestingPort()
 getRequestingProtocol() : returns the protocol requesting the connection.  
 
getRequestingProtocol() : returns the protocol requesting the connection.  
Syntax : protected final String getRequestingProtocol()
 getRequestingPrompt() : returns the message prompted by requester.  
 
getRequestingPrompt() : returns the message prompted by requester.  
Syntax : protected final String getRequestingPrompt()
 getRequestingScheme() : returns the scheme of the of requesting site.  
 
getRequestingScheme() : returns the scheme of the of requesting site.  
Syntax : protected final String getRequestingScheme()
 getPasswordAuthentication() : this method is called when password authentication is required. All subclasses must override this method as default method always returns null.  
 
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()
 getRequestingURL() : returns the url of the requester.  
 
getRequestingURL() : returns the url of the requester.  
Syntax : protected final URL getRequestingURL()
 getRequestorType() : returns if the requestor is proxy or server.  
 
getRequestorType() : returns if the requestor is proxy or server.  
Syntax : protected Authenticator.RequestorType getRequestorType()
 
 
sumitgumber28
Java-Networking
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java