ホームページ  >  記事  >  Java  >  Java認証システム

Java認証システム

PHPz
PHPzオリジナル
2024-08-30 15:45:08697ブラウズ

Java 認証クラスは、ネットワーク接続の認証に使用されます。このクラスは標準の Java クラスです。オーセンティケーター クラスは、特定の URL にアクセスするためにユーザー認証を必要とするアプリケーションで使用されます。オーセンティケーター クラスは、ユーザー名やパスワードなどの資格情報の入力をユーザーに求めることで認証を実行します。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

アプリケーションで認証を実行するには、アプリケーションでいくつかの手順を実行する必要があります。まず、アプリケーション クラスが java.net.Authenticator クラスを拡張し、サブクラス内の java.net.Authenticator クラスの getPasswordAuthentication() 関数をオーバーライドします。通常、この関数には、ユーザー名やパスワードなど、認証のためのユーザー情報を取得する getXXX() 関数が含まれています。 getPasswordAuthentication() 関数は、認証情報を含む PasswordAuthentication 値を返します。

Java の Authenticator クラスの構文は –

です。
public abstract class Authenticator extends Object
{
// code of the authenticator class
}

Authenticator クラスのメンバー関数

認証に使用できる Authenticator クラスのメンバー関数のリスト全体を見てみましょう。

1. getPasswordAuthentication() – パスワードが必要な場合にこの関数が使用されます。すべてのサブクラスは、デフォルトで null を返す getPasswordAuthentication () をオーバーライドする必要があります。

構文 –

protected PasswordAuthentication getPasswordAuthentication();

2. getRequestingHost() – この関数は、認証を要求しているサイトのホスト名を取得し、ホスト名が見つからない場合は null を返します。

構文 –

protected final String getRequestingHost();

3. getRequestingPort() – この関数は、接続を要求しているポート番号を取得するために使用されます。

構文 –

protected final int getRequestingPort();

4. getRequestingPrompt() – この関数は、リクエスターの文字列メッセージを要求するために使用されます。

構文 –

protected final String getRequestingPrompt();

5. getRequestingProtocol() – この関数は、接続を要求しているプロトコルを取得するために使用されます。

構文 –

protected final int getRequestingProtocol();

6. getRequestingScheme() – この関数は、リクエスター サイトのスキームを削除するために使用されます。

構文 –

protected final String getRequestingScheme();

7. getRequestingSite() – この関数は、要求元サイトの InetAddress を取得するために使用され、InetAddress が存在しない場合は null を返します。

構文 –

protected final InetAddress getRequestingSite();

8. getRequestingURL() – この関数は、リクエスターの URL を取得するために使用されます。

構文 –

protected URL getRequestingURL();

9. setDefault(Authenticator a) – HTTP サーバーが認証を必要とするときにネットワークで使用されるオーセンティケータを設定するために使用されます。

構文 –

public static void setDefault(Authenticator a);

10. getRequestorType() – この関数は、リクエスタがサーバーであるかプロキシであるかを取得するために使用されます。

構文 –

protected Authenticator.RequestorType getRequestorType();

Java の認証クラスの例

次に、Authenticator クラスをより明確に理解するために Java コードを作成します。次の例では、Authenticator クラスのサブクラスを作成し、getPasswordAuthentication() 関数をオーバーライドして、以下のように URL の認証を実行します –

例 #1

コード:

package p1;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URL;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.MalformedURLException;
class MyAuthenticatorclass extends Authenticator
{
protected PasswordAuthentication getPasswordAuthentication()
{
String username = "John", password = "pass123";
this.print();
return new PasswordAuthentication(username, password.toCharArray());
}
void print()
{
int hostname = getRequestingPort();
System.out.println("The request Port number :" + hostname);
}
}
public class Demo
{
public static void main(String[] arg)
{   String data;
try {
//create object of authenticator class
MyAuthenticatorclass obj =new MyAuthenticatorclass();
Authenticator.setDefault(new MyAuthenticatorclass());
URL url = new URL("https://www.educba.com/");
// reads data from the url in html form
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println("The requesting URL is : "+url.getHost());
obj.getPasswordAuthentication() ;
while ((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
} catch (MalformedURLException e) {
System.out.println("Malformed URL Exception : " + e);
} catch (IOException e) {
System.out.println("IO Exception: " + e);
}
}
}

出力:

Java認証システム

次に、以下のように、オーバーライドされた getPasswordAuthentication() 関数内のすべての情報を取得しようとする Authenticator クラスを理解する Java コードを作成します –

例 #2

コード:

package p1;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URL;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.PasswordAuthentication;
import java.net.MalformedURLException;
class MyAuthenticatorclass extends Authenticator
{
protected PasswordAuthentication getPasswordAuthentication()
{
String username = "John", password = "pass123";
this.print();
return new PasswordAuthentication(username, password.toCharArray());
}
void print()
{
// get all information
String prom = getRequestingPrompt();
URL url = getRequestingURL();
int prt = getRequestingPort();
String hostname = getRequestingHost();
InetAddress ipaddress = getRequestingSite();
String sh = getRequestingScheme();
RequestorType rt = getRequestorType();
String protocol = getRequestingProtocol();
System.out.println("The prompt is :" + prom+"\nThe hostname is :" + hostname+"\nThe ipaddress is :" + ipaddress+"\nThe port no is :" + prt);
System.out.println("The protocol is :" + protocol+"\nThe scheme is :" + sh+"\nThe URL is :" + url+"\nThe Requester Type is :" + rt);
}
}
public class Demo
{
public static void main(String[] arg)
{   String data;
try {
//create object of authenticator class
MyAuthenticatorclass obj =new MyAuthenticatorclass();
Authenticator.setDefault(new MyAuthenticatorclass());
URL url = new URL("https://www.educba.com/");
// reads data from the url in html form
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println("The requesting URL is : "+url.getHost());
obj.getPasswordAuthentication() ;
while ((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
} catch (MalformedURLException e) {
System.out.println("Malformed URL Exception : " + e);
} catch (IOException e) {
System.out.println("IO Exception: " + e);
}
}
}

出力:

Java認証システム

上記のコードは、認証に関連するすべての情報を取得しようとしています。

結論

Java オーセンティケーター クラスは、URL またはネットワーク接続認証を処理するために使用される組み込み Java クラスです。これまで見てきたように、この組み込みクラスには、それぞれ特定の目的に使用できる 10 個の関数があります。

以上がJava認証システムの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:JavaのinstanceOf次の記事:JavaのinstanceOf