Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java Authenticator

Introduction to Java Authenticator

The java Authenticator Class is used to perform authentication for network connection. The java authenticator class is a built-in class in java. The authenticator class is used in an application where authentication is required to visit some URLs for the users. An authenticator class performs authentication by prompting the user for credential information like username and password.

To perform authentication in an application, the application needs to perform some steps, first, an application class is to extend the java.net.Authenticator class and override the getPasswordAuthentication() function of java.net.Authenticator class in the subclass. Usually, this function contains getXXX() functions to get information from users for authentication like username and password. The getPasswordAuthentication() function return PasswordAuthentication value which contain credentials information.

The syntax of the Authenticator class in java is –

publicabstract class Authenticator extends Object
{
// code of the authenticator class
}

Authenticator Class Member Functions

Let’s see the list of all Authenticator class member functions, which can be used for the specific purpose for an Authentication

1. getPasswordAuthentication() – This function is used when password authorization is required. All subclasses should override getPasswordAuthentication () as default and this function always returns null.

Syntax –

protected PasswordAuthentication getPasswordAuthentication();

2. getRequestingHost() – This function is used to get the hostname of the site requesting authentication, return null if no hostname present.

Syntax –

protected final String getRequestingHost();

3. getRequestingPort() – This function is used to get port number of requesting connection.

Syntax –

protected final int getRequestingPort();

4. getRequestingPrompt() – This function is used to prompt a string message provided by the requestor.

Syntax –

protected final String getRequestingPrompt();

5. getRequestingProtocol() – This function is used to get the protocol that requesting the connection.

Syntax –

protected final int getRequestingProtocol();

6. getRequestingScheme() – This function is used to get off the scheme of the requestor site.

Syntax –

protected final String getRequestingScheme();

7. getRequestingSite() – This function is used to get InetAddress of the requesting site, return null if no InetAddress present.

Syntax –

protected final InetAddress getRequestingSite();

8. getRequestingURL() – This function is used to get the URL of the requester.

Syntax –

protected URL getRequestingURL();

9. setDefault(Authenticator a) – This function is used to sets the authenticator to be used by networking when the HTTP server requires authentication.

Syntax –

public static void setDefault(Authenticator a);

10. getRequestorType() – This function is used to get whether the requestor is a server or a Proxy.

Syntax –

protected Authenticator.RequestorType getRequestorType();

Examples for the Authenticator Class in Java

Next, we write the java code to understand the Authenticator class more clearly with the following example where we create a subclass of Authenticator class and override getPasswordAuthentication() function to perform authentication for some URL, as below –

Example #1

Code:

package p1;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URL;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.MalformedURLException;
class MyAuthenticatorclass extends Authenticator
{
protected PasswordAuthentication getPasswordAuthentication()
{
String username = "John", password = "pass123";
this.print();
return new PasswordAuthentication(username, password.toCharArray());
}
void print()
{
int hostname = getRequestingPort();
System.out.println("The request Port number :" + hostname);
}
}
public class Demo
{
public static void main(String[] arg)
{   String data;
try {
//create object of authenticator class
MyAuthenticatorclass obj =new MyAuthenticatorclass();
Authenticator.setDefault(new MyAuthenticatorclass());
URL url = new URL("https://www.educba.com/");
// reads data from the url in html form
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println("The requesting URL is : "+url.getHost());
obj.getPasswordAuthentication() ;
while ((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
} catch (MalformedURLException e) {
System.out.println("Malformed URL Exception : " + e);
} catch (IOException e) {
System.out.println("IO Exception: " + e);
}
}
}

Output:

As in the above code, the subclass of authenticator class is created and overrides the getPasswordAuthentication() function, where the authentication details are provided and the function returns the PasswordAuthentication object. As in the program, the URL (https://www.educba.com/) is reading by performing an authentication, because in the code before reading from url obj.getPasswordAuthentication() is called to authenticate, if the obj.getPasswordAuthentication() returns a null value, then it generates the java.lang.NullPointerException and the program execution stop here only, which means not authenticated, otherwise, the program executes farther, which means authenticated

Next, we write the java code to understand the Authenticator class where we try to get all information in the overridden getPasswordAuthentication() function, as below –

Example #2

Code:

package p1;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URL;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.PasswordAuthentication;
import java.net.MalformedURLException;
class MyAuthenticatorclass extends Authenticator
{
protected PasswordAuthentication getPasswordAuthentication()
{
String username = "John", password = "pass123";
this.print();
return new PasswordAuthentication(username, password.toCharArray());
}
void print()
{
// get all information
String prom = getRequestingPrompt();
URL url = getRequestingURL();
int prt = getRequestingPort();
String hostname = getRequestingHost();
InetAddress ipaddress = getRequestingSite();
String sh = getRequestingScheme();
RequestorType rt = getRequestorType();
String protocol = getRequestingProtocol();
System.out.println("The prompt is :" + prom+"\nThe hostname is :" + hostname+"\nThe ipaddress is :" + ipaddress+"\nThe port no is :" + prt);
System.out.println("The protocol is :" + protocol+"\nThe scheme is :" + sh+"\nThe URL is :" + url+"\nThe Requester Type is :" + rt);
}
}
public class Demo
{
public static void main(String[] arg)
{   String data;
try {
//create object of authenticator class
MyAuthenticatorclass obj =new MyAuthenticatorclass();
Authenticator.setDefault(new MyAuthenticatorclass());
URL url = new URL("https://www.educba.com/");
// reads data from the url in html form
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println("The requesting URL is : "+url.getHost());
obj.getPasswordAuthentication() ;
while ((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
} catch (MalformedURLException e) {
System.out.println("Malformed URL Exception : " + e);
} catch (IOException e) {
System.out.println("IO Exception: " + e);
}
}
}

Output:

As in the above code is trying to get all information related to authentication.

Conclusion

The java authenticator class is a built-in class in java that is used to perform authentication for URL or network connection. This built-in class having ten function which can be used for the specific purpose as we have seen above.

Recommended Articles

This is a guide to Java Authenticator. Here we discuss the functions and examples for the Authenticator Class in Java along with the syntax. You can also go through our other suggested articles to learn more –

  1. repaint in Java
  2. Java TimeZone
  3. Java Directories
  4. ThreadGroup in Java

The post Java Authenticator appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

Java Authenticator

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×