Classe Java.net.Authenticator in Java

La classe Authenticator viene utilizzata nei casi in cui è richiesta un'autenticazione per visitare alcuni URL. Una volta noto che è richiesta l'autenticazione, richiede all'utente la stessa o utilizza nome utente e password codificati. 
Per utilizzare questa classe vengono seguiti i seguenti passaggi: 
 


  1. Crea una classe che estende l'Authenticator. Chiamiamolo customAuth.
  2. Sostituisci il metodo getPasswordAuthentication(). Questo metodo contiene diversi metodi per ottenere i dettagli dell'entità che richiede l'autenticazione. Tutti questi metodi verranno discussi in dettaglio più avanti.
  3. Imposta la sottoclasse appena creata come autenticatore predefinito da utilizzare quando un server http richiede l'autenticazione con il metodo setDefault(Authenticator a) della classe Authenticator.
      setDefault(Autenticatore a): Imposta l'autenticatore da utilizzare quando un server HTTP richiede l'autenticazione. 
       
  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 

  1.  
  2. requestPasswordAuthentication() : Chiede la password all'autenticatore registrato nel sistema. Restituisce nome utente/password o null se non trovato.
     
  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. 

  1. Un altro metodo di sovraccarico che può essere utilizzato in situazioni in cui è possibile utilizzare il nome host se l'indirizzo ineta non è disponibile. 
     
  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. 

  1. Un altro metodo di sovraccarico che può essere utilizzato se si conosce solo l'URL del sito che richiede l'autenticazione e non l'indirizzo e il nome host. 
     
  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. 

  1.  
  2. getRequestingHost() : restituisce l'hostname del sito che richiede l'autenticazione. 
     
  Syntax : protected final String getRequestingHost()   

  1.  
  2. getRequestingSite() : restituisce l'indirizzo ine del sito che richiede l'autenticazione. 
     
  Syntax : protected final InetAddress getRequestingSite()   

  1.  
  2. getRequestingPort() : restituisce la porta di connessione. 
     
  Syntax : protected final int getRequestingPort()   

  1.  
  2. getRequestingProtocol() : restituisce il protocollo che richiede la connessione. 
     
  Syntax : protected final String getRequestingProtocol()   

  1.  
  2. getRequestingPrompt() : restituisce il messaggio richiesto dal richiedente. 
     
  Syntax : protected final String getRequestingPrompt()   

  1.  
  2. getRequestingScheme() : restituisce lo schema del sito richiedente. 
     
  Syntax : protected final String getRequestingScheme()   

  1.  
  2. getPasswordAutenticazione() : questo metodo viene chiamato quando è richiesta l'autenticazione della password. Tutte le sottoclassi devono sovrascrivere questo metodo poiché il metodo predefinito restituisce sempre null. 
     
  Syntax : protected PasswordAuthentication getPasswordAuthentication()   

  1.  
  2. getRequestingURL() : restituisce l'URL del richiedente. 
     
  Syntax : protected final URL getRequestingURL()   

  1.  
  2. getRequestorType() : restituisce se il richiedente è proxy o server. 
     
  Syntax : protected Authenticator.RequestorType getRequestorType()   

  1.  
Crea quiz