2015年1月8日 星期四

[Spring Security] Get Current User

To get the current authentication:

Method 1
      Authentication auth = SecurityContextHolder.getContext().getAuthentication();
      String name = auth.getName(); //get logged in username
 Method 2
User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String name = user.getUsername(); //get logged in username

http://www.mkyong.com/spring-security/get-current-logged-in-username-in-spring-security/

沒有留言:

張貼留言