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/

沒有留言:

張貼留言

日常秘技小輯錄

媽媽傳授: 用爉燭塗在線上縫紉,可以更容易打結,以及拉縫線更順 爸爸傳授: 貼玻璃紙前在玻璃面塗上肥皂水,可以移動玻璃紙不留摺痕