使用 Java Windows 服务器 2012 中的基本身份验证失败

Basic Authentication failed in Windows server 2012 Using Java

我正在尝试通过 windows 服务器 2012 连接我的证书服务。我已经在服务器上启用了基本身份验证,我可以通过提供用户名和密码的浏览器访问我的服务。 我在 Java 中使用 HttpURLConnection 打开连接。 使用以下代码连接成功。

static class MyAuthenticator extends Authenticator {
    public PasswordAuthentication getPasswordAuthentication() {
        System.out.println("trying to authenticate");
        return new PasswordAuthentication("user", "password".toCharArray());
    }
}

Authenticator.setDefault(new MyAuthenticator())

但我需要使用以下代码进行连接,但出现 401 错误:

String encoding = Base64.encode("user:password".getBytes());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty  ("Authorization", "Basic " + encoding);

知道我为什么会得到 401。

谢谢,

IIS ManagerDefault Web Site 的基本身份验证已启用,但特定站点的 Basic Authentication 未启用。通过为特定网站启用 Basic Authentication 解决了这个问题。