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/

沒有留言:

張貼留言

Weight Loss! The journey continues

是一個漫長的奮鬥旅程…… 最近嘗試努力改變的生活習慣: 晚上九時半去瞓覺 瞓覺前唔用電子奶咀,即喺ipad同手提電話(因為呢家部電話用耐咗,會無電要叉過夜) 用蔬果代替日常零食,例子有:車厘茄、藍莓、士多啤梨、粟米芯、提子,都係細細地嘅一舊一舊comp食啲 唔食熱氣零食 安靜去每...