Solved

Authenticator.setDefault( new MyAuthenticator()) not working

Posted on 2004-03-30
5
1,786 Views
Last Modified: 2008-03-03
Hi,

I have written a simple web app that calls a web page on another web server that returns html.  I have extended the Authenticator class as follows and I set it as the default in the init() method.


public class MyAuthenticator extends Authenticator
{
  public MyAuthenticator()
  {
    System.out.println("New MyAuthenticator");
  }

  protected PasswordAuthentication getPasswordAuthentication()
  {
    System.out.println("Obtaining Authentication data");
    return new PasswordAuthentication("EcoRobotic", "tester01".toCharArray());
  }
}


public void init() throws ServletException
{
    Authenticator.setDefault(new MyAuthenticator());
}


The actual HttpURLCOnnection is obtained as follows.

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    try
    {
      //Obtain the connection
      URL url = new URL("http://url-of-web-page");
      HttpURLConnection connection = (HttpURLConnection)url.openConnection();

      //Read the data from the connection and dump it out
      InputStreamReader is = new InputStreamReader(connection.getInputStream());
      BufferedReader in = new BufferedReader(is);

      String inputLine;
      while ((inputLine = in.readLine()) != null)
      {
        System.out.println(inputLine);
      }
    }
    catch(Exception e)
    {
      System.out.println(e.toString());
    }
}


When I call the page from the browser I get the following output.

java.io.FileNotFoundException: Response: '401: Unauthorized xxx' for url: 'http: <url name>"

What is really strange is that the System.out.println() declared in the getPasswordAuthentication() method of MyAuthenticator is never called.


Any ideas, as I have no idea why this is failing.  If I call a non Authenticated URL it works fine.

Thanks
0
Comment
Question by:flanagaj
  • 2
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10713742
Try recycling the container to make sure init's getting called
0
 

Author Comment

by:flanagaj
ID: 10713819
Have tried that, makes no difference.  I have got it to work by setting a Header Request Param

      URL url = new URL("http://<lan url>");
      String userPassword = "username:password";

     // Encode String
      String encoding = new sun.misc.BASE64Encoder().encode (userPassword.getBytes());

     // Need to work with URLConnection to set request property
      URLConnection uc = url.openConnection();
      uc.setRequestProperty("Authorization", "Basic " + encoding);
      InputStream content = (InputStream)uc.getInputStream();
      BufferedReader in   = new BufferedReader (new InputStreamReader(content));
     
      String line;
      while ((line = in.readLine()) != null)
      {
        System.out.println(line);
      }

This worked first time.  I wonder if it something to do with the config of BEA Weblogic.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10713965
>>wonder if it something to do with the config of BEA Weblogic.

It's possible yes. If you're OK now, please feel free to get the q deleted
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 10908983
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now