【问题标题】:j_security_check with ajaxj_security_check 与 ajax
【发布时间】: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


【解决方案1】:

我将这些代码添加到 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.htmlerror-login.html 完全相同,

除了 error-login.html 有一个文本“您输入了错误的密码或用户名

【讨论】:

    【解决方案2】:

    contentType: "application/text" 是罪魁祸首。我刚刚评论了那条线,一切都开始起作用了。

    还有一个问题。当身份验证出错时,它会重定向到 index.html 但没有 css 并且地址栏包含在成功的情况下应该去的地址 /protected/adminWelcome.html。

    【讨论】:

      猜你喜欢
      • 2012-11-17
      • 2023-03-18
      • 2010-09-12
      • 2013-03-30
      • 2012-03-20
      • 2011-03-28
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多