我正在尝试使用 AJAX 向 tomcat 服务器发送 DELETE 请求。
我希望请求使用基本身份验证。如果密码包含特殊字符,我真的无法到达那里。
系统的配置是:
- jquery 1.10.2
- tomcat 7.0.47
在询问之前,我尝试按照我在以下位置找到的内容:
- How to use Basic Auth with jQuery and AJAX?
- How can you encode a string to Base64 in JavaScript?
- Javascript Ajax call using password with special characters
Tomcat 配置为:
</tomcat-users>
<role rolename="authUser"/>
<user username="username" password="password" roles="authUser"/>
</tomcat-users>
我使用 jquery 发出了一个 ajax 请求
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
username: "username",
password: "password",
success: function(){
alert("Data Deleted");
}
});
使用此配置一切正常。
如果我在 tomcat 的配置文件中加入特殊字符,我将无法发出正确的请求。
</tomcat-users>
<role rolename="authUser"/>
<user username="username" password="AeHm#%%9Rt\'JzX^|A'N" roles="authUser"/>
</tomcat-users>
我尝试使用两个 ajax 设置
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
username: "username",
password: "AeHm#%%9Rt\'JzX^|A'N",
success: function(){
alert("Data Deleted");
}
});
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
username: encodeURIComponent("username"),
password: encodeURIComponent("AeHm#%%9Rt\'JzX^|A'N"),
success: function(){
alert("Data Deleted");
}
});
通过这两种配置,我看到了相同的请求。
所以我尝试了一个更老的方法
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
beforeSend: function(xhr) {
xhr.setRequestHeader("Authentication", "Basic " + btoa(unescape(encodeURIComponent("username" + ":" + "AeHm#%%9Rt\'JzX^|A'N"))));
xhr.setRequestHeader("Authorization", "Basic " + btoa(unescape(encodeURIComponent("username" + ":" + "AeHm#%%9Rt\'JzX^|A'N"))));
},
success: function(){
alert("Data Deleted");
}
});
对于这个请求,我总是得到错误:401(未经授权)
但是使用 c# 客户端我可以验证请求:
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:8080/TestAuthentication");
request.Credentials = new NetworkCredential("username", @"AeHm#%%9Rt\'JzX^|A'N");
....
如果我将“用户名”和“AeHm#%%9Rt\'JzX^|A'N”置于警报状态,我也可以访问 URL 并从浏览器中显示给我。
看来是我提出的请求的问题。
拜托,有人可以帮助我吗?
最佳答案
最后我明白了问题是:escape
写作:
- AeHm#%%9Rt\'JzX^|A'N
- 错误的输出:AeHm#%%9Rt'JzX^|A'N =>所以我丢失了“\”字符
写转义“\”字符的密码
- AeHm#%%9Rt\\'JzX^|A'N
- 正确输出:AeHm#%%9Rt\'JzX^|A'N
这里是工作代码:
$.ajax({
type: "DELETE",
url: "http://localhost:8080/TestAuthentication",
beforeSend: function(xhr) {
//May need to use "Authorization" instead
xhr.setRequestHeader("Authentication", "Basic " + btoa(unescape(encodeURIComponent("username" + ":" + "AeHm#%%9Rt\\'JzX^|A'N"))));
xhr.setRequestHeader("Authorization", "Basic " + btoa(unescape(encodeURIComponent("username" + ":" + "AeHm#%%9Rt\\'JzX^|A'N"))));
},
success: function(){
alert("Data Deleted");
}
});
关于javascript - 使用 Jquery Ajax Tomcat 的特殊字符的基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21902216/