![](/img/trans.png)
[英]Alert is shown for entering username and password even though am passing basic auth
[英]Basic Auth through Ajax for accessing a page secured with https without entering Username and Password in dialog box
我需要访问并显示此网址上托管的网站页面 - https://xxxxx.com ,我的应用程序中没有任何身份验证对话框托管在同一网络中。但我无法再见 - 通过对话框进入用户名和密码。
我使用Basic Auth进行授权,它可以和Postman一起使用。我通过在头文件中提供Basic Auth值作为授权,在Postman中得到一个成功的响应。基本Auth的相同加密值,我在Springboot代码中用来从中获取一些细节页面,它也工作正常。
但是,当我尝试使用标记和Javascript导航/显示此页面时,在为Basic Auth设置所需的标题后,我收到错误响应并且未经授权 - 401带有消息 - WWW-Authenticate:Basic realm =“请登录Office电子邮件用户ID(不包含@ xxx.xx)“
我尝试了不同的ajax组合,但没有得到200个代码,它总是要求输入对话框,要求我输入用户名和密码。否则最终在401中。
以下是使用的代码。
function handleSubmit(e){
$.ajax({
url: 'https://xxxxxx.com/se/',
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type':'application/json',
},
type: 'GET',
dataType: 'json',
async: false,
data: '',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "Basic cmlqdS5mcmFuY2lzOkN1cmlwczIxxxxxxxx==");
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
},
success: function(){
alert('succes: ');
}
});
}
这是我在HTML中的表单 -
这是我的Ajaxrequests的基本代码。 我使用它在我的Webspace上请求一个安全的Dir。 作为URL,我使用CORS代理只是为了绕过CORS问题并使AuthAccess正常工作。
也许你可以尝试这个为您的应用程序。 如果你在没有401错误的情况下工作而没有要求登录,你可以在不使用CORS代理URL的情况下添加CORS Header。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function ajax() {
user = "test"; // Insert your
pass = "test"; // real Login here
$.ajax({
url : "https://--insert-a-cors-proxy-here--.com/https://youdomain.here/secured/index.php",
type : "get",
cache : false,
beforeSend : function(xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(user+":"+pass));
},
success : function (result) {
$("#result").html(result);
},
"error": function() { $('#errorbox').html('Oh noo, Error!'); }
});
}
</script>
</head>
<body>
<input type="button" value="Request with Ajax!" onclick="ajax();">
<div id="errorbox" style="background-color:red"> </div>
<div id="result" style="background-color:yellow"> </div>
</body>
</html>
在我的函数中修改为history.pushState({},null,'/ xxx.com')后,这工作正常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.