Authenticator.requestPasswordAuthentication(String, InetAddress, int, String, String, String, URL, RequestorType) in Java

Authenticator.requestPasswordAuthentication(String, InetAddress, int, String, String, String, URL, RequestorType): This method is available in the java.net.Authenticator class of Java.

Syntax:

PasswordAuthentication java.net.Authenticator.requestPasswordAuthentication(String host, InetAddress addr, int port, String protocol, String prompt, String scheme, URL url, RequestorType reqType)

This method takes 8 arguments. This method asks the authenticator that has been registered with the system for a password.

Parameters: 8 Parameters are required for this method.

host: The hostname of the site requesting authentication.

addr: The InetAddress of the site requesting authorization, or null if not known.

port: the port for the requested connection.

protocol: The protocol that's requesting the connection.

prompt: A prompt string for the user.

scheme: The authentication scheme.

url: The requesting URL that caused the authentication.

reqType: The type (server or proxy) of the entity requesting authentication.

Returns: The username/password, or null if one can't be gotten.

Throws:

1. SecurityException - if a security manager exists and its checkPermission method doesn't allow the password authentication request.

Approach

Java

package com.Authenticator;

import java.net.Authenticator;
import java.net.Authenticator.RequestorType;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.UnknownHostException;

public class AuthenticatorrequestPasswordAuthentication3 {
    public static void main(String[] args) throws MalformedURLException, UnknownHostException {

        ClassAuthenticator obj1 = new ClassAuthenticator();
        Authenticator.setDefault(obj1);

        URL url = new URL("https://beingcodeexpert.blogspot.com");
        System.out.println(Authenticator.requestPasswordAuthentication("www.google.com", InetAddress.getLocalHost(), 0,
                "https", null, null, url, RequestorType.SERVER));
    }

    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:

Port Requesting :0 java.net.PasswordAuthentication@4cdbe50f


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