使用Ajax的Spring安全性&x2013;记录的用户详细信息

使用Ajax的Spring安全性&x2013;记录的用户详细信息,ajax,spring-security,user-data,logged,Ajax,Spring Security,User Data,Logged,我已经使用AuthenticationEntryPoint和SimpleRulthenticationSuccessHandler通过Ajax实现了用户身份验证 现在我需要将登录的用户名输入到脚本变量中 有人能帮我吗 MyAuthenticationSuccessHandler public class MyAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { private Log l

我已经使用AuthenticationEntryPoint和SimpleRulthenticationSuccessHandler通过Ajax实现了用户身份验证

现在我需要将登录的用户名输入到脚本变量中

有人能帮我吗

MyAuthenticationSuccessHandler

public class MyAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    private Log log = LogFactory.getLog(MyAuthenticationSuccessHandler.class);

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {

        log.info("point-2-->"+authentication.getName()); //this prints what I need.

        // This is actually not an error, but an OK message. It is sent to avoid redirects.
        response.sendError(HttpServletResponse.SC_OK);

    }
}
@RequestMapping(value = "/api/loggeduser", method = RequestMethod.POST)
       public String printWelcome(ModelMap model, Principal principal ) {

        try {

            String name = null;

            if (principal!=null) {

           name = principal.getName();

          }

                model.addAttribute("username", name);



    } catch (Exception e) {
        e.printStackTrace();
    }
  }
我的JavaScript函数

$("#login").on('click', function(e) {
        e.preventDefault();
        $.ajax({url: getHost() + "/j_spring_security_check",
            type: "POST",
            beforeSend: function(xhr) {
                xhr.withCredentials = true;
            },
            data: $("#loginForm").serialize(),
            success: function(response, options) {
                    // We get a success from the server
                    //I need to get user name here
            },
            error: function(result, options) {
                    // We get a failure from the server...
                $(".error").remove();
                $('#j_username').before('<div class="error">Login failed, please try again.</div>');
        }


        });
    });
success: function(response, options) {
                    // We get a success from the server
                    //I need to get user name here

    $.post('/api/loggeduser', function(data) {
           $('#loggeduser').html(data);
        }); 
},
$(“#登录”)。在('click',函数(e){
e、 预防默认值();
$.ajax({url:getHost()+“/j_spring_security_check”,
类型:“POST”,
发送前:函数(xhr){
xhr.withCredentials=true;
},
数据:$(“#loginForm”).serialize(),
成功:功能(响应、选项){
//我们从服务器上获得了成功
//我需要在这里获取用户名
},
错误:函数(结果、选项){
//我们从服务器上得到一个故障。。。
$(“.error”).remove();
$(“#j#u username”)。之前('登录失败,请重试');
}
});
});
我已经附上了不同问题的所有相关文件。请访问下面的链接进行检查

我找到了解决办法

简单地说,我做了另一个Ajax调用,并在那里捕获了登录的用户

我的JavaScript函数

$("#login").on('click', function(e) {
        e.preventDefault();
        $.ajax({url: getHost() + "/j_spring_security_check",
            type: "POST",
            beforeSend: function(xhr) {
                xhr.withCredentials = true;
            },
            data: $("#loginForm").serialize(),
            success: function(response, options) {
                    // We get a success from the server
                    //I need to get user name here
            },
            error: function(result, options) {
                    // We get a failure from the server...
                $(".error").remove();
                $('#j_username').before('<div class="error">Login failed, please try again.</div>');
        }


        });
    });
success: function(response, options) {
                    // We get a success from the server
                    //I need to get user name here

    $.post('/api/loggeduser', function(data) {
           $('#loggeduser').html(data);
        }); 
},
loggeduser控制器

public class MyAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    private Log log = LogFactory.getLog(MyAuthenticationSuccessHandler.class);

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {

        log.info("point-2-->"+authentication.getName()); //this prints what I need.

        // This is actually not an error, but an OK message. It is sent to avoid redirects.
        response.sendError(HttpServletResponse.SC_OK);

    }
}
@RequestMapping(value = "/api/loggeduser", method = RequestMethod.POST)
       public String printWelcome(ModelMap model, Principal principal ) {

        try {

            String name = null;

            if (principal!=null) {

           name = principal.getName();

          }

                model.addAttribute("username", name);



    } catch (Exception e) {
        e.printStackTrace();
    }
  }
我找到了解决办法

简单地说,我做了另一个Ajax调用,并在那里捕获了登录的用户

我的JavaScript函数

$("#login").on('click', function(e) {
        e.preventDefault();
        $.ajax({url: getHost() + "/j_spring_security_check",
            type: "POST",
            beforeSend: function(xhr) {
                xhr.withCredentials = true;
            },
            data: $("#loginForm").serialize(),
            success: function(response, options) {
                    // We get a success from the server
                    //I need to get user name here
            },
            error: function(result, options) {
                    // We get a failure from the server...
                $(".error").remove();
                $('#j_username').before('<div class="error">Login failed, please try again.</div>');
        }


        });
    });
success: function(response, options) {
                    // We get a success from the server
                    //I need to get user name here

    $.post('/api/loggeduser', function(data) {
           $('#loggeduser').html(data);
        }); 
},
loggeduser控制器

public class MyAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

    private Log log = LogFactory.getLog(MyAuthenticationSuccessHandler.class);

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {

        log.info("point-2-->"+authentication.getName()); //this prints what I need.

        // This is actually not an error, but an OK message. It is sent to avoid redirects.
        response.sendError(HttpServletResponse.SC_OK);

    }
}
@RequestMapping(value = "/api/loggeduser", method = RequestMethod.POST)
       public String printWelcome(ModelMap model, Principal principal ) {

        try {

            String name = null;

            if (principal!=null) {

           name = principal.getName();

          }

                model.addAttribute("username", name);



    } catch (Exception e) {
        e.printStackTrace();
    }
  }