2013-03-26 108 views
1

我正在使用spring security登录+ ajax登录。如果用户提供了错误的凭据,我需要显示一条消息,指出“无效的用户/密码”使用spring security和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>' }); 
    } 
}); 
}); 

这是春天的安全登录

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

任何帮助将不胜感激。

谢谢:)

回答

2

您需要:

  1. 提供一个自定义的AuthenticationEntryPoint(也可以使用默认LoginUrlAuthenticationEntryPoint作为基类)。在AuthenticationEntryPoint.commence(...)方法检查这是AJAX调用并返回403代码,而不是重定向到登录页面。
  2. 在Spring Security conf中设置您的自定义AuthenticationEntryPoint。
  3. 检查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); 
     } 
    } 
} 
+0

可以ü请自定义提供的链接/示例代码的AuthenticationEntryPoint – prakash 2013-03-26 10:04:47

+0

我不知道我的回答是更新 – 2013-03-26 10:29:26

+0

,可能是你需要将此代码放置在自定义的AuthenticationFailureHandler中。 – 2013-03-26 12:36:01

相关问题