Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:使用HTTPBasic身份验证获取URL_Java_Http_Authentication_Url_Http Authentication - Fatal编程技术网

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

Java:使用HTTPBasic身份验证获取URL,java,http,authentication,url,http-authentication,Java,Http,Authentication,Url,Http Authentication,我正在做一些简单的HTTP身份验证,并得到一个 java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic OGU0ZTc5ODBk(...trimmed from 76 chars...) (...more password data...) 我想这是因为我有一个很长的用户名和密码,编码器用一个76个字符的\n来包装它。我有什么办法可以绕开这件事吗?URL仅支持HTTP基本身份

我正在做一些简单的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;
}
这似乎是一个问题

您是否尝试过使用其他HTTP客户端,例如Apache中的库

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

URL url = new URL("http://www.example.com/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Basic OGU0ZTc5ODBkABcde....");
令牌值是encodeBase64(“用户名:密码”)。

这对我很有用

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

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

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

只要我在你的错误帖子中进行修复,效果就很好。
String encoding=new sun.misc.BASE64Encoder().encode(userpass.getBytes());//Java错误:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6459815 		encoding=encoding.replaceAll(“\n”和“)。在对Atlassian REST API(如Bamboo)进行身份验证时,ThanksA 64个字符的密码仍然失败。就我而言,更换换行符是不够的。Groovy HTTPBuilder和AHC正确地处理了长密码。谢谢Oracle,我浪费了30分钟的时间来处理这个愚蠢的问题。(回答向上投票,真诚的感谢)安全提示:如果通过HTTP使用用户名和密码,这两个东西都将作为清晰的字符串发送。最好使用不同的身份验证(表单、POST)和传输协议(HTTPS、H2)。