Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 111973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:28:17+00:00 2026-05-11T02:28:17+00:00

We have a static method in a utility class that will download a file

  • 0

We have a static method in a utility class that will download a file from a URL. An authenticator has been set up so that if a username and password is required, the credentials can be retrieved. The problem is that the credentials from the first successful connection are used for every connection afterwords, as long as the credentials are valid. This is a problem because our code is multi user, and since the credentials are not checked for every connection, it’s possible that a user without proper credentials could download a file.

Here’s the code we’re using

private static URLAuthenticator auth;  public static File download(String url, String username, String password, File newFile) {     auth.set(username, password);     Authenticator.setDefault(auth);     URL fURL = new URL(url);     OutputStream out = new BufferedOutputStream(new FileOutputStream(newFile));     URLConnection conn = fURL.openConnection();     InputStream in = conn.getInputStream();      try     {         copyStream(in, out);     }     finally     {         if (in != null)             in.close();         if (out != null)             out.close();     }      return newFile; }  public class URLAuthenticator extends Authenticator {     private String username;     private String password;      public URLAuthenticator(String username, String password)     {          set(username, password);     }      public void set(String username, String password)     {         this.username = username;         this.password = password;     }      protected PasswordAuthentication getPasswordAuthentication()     {         log.debug('Retrieving credentials '' + username + '', '' + password + ''.');         return new PasswordAuthentication(username, password.toCharArray());     } } 

I only see the log statement from getPasswordAuthentication once, the first time that a file is downloaded. After that first successful attempt, getPasswordAuthentication is not called again, even though the credentials have been reset. The result is that after the first successful connection, invalid credentials can be entered, and a successful connection can still be made. Is this possibly a result of the download method being static, and in a static class?

Edit I forgot to mention that this is in a JSF webapp running under tomcat – maybe one of those technologies is setting some default credentials somewhere?

I’ve pulled the URLAuthenticator out into its own class, and made it as non-static as possible, but the problem still exists. I’ve read that if the default authenticator is set to null with Authenticator.setDefault(null), then on windows the NTLM authentication will be used. That shouldn’t be the problem here since I’m setting the Authenticator everytime, but I thought I’d throw it out there. The NTLM authentication is definately getting used, because if the server is run as a user that has access to the downloaded file, the credentials aren’t even asked for, the file just downloads. So something obviously is grabbing my credentials and passing them in before the authenticator is called.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:28:18+00:00Added an answer on May 11, 2026 at 2:28 am

    I’ve figured something out at least. It appears that this behavior is a bug. A workaround is to use a Sun specific class to explicitly reset the cache, like so:

    import sun.net.www.protocol.http.AuthCacheValue; import sun.net.www.protocol.http.AuthCacheImpl; .... AuthCacheValue.setAuthCache(new AuthCacheImpl()); Authenticator.setDefault(new URLAuthenticator(username, password)); 

    I’m resetting the AuthCache at the top of the download function described in the question. During compile, you’ll get warnings about using these classes. This doesn’t completely fix the problem: if NTLM authentication works, the authenticator still won’t get called, but as long as the server is running under a user that has does not have permission for the requested file, this should clear the cache out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 107k
  • Answers 107k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Redirecting to a new page is done with Window.Location.replace. Multiple… May 11, 2026 at 9:07 pm
  • Editorial Team
    Editorial Team added an answer You can't call validate more than ones becouse you run… May 11, 2026 at 9:07 pm
  • Editorial Team
    Editorial Team added an answer SQL Server does simple counting of cascade paths and, rather… May 11, 2026 at 9:07 pm

Related Questions

I miss the .Net remoting days when I could just send an object over
My motto for Java is just because Java has static blocks, it doesn't mean
We have recently been faced with the problem of porting our C++ framework to
We have a PageRoles xml file which contains the page path and the user

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.