Java:使用 HTTPBasic 身份验证获取 URL
我正在做一些简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这似乎是一个 Java 中的错误。
您是否尝试过使用替代 HTTP 客户端,例如 Apache 的库?
或者不使用身份验证器,而是手动设置标头?
令牌值为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?
The token value is encodeBase64("username:password").
这对我有用。
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", ""));
我发现非法字符是由“Authorization: Basic”引起的,编码
应该是“授权”、“基本”+编码
I found that the illegal character was caused by "Authorization: Basic ", encoded
which should be "Authorization", "Basic " + encoded
从“授权:基本”编码更改为“授权”,“基本”+编码解决了标头问题中的非法字符。
changing from "Authorization: Basic ", encoded to "Authorization", "Basic " + encoded solved my illegal character in header issue.