Home » Tip Bank » Java
  • Comment on this Article
Language: Java
Expertise: Intermediate
May 6, 2020



Building the Right Environment to Support AI, Machine Learning and Deep Learning


Understanding java.net.PasswordAuthentication

PasswordAuthentication holds the data that will be used by the Authenticator. The username and password are stored in the PasswordAuthentication object. The methods getUserName() and getPassword() are made available and return the userName and password respectively.

import java.net.PasswordAuthentication;

public class UnderstandingPasswordAuthentication
   public static void main(String args[])
      UnderstandingPasswordAuthentication understandingPasswordAuthentication = new UnderstandingPasswordAuthentication();
   private void proceed()
      //Initializing the user name
      String userName = "devUser";
      //Initializing the password - This is a char array since the PasswordAuthentication supports this argument
      char[] password = {'d','e','v','U','s','e','r'};
      PasswordAuthentication passwordAuthentication = new PasswordAuthentication(userName, password);
      System.out.println("Details being retrieved from PasswordAuthentication object post initializing");
      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()));


Expected output:

[root@mypc]# java UnderstandingPasswordAuthentication
Details being retrieved from PasswordAuthentication object post initializing
UserName: devUser
Password: [C@15db9742
Password: devUser

M S Sridhar
Submit a Tip
Browse "Java" Tips
Browse All Tips