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:
-
Crea una classe che estende l'Authenticator. Chiamiamolo customAuth. - 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.
- 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.
-
-
Un altro metodo di sovraccarico che può essere utilizzato in situazioni in cui è possibile utilizzare il nome host se l'indirizzo ineta non è disponibile.
-
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.
-
-
-
-
-
-
-
-
-
-
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
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.
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.
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.
getRequestingHost() : restituisce l'hostname del sito che richiede l'autenticazione.
Syntax : protected final String getRequestingHost()
getRequestingSite() : restituisce l'indirizzo ine del sito che richiede l'autenticazione.
Syntax : protected final InetAddress getRequestingSite()
getRequestingPort() : restituisce la porta di connessione.
Syntax : protected final int getRequestingPort()
getRequestingProtocol() : restituisce il protocollo che richiede la connessione.
Syntax : protected final String getRequestingProtocol()
getRequestingPrompt() : restituisce il messaggio richiesto dal richiedente.
Syntax : protected final String getRequestingPrompt()
getRequestingScheme() : restituisce lo schema del sito richiedente.
Syntax : protected final String getRequestingScheme()
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()
getRequestingURL() : restituisce l'URL del richiedente.
Syntax : protected final URL getRequestingURL()
getRequestorType() : restituisce se il richiedente è proxy o server.
Syntax : protected Authenticator.RequestorType getRequestorType()
Potrebbe Piacerti
Articoli Più
Categoria
Articoli Interessanti
-