Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
ajax保护身份验证的最佳方法_Ajax_Security_Authentication_Encryption_Header - Fatal编程技术网

ajax保护身份验证的最佳方法

ajax保护身份验证的最佳方法,ajax,security,authentication,encryption,header,Ajax,Security,Authentication,Encryption,Header,使用Ajax,要访问我的一项服务,我需要通过标题提供用户名和密码 为此,我使用beforesend函数,如下所示: beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', 'Basic ' + btoa('my_username:my_password')); } 我的问题是每个人都可以看到变量My_username和My_password的值 向我的用户隐藏这些变量的最佳方法是什么?您需要使用代理文件(例如PHP)

使用Ajax,要访问我的一项服务,我需要通过标题提供用户名和密码

为此,我使用beforesend函数,如下所示:

beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic ' + btoa('my_username:my_password'));
}
我的问题是每个人都可以看到变量My_username和My_password的值


向我的用户隐藏这些变量的最佳方法是什么?

您需要使用代理文件(例如PHP),在该文件中可以创建标头,然后将其发送到服务器

                $.ajax({
                    url: 'MakeRequest.php,
                    type: 'POST',
                    data: {your_data},
                    contentType: 'application/json; charset=utf-8',
                    success: function(data) {
                      /*Success*/

                     },error: function(data) {
                      /*Error*/
                    }
                });

然后在PHP(或您正在使用的任何文件)上,您可以使用CURL向服务器发出认证请求

什么会阻止攻击者通过php文件访问服务?:)服务器端没有人能看到发出请求所使用的令牌-他需要将其保存在数据库、cookie或localstorageYes上,但还有一个客户端,它需要连接到这个php,然后它将查询到实际的服务。作为攻击者,为什么我不能使用此php而不是原始服务?它只会为我添加正确的凭证。@LukeJoshuaPark,甚至连正确的答案都没有。HTTPS不能解决这个问题,这个答案也没有帮助。问题是不清楚提问者想要实现什么。@GaborLengyel我错过了他说“来自我的用户”的部分。你想要实现什么?您想知道用户是谁吗?上述解决方案对此没有帮助。您想确保只有您的javascript可以连接,而其他javascript不能连接吗?这是不可能的。