📜  Java的.net.PasswordAuthentication类在Java中

📅  最后修改于: 2022-05-13 01:55:46.799000             🧑  作者: Mango

Java的.net.PasswordAuthentication类在Java中

PasswordAuthentication 类由包Java.net 提供,用于实现网络应用程序,在需要保存 Authenticator 将使用的数据的情况下使用它。它保存用户名和密码。

其构造函数的语法:

PasswordAuthentication(String userName, char[] password)

这将为给定的用户名和密码创建新的 PasswordAuthentication 对象。给定的用户密码在存储在新的 PasswordAuthentication 对象中之前会被克隆。

Method Return Type
getUserName()Returns the username.
getPassword()Returns the user password.

方法详情:

  1. getUserName() :这将给出用户名并返回一个字符串值。
  2. getPassword() :这将返回用户密码并返回字符数组。

它从类Java.lang.Object 继承的方法:

  1. 等于()
  2. toString()
  3. 哈希码()
  4. 克隆()
  5. 获取类()
  6. 完成()
  7. 通知()
  8. 通知所有()
Java
// Java Program to illustrate the 
// java.net.PasswordAuthentication
// Class
import java.io.*;
import java.net.PasswordAuthentication;
  
class GFG {
  
    public static void main(String args[])
    {
        GFG acc = new GFG();
        acc.proceed();
    }
  
    private void proceed()
    {
        // Initializing the user name
        String userName = "Geek";
        
        // Initializing the password - This is a char
        // array since the PasswordAuthentication
        // supports this argument
        char[] password = { 'g', 'e', 'e', 'k', 'g', 'o',
                            'r', 'g', 'e', 'e', 'k', 's' };
  
        PasswordAuthentication passwordAuthentication
            = new PasswordAuthentication(userName,
                                         password);
        System.out.println(
            "UserName: "
            + passwordAuthentication.getUserName());
        
        // The below getPassword actually returns the
        // reference to the password as per the Java API
        // documentation.
        System.out.println(
            "Password: "
            + passwordAuthentication.getPassword());
        
        // You can get the password in normal string
        System.out.println(
            "Password: "
            + String.copyValueOf(
                passwordAuthentication.getPassword()));
    }
}


输出
UserName: Geek
Password: [C@4e50df2e
Password: geekgorgeeks