Solved

java.net.Authenticator and setDault()

Posted on 2003-11-24
17
867 Views
Last Modified: 2012-05-04
How can the authenticator initially set using setDefault() be 'undone' or 'reset'. In my code I'd like to reset the authenticator to use a new set of credentials each time it's called but it keeps using the credentials that were used to create the initial authenticator. Any help out there?
0
Comment
Question by:craigdawson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
  • 2
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9812982
How are you trying to reset it? - please post code
0
 

Author Comment

by:craigdawson
ID: 9813077
I am not trying because I don't know how to do it :-)  The basic code is below.

// Install Authenticator
Authenticator.setDefault (new MyAuthenticator (userId, password));
...

class MyAuthenticator extends Authenticator {
         private String username = "";
          private String password = "";
   
          public MyAuthenticator(){}
   
          public MyAuthenticator(String username, String password){
                this.username = username;
                this.password = password;
          }
   
          protected PasswordAuthentication getPasswordAuthentication() {
                  return new PasswordAuthentication (this.username, this.password.toCharArray());
          }

}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9813100
You can use the same authenticator:

String password = null;
String username = null;
String hostname = getRequestingHost();
if (hostname.equals("somehost.somewhere.com")) {
  username = "uname1";
  password = "password1");
}
else if(.....
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 86

Expert Comment

by:CEHJ
ID: 9813146
To have a looser coupling between your code and the credentials, you could keep them in a Map keyed on hostname and use Properties to load username/passwords (which you could encrypt if necessary)

0
 

Author Comment

by:craigdawson
ID: 9813150
Not sure that helps me. I want to hit the same url with different credentials each time.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9813187
Then just rotate credentials each time the callback function is called:

numberOfCalls++;      
switch(numberOfCalls) {
      case 1:
            username = "y";
            password = "x";
            break;
      case 2:
            username = "p";
            password = "q";
            break;
}            
            
0
 

Author Comment

by:craigdawson
ID: 9813247
Not just different credentials. User-supplied credentials. I can't hard code them.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9813287
Then what's wrong with

username = getUsernameFromUser();      
password = getPasswordFromUser();

?
0
 

Author Comment

by:craigdawson
ID: 9813354
I have Java web app. The login page accepts credentials. Once the user has entered id and pw, the code must invoke an ASP page with thise same credentials, stream in some content, and continue with the java/jsp processing. It's the call to the ASP I'm trying to use the Authenticator for. I can't ask for the credentials again.

Thanks for all your input to this point.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9813388
>>It's the call to the ASP I'm trying to use the Authenticator for

I still don't see what the problem is. What about

String username = (String)request().getSession().getAttribute("username");
String password = (String)request().getSession().getAttribute("password");

(these details having previously been put in the session)

?
0
 

Author Comment

by:craigdawson
ID: 9813842
Maybe I'm just dense but ...

The request object isn't available in getPasswordAuthentication(). If I add it to the constructor and store it, I get an exception: java.lang.IllegalStateException: Context has not been prepared for next connection.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9813877
You could pass the session as an argument to an Authenticator constructor
0
 
LVL 92

Expert Comment

by:objects
ID: 9814864
> You could pass the session as an argument to an Authenticator constructor

Don't think you can do that.
0
 
LVL 92

Expert Comment

by:objects
ID: 9814877
Is there a point in your code where you know the cr4edentials have changed and what they are? If so you could change the authenticator at that point.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 9817377
>>Don't think you can do that.

What makes you think that? Give it a try craigdawson...
0
 

Author Comment

by:craigdawson
ID: 9817575
Using session worked. Full marks for persistence to CEHJ! Thanks.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9817666
:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

691 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