I found a solution.
Simply I made another Ajax call and there I captured logged user.
My JavaScript function
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 Controller
@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();
}
}