O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. We need to extend java.net.Authenticator and override its getPasswordAuthentication() method. This method should return an instance of java.net.PasswordAuthentication. Let's create a class, UsernamePasswordAuthenticator, which extends java.net.Authenticator:
        public class UsernamePasswordAuthenticator           extends Authenticator{        }
  1. We will create two instance variables in the UsernamePasswordAuthenticator class  to store the username and password, and we'll provide a constructor to initialize the same:
        private String username;        private String password;        public UsernamePasswordAuthenticator(){}        public UsernamePasswordAuthenticator ( String username,                                                String password){          this.username = username;          this.password = password;        }
  1. We ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required