Клас Java.net.Authenticator в Java
Клас автентифікатора використовується в тих випадках, коли для відвідування певної URL-адреси потрібна автентифікація. Коли стає відомо, що потрібна автентифікація, користувач запитує її або використовує жорстко закодовані ім’я користувача та пароль.
Щоб використовувати цей клас, виконайте наступні кроки:
-
Створіть клас, який розширює Authenticator. Назвемо його customAuth. - Перевизначте метод getPasswordAuthentication(). Цей метод містить кілька методів для отримання деталей сутності, яка запитує автентифікацію. Усі ці методи детально обговорюються пізніше.
- Встановіть щойно створений підклас як автентифікатор за замовчуванням, який буде використовуватися, коли http-сервер запитує автентифікацію за допомогою методу setDefault(Authenticator a) класу Authenticator.
-
-
Ще один перевантажений метод, який можна використовувати в ситуаціях, коли можна використовувати ім’я хоста, якщо inetaddress недоступний.
-
Інший перевантажений метод, який можна використовувати, якщо відома лише URL-адреса сайту, який запитує автентифікацію, а не inetaddress і ім’я хоста.
-
-
-
-
-
-
-
-
-
-
setDefault(Authenticator a): Встановлює автентифікатор, який буде використовуватися, коли сервер HTTP вимагає автентифікації.
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() : Запитує пароль у зареєстрованого в системі автентифікатора. Повертає ім'я користувача/пароль або null, якщо не знайдено.
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() : повертає ім’я хоста сайту, який запитує автентифікацію.
Syntax : protected final String getRequestingHost()
getRequestingSite() : повертає inetaddress сайту, який запитує автентифікацію.
Syntax : protected final InetAddress getRequestingSite()
getRequestingPort() : повертає порт підключення.
Syntax : protected final int getRequestingPort()
getRequestingProtocol() : повертає протокол, який запитує з’єднання.
Syntax : protected final String getRequestingProtocol()
getRequestingPrompt() : повертає повідомлення, запропоноване запитувачем.
Syntax : protected final String getRequestingPrompt()
getRequestingScheme() : повертає схему сайту запиту.
Syntax : protected final String getRequestingScheme()
getPasswordAuthentication() : цей метод викликається, коли потрібна автентифікація за паролем. Усі підкласи повинні перевизначати цей метод, оскільки метод за замовчуванням завжди повертає значення null.
Syntax : protected PasswordAuthentication getPasswordAuthentication()
getRequestingURL() : повертає URL запитувача.
Syntax : protected final URL getRequestingURL()
getRequestorType() : повертає, якщо запитувач є проксі або сервером.
Syntax : protected Authenticator.RequestorType getRequestorType()
Вам Може Сподобатися
Кращі Статті
Категорія
Цікаві Статті
-