【发布时间】:2013-05-25 05:02:13
【问题描述】:
编辑:我认为 url 是罪魁祸首。在工作 login.html 案例中,我进入了日志:
FINE:安全检查请求 POST /SesamaMaven/protected/admin/j_security_check
在 AJAX 版本中我得到了:
FINE:安全检查请求 POST /SesamaMaven/
我在 Glassfish 中使用 JDBCRealm 配置了身份验证,它似乎可以像这样使用普通 login.html:
<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>
我的问题是,当我尝试使用 AJAX 实现相同功能时,它不起作用。有没有可能让它工作?
HTML
<form class="navbar-form pull-right">
<input class="span2" type="text" placeholder="Email" name="j_username" id="username">
<input class="span2" type="password" placeholder="Password" name="j_password" id="password">
<button type="button" class="btn" id="btnSignIn">Sign in</button>
</form>
JS
$('#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);
}
});
});
问题
1) 正确的 contentType 是什么:“application/text”?
2) URL 标签是正确的还是应该使用action?
3) 如果是这样的话,参数用户名和密码呢?
Glassfish 尝试进行身份验证,但没有用户名和密码。
【问题讨论】:
-
旧的,所以只针对那些感兴趣的人:1)您需要先点击一个安全页面才能登录(容器需要启动登录过程)。 2.登录失败的响应是重定向到错误URL,所以你需要在ajax响应中检查这个。 3. Content-Type 应该是
x-www-form-urlencoded(普通的HTML表单帖子)
标签: javascript ajax authentication j-security-check