相关疑难解决方法(0)

重置身份验证器凭据

我们在实用程序类中有一个静态方法,它将从URL下载文件.已设置验证器,因此如果需要用户名和密码,则可以检索凭据.问题是,只要凭证有效,第一次成功连接的凭证就会用于后续的每个连接.这是一个问题,因为我们的代码是多用户的,并且由于没有为每个连接检查凭据,因此没有正确凭据的用户可能会下载文件.

这是我们正在使用的代码

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, …
Run Code Online (Sandbox Code Playgroud)

java

7
推荐指数
2
解决办法
1万
查看次数

标签 统计

java ×1