Is there a "getCurrentUser
" method in Spring to access the user that is currently part of a request - even if that user's name is not being passed around as part of a web request?
views:
42answers:
1
+4
A:
Since you have tagged your question with spring-security I assume your question is in same context. With spring-security you can retrieve current user as :
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
UserDetails userDetails = null;
if (principal instanceof UserDetails) {
userDetails = (UserDetails) principal;
}
String userName = userDetails.getUsername();
Gopi
2010-08-19 16:14:04
Also Spring Security exposes its authentication data via `HttpServletRequest.getPrincipal()`.
axtavt
2010-08-19 16:21:56