If you are using Spring Security ver >= 3.2, you can use the @AuthenticationPrincipal
annotation:
@RequestMapping(method = RequestMethod.GET)
public ModelAndView showResults(@AuthenticationPrincipal CustomUser currentUser, HttpServletRequest request) {
String currentUsername = currentUser.getUsername();
// ...
}
Here, CustomUser
is a custom object that implements UserDetails
that is returned by a custom UserDetailsService
.
More information can be found in the @AuthenticationPrincipal chapter of the Spring Security reference docs.