使用Spring Security的AngularJS/AJAX身份验证-返回JSON响应

使用Spring Security的AngularJS/AJAX身份验证-返回JSON响应,angularjs,spring,spring-mvc,spring-security,Angularjs,Spring,Spring Mvc,Spring Security,Spring安全配置 <security:form-login login-page="/login" default-target-url="/myapp" always-use-default-target="true" authentication-success-handler-ref="myAuthenticationSuccessHandler" authentication-failure-handler-ref="myAut

Spring安全配置

<security:form-login login-page="/login"
        default-target-url="/myapp" always-use-default-target="true"
        authentication-success-handler-ref="myAuthenticationSuccessHandler" 
        authentication-failure-handler-ref="myAuthenticationFailureHandler" />

<bean id="myAuthenticationSuccessHandler"
        class="com.myapp.auth.AjaxAuthenticationSuccessHandler" />

<bean id="myAuthenticationFailureHandler"
        class="com.myapp.auth.AjaxAuthenticationFailureHandler" />
问题:
登录表单作为表单Post通过AJAX/Angular应用程序提交,但响应应为JSON(无重定向)

登录成功案例成功调用onAuthenticationSuccess,因此我可以返回JSON。
但是登录失败案例根本不调用onAuthenticationFailure。相反,我直接看到401响应。


我做错了什么?或其他实现此行为的方法?

AjaxAuthenticationFailureHandler中onAuthenticationFailure的正确方法签名为(注意此方法的最后一个参数):


这就解决了问题。

AjaxAuthenticationFailureHandler中onAuthenticationFailure的正确方法签名是(请注意此方法的最后一个参数):


这就解决了问题。

在使用
SimpleRuthenticationSuccessHandler时,它是否帮助您管理会话或登录会话?因为我想使用的是
SavedRequestAwareAuthenticationSuccessHandler
。我不太清楚他们之间的区别。你能帮忙吗?agpt>我不知道SavedRequestStataWareAuthenticationSuccessHandler。。很抱歉。也许可以查看javadocs…好的。。没问题,但您能否简要介绍一下您在javascript端维护会话/cookie的具体情况?你遇到过什么问题吗?Thanksagpt>这是由Spring Security自动完成的-我们不需要显式地处理它。使用任何浏览器开发工具或Fiddler,您都可以看到http与Cookie等的信息交换。它是否有助于您在使用
SimpleRuthenticationSuccessHandler
时管理会话或登录会话?因为我想使用的是
SavedRequestAwareAuthenticationSuccessHandler
。我不太清楚他们之间的区别。你能帮忙吗?agpt>我不知道SavedRequestStataWareAuthenticationSuccessHandler。。很抱歉。也许可以查看javadocs…好的。。没问题,但您能否简要介绍一下您在javascript端维护会话/cookie的具体情况?你遇到过什么问题吗?Thanksagpt>这是由Spring Security自动完成的-我们不需要显式地处理它。使用任何浏览器开发工具或Fiddler,您都可以看到http与Cookie等进行信息交换。
public class AjaxAuthenticationSuccessHandler extends
SimpleUrlAuthenticationSuccessHandler {

public void onAuthenticationSuccess(HttpServletRequest request,
    HttpServletResponse response, Authentication auth)
    throws IOException, ServletException {

    response.getWriter().print(
            "{'login': 'SUCCESS'}");
    response.getWriter().flush();

}}


public class AjaxAuthenticationFailureHandler extends
SimpleUrlAuthenticationFailureHandler {

public void onAuthenticationFailure(HttpServletRequest request,
    HttpServletResponse response, Authentication auth)
    throws IOException, ServletException {
    System.out.println("Login Failed...");
    response.getWriter().print(
            "{'login': 'FAIL'}");
    response.getWriter().flush();

}}
public void onAuthenticationFailure(javax.servlet.http.HttpServletRequest request, 
        javax.servlet.http.HttpServletResponse response, AuthenticationException exception)
{

    try {
        response.getWriter().print(
                "{'login': 'FAILURE'}");
        response.getWriter().flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}