Possible Duplicate:
When using Spring Security, what is the proper way to obtain current username (i.e. SecurityContext) information in a bean?
I am using spring security and i would like to know how to retrive the current logged in username in my controller. Can i store the userid associated to login username also somewhere? Currently i am getting the username from Priciple object
public boolean populateUserName(ModelMap model, Principal principal) {
if (principal != null) {
String name = principal.getName();
model.addAttribute("username", name);
System.out.println("userName - " + name);
return true;
}
else
{
System.out.println("principal is null");
return false;
}
}
I am getting this Principal object in every controller method. I dont know if this is a optimal way to get it. For example here is the code
@RequestMapping(value = "/secure/myitem.htm", method = RequestMethod.GET)
public ModelAndView showItem(@RequestParam("listingId") String listingId,
ModelMap model, Principal principal) {
populateUserName(model, principal);
}