Authenticator.requestPasswordAuthentication(String, InetAddress, int, String, String, String): 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)
This method takes six arguments. 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.
Parameters: Six parameters are required for this method.
host: The hostname of the site requesting authentication.
addr: The InetAddress of the site requesting authentication, 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 which identifies the authentication realm.
scheme: The authentication scheme.
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.InetAddress;import java.net.MalformedURLException;import java.net.PasswordAuthentication;import java.net.UnknownHostException;public class AuthenticatorrequestPasswordAuthentication2 {public static void main(String[] args) throws MalformedURLException, UnknownHostException {ClassAuthenticator obj1 = new ClassAuthenticator();Authenticator.setDefault(obj1);System.out.println(Authenticator.requestPasswordAuthentication("www.google.com", InetAddress.getLocalHost(), 0,"https", null, null));}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@769c9116
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