Java:使用 HTTPBasic 身份验证获取 URL

发布于 2024-08-17 07:29:19 字数 1426 浏览 2 评论 0原文

我正在做一些简单的 HTTP 身份验证,并得到了一个

java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic OGU0ZTc5ODBk(...trimmed from 76 chars...)
(...more password data...)

,我认为这是因为我有一个非常长的用户名和密码,并且编码器用 76 个字符的 \n 包装它。有什么办法可以解决这个问题吗?该 URL 仅支持 HTTP 基本身份验证。

这是我的代码:

private class UserPassAuthenticator extends Authenticator {
    String user;
    String pass;
    public UserPassAuthenticator(String user, String pass) {
        this.user = user;
        this.pass = pass;
    }

    // This method is called when a password-protected URL is accessed
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, pass.toCharArray());
    }
}

private String fetch(StoreAccount account, String path) throws IOException {
    Authenticator.setDefault(new UserPassAuthenticator(account.getCredentials().getLogin(), account.getCredentials().getPassword()));

    URL url = new URL("https", account.getStoreUrl().replace("http://", ""), path);
    System.out.println(url);

    URLConnection urlConn = url.openConnection();
    Object o = urlConn.getContent();
    if (!(o instanceof String)) 
        throw new IOException("Wrong Content-Type on " + url.toString());

    // Remove the authenticator back to the default
    Authenticator.setDefault(null);
    return (String) o;
}

I'm doing some simple HTTP authentication and am getting a

java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic OGU0ZTc5ODBk(...trimmed from 76 chars...)
(...more password data...)

which I think is due to me having a really long username and password and the encoder wraps it with a \n at 76 chars. Is there any way I can get around this? The URL only supports HTTP Basic Auth.

Here is my code:

private class UserPassAuthenticator extends Authenticator {
    String user;
    String pass;
    public UserPassAuthenticator(String user, String pass) {
        this.user = user;
        this.pass = pass;
    }

    // This method is called when a password-protected URL is accessed
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(user, pass.toCharArray());
    }
}

private String fetch(StoreAccount account, String path) throws IOException {
    Authenticator.setDefault(new UserPassAuthenticator(account.getCredentials().getLogin(), account.getCredentials().getPassword()));

    URL url = new URL("https", account.getStoreUrl().replace("http://", ""), path);
    System.out.println(url);

    URLConnection urlConn = url.openConnection();
    Object o = urlConn.getContent();
    if (!(o instanceof String)) 
        throw new IOException("Wrong Content-Type on " + url.toString());

    // Remove the authenticator back to the default
    Authenticator.setDefault(null);
    return (String) o;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我很OK 2024-08-24 07:29:19

这似乎是一个 Java 中的错误

您是否尝试过使用替代 HTTP 客户端,例如 Apache 的库?

或者不使用身份验证器,而是手动设置标头?

URL url = new URL("http://www.example.com/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Basic OGU0ZTc5ODBkABcde....");

令牌值为encodeBase64(“用户名:密码”)。

That seems to be a bug in Java.

Have you tried using alternative HTTP clients, such as the library from Apache?

Or instead of using the Authenticator, manually setting the header?

URL url = new URL("http://www.example.com/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Basic OGU0ZTc5ODBkABcde....");

The token value is encodeBase64("username:password").

墨离汐 2024-08-24 07:29:19

这对我有用。

HttpsURLConnection con = null;
con = (HttpsURLConnection) obj.openConnection();
字符串编码 = Base64.getEncoder().encodeToString("用户名:密码".getBytes(StandardCharsets.UTF_8));
con.setRequestProperty("授权","基本"+encoding.replaceAll("\n", ""));

This works for me .

HttpsURLConnection con = null;
con = (HttpsURLConnection) obj.openConnection();
String encoding = Base64.getEncoder().encodeToString("username:password".getBytes(StandardCharsets.UTF_8));
con.setRequestProperty("Authorization","Basic "+encoding.replaceAll("\n", ""));

☆獨立☆ 2024-08-24 07:29:19

我发现非法字符是由“Authorization: Basic”引起的,编码
应该是“授权”、“基本”+编码

I found that the illegal character was caused by "Authorization: Basic ", encoded
which should be "Authorization", "Basic " + encoded

最佳男配角 2024-08-24 07:29:19

从“授权:基本”编码更改为“授权”,“基本”+编码解决了标头问题中的非法字符。

changing from "Authorization: Basic ", encoded to "Authorization", "Basic " + encoded solved my illegal character in header issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文