Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用spring安全性和ajax登录处理无效用户/密码_Jquery_Spring Security - Fatal编程技术网

Jquery 使用spring安全性和ajax登录处理无效用户/密码

Jquery 使用spring安全性和ajax登录处理无效用户/密码,jquery,spring-security,Jquery,Spring Security,我使用的是spring安全登录+ajax登录。如果用户提供了错误的凭证,我需要显示一条消息,说明“无效的用户/密码” 这是ajax登录jquery代码 $(document).ready(function(){ $('#login_btn').click(function(){ //alert("aaa"+validateLoginForm()); if(validateLoginForm()){ sprpst = 'j_username='+$('#j_use

我使用的是spring安全登录+ajax登录。如果用户提供了错误的凭证,我需要显示一条消息,说明“无效的用户/密码”

这是ajax登录jquery代码

$(document).ready(function(){
$('#login_btn').click(function(){
    //alert("aaa"+validateLoginForm());
    if(validateLoginForm()){

        sprpst = 'j_username='+$('#j_username').val()+'&j_password='+$('#j_password').val();
        $.post('j_spring_security_check',sprpst,function(data1) {

            window.location.reload();

        }); 
        $.blockUI({ message : '<h1><img src="images/busy.gif" /> Loading...</h1>' });
    }
});
});
$(文档).ready(函数(){
$('#login_btn')。单击(函数(){
//警报(“aaa”+validateLoginForm());
if(validateLoginForm()){
sprpst='j#u username='+$('j#u username').val()+'&j#u password='+$('j#u password').val();
$.post('j_spring_security_check',sprpst,函数(数据1){
window.location.reload();
}); 
$.blockUI({消息:'正在加载…});
}
});
});
这是spring安全登录

<form-login login-page="/login" default-target-url="/deals" authentication-failure-url="/deals?login_error=1"/>

任何帮助都将不胜感激

谢谢:)

您需要:

  • 提供自定义AuthenticationEntryPoint(可以使用默认的LoginRuthenAuthenticationEntryPoint作为基类)。在
    AuthenticationEntryPoint.start(…)
    方法中,检查这是否是AJAX调用,并返回403代码,而不是重定向到登录页面
  • 在Spring安全配置中设置自定义AuthenticationEntryPoint
  • 检查AJAX响应的代码。如果包含403,则显示关于错误凭据的相应错误消息
  • 自定义身份验证入口点的代码可能如下所示:

    public class CustomAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {
    
        @Override
        public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
            String header = request.getHeader("X-Requested-With");
            if (StringUtils.hasText(header) && header.equals("XMLHttpRequest") && authException != null) {
                response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
            } else {
                super.commence(request, response, authException);
            }
        }
    }
    

    您能否提供自定义AuthenticationEntryPoint的链接/示例代码?我不确定,您是否也需要将此代码放入自定义AuthenticationFailureHandler中。