【问题标题】:Basic Authentication with special characters with Jquery Ajax Tomcat使用 Jquery Ajax Tomcat 使用特殊字符进行基本身份验证
【发布时间】:2014-02-20 08:32:51
【问题描述】:

我正在尝试使用 AJAX 向 tomcat 服务器发送 DELETE 请求。

我希望请求使用基本身份验证。如果密码包含特殊字符,我真的无法到达那里。

系统的配置是:

  • jquery 1.10.2
  • tomcat 7.0.47

在询问之前,我尝试遵循我在以下内容中找到的内容:

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。

看来是我提出的请求有问题。

请问有人可以帮我吗?

【问题讨论】:

标签: javascript jquery ajax tomcat basic-authentication


【解决方案1】:

最后我不明白的问题是: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");
    }
});

【讨论】:

猜你喜欢
  • 2011-07-27
  • 2016-01-01
  • 2012-04-30
  • 2012-01-27
  • 1970-01-01
  • 2012-09-16
  • 2017-06-13
相关资源
最近更新 更多