如何解决JAVA中的授权标头字符长度是否有限制?
我正在从android应用发送GET请求,其中包括AUTHORIZATION标头。 当我在服务器上检查auth.password时,它被截断了。 原始字符串为60个字符,服务器仅接收48个字符。 密码是哈希字符串
下面是生成请求的函数的代码。
private String getToken(username,password){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet= new HttpGet(url);
String credentials= "";
if (username.length != 0 && password.length != 0) ){
credentials= username + ":"+ password
}else {
return "";
}
String encoding= Base64.encodeToString(credentials.getBytes("utf-8"),Base64.DEFAULT);
httpGet.setHeader(HttpHeaders.HOST,SERVER);
httpGet.setHeader(HttpHeaders.AUTHORIZATION,"Basic " + encoding);
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(httpGet);
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null) {
result = convertInputStreamToString(inputStream);
result = new JSONObject(result).getString("token");
}
} catch (Exception e) {
Log.d("InputStream",e.getLocalizedMessage());
result= "";
}
return result;
}
将变量编码解码回字符串可以得到原始凭证
Log.e(TAG,"getToken: DECODED BYTES"+ new String(Base64.decode(encoding,0),"utf-8") );
从Postman发送相同的请求不会截断密码。
示例密码$2b$12$FywsqVzLrCR1iSVDLe7eBeK/.K07JUXX9YEHklyWPz9W7/nj52Xfa
服务器应用程序是用python编写的
解决方法
因此,答案是更改此行代码
String encoding= Base64.encodeToString(credentials.getBytes("utf-8"),Base64.DEFAULT);
TO
String encoding= Base64.encodeToString(credentials.getBytes("utf-8"),Base64.URL_SAFE| Base64.NO_WRAP);
这将保护网址免受诸如 '之类的字符的侵害。 ' (点),尤其是加密密码。 如果您检查示例密码,则点字符在那里。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。