Authenticator setDefault(Authenticator) in Java

setDefault(Authenticator): This method is available in the java.net.Authenticator class of Java.

Syntax:

void java.net.Authenticator.setDefault(Authenticator a)

This method takes one argument. This method sets the authenticator that will be used by the networking code when a proxy or an HTTP server asks for authentication.

Parameters: One parameter is required for this method.

a: The authenticator to be set. If a is null then any previously set authenticator is removed.

Returns: NA

Throws:

1. SecurityException - if a security manager exists and its checkPermission method doesn't allow setting the default authenticator.

Approach

Java

package com.Authenticator;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

public class AuthenticatorsetDefault {
    public static void main(String[] args) {

        ClassAuthenticator obj1 = new ClassAuthenticator();
        Authenticator.setDefault(obj1);
        System.out.println("Successfully set default");
    }

    public static class ClassAuthenticator extends Authenticator {
        protected PasswordAuthentication getPasswordAuthentication() {
            this.show();
            String username = "user";
            String password = "password";
            return new PasswordAuthentication(username, password.toCharArray());
        }

        void show() {
            int hostname = getRequestingPort();
            System.out.println("Port Requesting :" + hostname);
        }
    }
}

Output:

Successfully set default


Some other methods of Authenticator

getDefault()This method gets the default authenticator.

Authenticator.requestPasswordAuthentication(InetAddress, int, String, String, String)This method asks the authenticator that has been registered with the system for a password.

Authenticator.requestPasswordAuthentication(String, InetAddress, int, String, String, String)This method asks the authenticator that has been registered with the system for a password. This is the preferred method for requesting a password because the hostname can be provided in cases where the InetAddressis not available.

Authenticator.requestPasswordAuthentication(String, InetAddress, int, String, String, String, URL, RequestorType)This method asks the authenticator that has been registered with the system for a password.

Authenticator.requestPasswordAuthentication(Authenticator, String, InetAddress, int, String, String, String, URL, RequestorType)This method asks the given authenticator for a password. If the given authenticator is null, the authenticator, if any, that has been registered with the system using setDefault is used.

Authenticator.requestPasswordAuthenticationInstance(String, InetAddress, int, String, String, String, URL, RequestorType)This method asks this authenticator for a password.

setDefault(Authenticator)This method sets the authenticator that will be used by the networking code when a proxy or an HTTP server asks for authentication.

No comments:

Post a Comment