Javascript j_安全检查与ajax
编辑:我认为url是罪魁祸首。在working login.html中,我在日志中得到: 罚款:安全检查请求POST/SesamaMaven/protected/admin/j_Security_check 在AJAX版本中,我得到: 罚款:安全检查请求POST/SesamaMaven/ 我使用JDBCRealm在Glassfish中配置了身份验证,它似乎可以使用普通的login.html,如下所示:Javascript j_安全检查与ajax,javascript,ajax,authentication,j-security-check,Javascript,Ajax,Authentication,J Security Check,编辑:我认为url是罪魁祸首。在working login.html中,我在日志中得到: 罚款:安全检查请求POST/SesamaMaven/protected/admin/j_Security_check 在AJAX版本中,我得到: 罚款:安全检查请求POST/SesamaMaven/ 我使用JDBCRealm在Glassfish中配置了身份验证,它似乎可以使用普通的login.html,如下所示: <html xmlns="http://www.w3.org/1999/xhtml"&g
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login Form</title>
</head>
<body>
<form method="post" action="j_security_check">
<p>You need to log in to access protected information.</p>
<table>
<tr>
<td>User name:</td>
<td><input type="text" name="j_username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="j_password" /></td>
</tr>
</table>
<p><input type="submit" value="Login" /></p>
</form>
</body>
</html>
问题
1正确的内容类型是什么:应用程序/文本
2 URL标记是否正确,或者我是否应该使用action
3.在这种情况下,用户名和密码参数如何
Glassfish尝试进行身份验证,但没有用户和密码。内容类型:应用程序/文本是罪魁祸首。我刚把那句话讲完,一切都开始起作用了 还有一个问题。当身份验证中出现错误时,它会重定向到index.html,但没有css,地址栏中包含了它在成功案例/protected/adminWelcome.html中应该去的地址。我将这些代码添加到login.html文件中 在开发过程中,我懒得输入用户名和密码
xhttp.open("POST", "j_security_check", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("j_username=MY_USERNAME&j_password=MY_PASSWORD");
location.reload(true);
看起来,当用户输入错误的凭证时,您试图通知用户
在我的例子中,login.html和error-login.html完全相同
除了error-login.html有一个文本外,您输入了错误的密码或用户名,因此仅针对那些感兴趣的人:1您需要先点击一个安全页面,然后登录才能工作。容器需要启动登录过程。2.登录失败的响应是重定向到错误URL,所以您需要在ajax响应中检查这一点。3.内容类型应为x-www-form-urlencoded常规HTML表单帖子
$('#btnSignIn').click(function() {
$.ajax({
type: "POST",
contentType: "application/text",
url: "j_security_check",
// This is the type what you are waiting back from the server
dataType: "text",
async: false,
crossDomain: false,
data: {
j_username: "admin",
j_password: "paSSWORD"
},
success: function(data, textStatus, xhr) {
alert('Thanks for your signin in! ' + xhr.status);
window.location = "/SesamaMaven/protected/adminWelcome.html";
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
window.location = "/SesamaMaven/index.html";
alert(' Error in signIn-process!! ' + textStatus);
}
});
});
xhttp.open("POST", "j_security_check", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("j_username=MY_USERNAME&j_password=MY_PASSWORD");
location.reload(true);