I want to access the current logged in user I am doing it like this (from a static method)
public static User getCurrentUser() {
final Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal instanceof User) {
return (User) principal;
}
}
or injecting and casting like this :
@RequestMapping(value = "/Foo/{id}", method = RequestMethod.GET)
public ModelAndView getFoo(@PathVariable Long id, Principal principal) {
User user = (User) ((Authentication) principal).getPrincipal();
..
Where user implements userdetails, both seem a bit lame is there a better way in Spring 3.2 ?
I don’t think that it has something new in
spring 3.2for that purpose. Have you thought about using a custom annotation?Something like this :
The controller with the custom annotation :
The LoggedUser annotation :
The WebArgumentResolver :
Beans configuration :