10 Haziran 2009 Çarşamba

Obtaining Information About The Current User In Spring Security

The details of the principal currently interacting with the application is stored inside SecurityContextHolder. Spring Security uses an Authentication object to represent this information. Use the following code block -from anywhere in your application- to obtain the name and the password of the currently authenticated user:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails principal = (UserDetails) authentication.getPrincipal();
String username = principal.getUsername();
String password = principal.getPassword();