ajax xhr auth,HTTP Basic Authentication with jQuery AJAX requests

Problem

Sometimes the access to a web page or resource should be protected.

To accomplish the task use a HTTP authentication.

The request for such a resource through the XmlHttpRequest

interface or Fetch API may hurt user experience

since an alert asking for user credentials will appear.

HTTP Authentication

HTTP Authentication provides mechanism to protect web pages and resources.

Basic

The browser sends the username and password as Base64-encoded text, without any encryption. Basic authentication should only be used with HTTPS, otherwise the password can be exposed to everyone.

If the client request protected resource without providing credentials, the server will reject the request and send back 401 HTTP status and WWW-Authenticate header.

HTTP/1.1 401 Unauthorized

WWW-Authenticate: Basic realm="Restricted area"

Then the browser will display popup asking for user credentials used to retry the request with Authorization header.

Authorization: Basic bXl1c2VyOm15cHN3ZA==

Digest

The client sends the hashed variant of the username and password. Encryption instead of encoding makes the digest authentication safer than basic auth.

Request

The solution is quite simple, an Authorization header sent with the request.

// using jQuery & 'beforeSend' callback

$.ajax({

xhrFields: {

withCredentials: true

},

beforeSend: function (xhr) {

xhr.setRequestHeader('Authorization', 'Basic ' + btoa('myuser:mypswd'));

},

url: "https://www.example.org/protected-data.php"

});

// using jQuery & 'headers' property

$.ajax({

xhrFields: {

withCredentials: true

},

headers: {

'Authorization': 'Basic ' + btoa('myuser:mypswd')

},

url: "https://www.example.org/protected-data.php"

});

// using XMLHttpRequest

var xhr = new XMLHttpRequest();

xhr.open("GET", "https://www.example.org/protected-data.php", true);

xhr.withCredentials = true;

xhr.setRequestHeader("Authorization", 'Basic ' + btoa('myuser:mypswd'));

xhr.onload = function () {

console.log(xhr.responseText);

};

xhr.send();

// using Fetch API

var myHeaders = new Headers();

myHeaders.append("Authorization", 'Basic ' + btoa('myuser:mypswd'));

fetch("https://www.example.org/protected-data.php", {

credentials: "include",

headers: myHeaders

}).then(function (response) {

return response.json();

}).then(function (json) {

console.log(json);

});

jQuery API provides username and password parameters as part of the ajax settings object, which intends to do the job instead of sending a header by yourself, unfortunatelly they doesn't works.

Response

To trigger the basic authentication use your prefered method.

PHP

if (!(isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])

&& $_SERVER['PHP_AUTH_USER'] == 'myuser'

&& $_SERVER['PHP_AUTH_PW'] == 'mypswd')) {

header('WWW-Authenticate: Basic realm="Restricted area"');

header('HTTP/1.1 401 Unauthorized');

exit;

}

?>

Apache

AuthType Basic

AuthName "Restricted area"

AuthUserFile "/var/www/.htpasswd"

Require valid-user

Nginx

auth_basic "Restricted area";

auth_basic_user_file /var/www/.htpasswd;

htpasswd is used to create and update the flat-files used to store usernames and password for basic authentication of HTTP users.

htpasswd -c /var/www/.htpasswd myuser

Cross-Domain Requests with CORS

In case, the protected resource or page is accessible through a domain that differs from the origin, a restriction from same origin policy is applied. To circumvent the same-origin policy, use the Cross-origin resource sharing.

# Request Headers

Authorization: Basic bXl1c2VyOm15cHN3ZA==

Host: subdomain.example.org

Origin: https://www.example.org

# Response Headers

Access-Control-Allow-Credentials: true

Access-Control-Allow-Headers: Origin, Authorization

Access-Control-Allow-Methods: POST, GET, OPTIONS

Access-Control-Allow-Origin: https://www.example.org

P3P:CP="CAO PSA OUR"

Demo

To see an example of ajax basic auth check out our demo.

a9b49f7ef7d9f0a7d40b31d7fc84f0ba.png

Make your website more secure by using the HTTP Headers for Wordpress, and never face a cross-origin issue again. Oh yes, it's FREE.

See also

Share this post

If you have questions about the ajax basic authentication, please leave a comment below. And do not be shy to share this article. Thanks so much for reading!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值