Get current logged in username in Spring Security
In this article, we show you three ways to get current logged in username in Spring Security.
1. SecurityContextHolder + Authentication.getName()
import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class LoginController { @RequestMapping(value="/login", method = RequestMethod.GET) public String printUser(ModelMap model) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String name = auth.getName(); //get logged in username model.addAttribute("username", name); return "hello"; } //...
2. SecurityContextHolder + User.getUsername()
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.User; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class LoginController { @RequestMapping(value="/login", method = RequestMethod.GET) public String printUser(ModelMap model) { User user = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); String name = user.getUsername(); //get logged in username model.addAttribute("username", name); return "hello"; } //...
3. UsernamePasswordAuthenticationToken
This is more elegant solution, in runtime, Spring will inject “UsernamePasswordAuthenticationToken” into the “Principal” interface.
import java.security.Principal; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class LoginController { @RequestMapping(value="/login", method = RequestMethod.GET) public String printWelcome(ModelMap model, Principal principal ) { String name = principal.getName(); //get logged in username model.addAttribute("username", name); return "hello"; } //...
Download Source Code
Download it – Spring-Security-Get-Logged-In-Username.zip (9 KB)
References
Tags : spring security username

Hi Mkyong,
I have to say thank you. Your tutorials had helped me a lot. I have a question related to how to get the current username. I have a table in MySQL with some fields (username, password, enabled, name) my question is: Is there anyway to return the value of the “name” field in that table? and how can i do that?
Thanks in advance.
Greetings
Hi mkyong, i’m trying to get the userPrincipal from any bean in my JSF application, but i’m getting a null pointer. I think that is because the spring getContext uses a ThreadLocal.
Do you have any idea ?
See this post:
http://www.lejava.com.br/java/jsf/jsf-2-spring-spring-security-3-and-database
Do you know if i use @ManagedProperty in any Bean, i will get the userPrincipal correctly ?
Thanks in advance
how can retrieve userid using spring security?
i am spring security in my current project..i have following code in springsecurity.taglib.xml
http://www.springframework.org/security/tags
authorize
org.springframework.faces.security.FaceletsAuthorizeTagHandler
areAllGranted
org.springframework.faces.security.FaceletsAuthorizeTagUtils
boolean areAllGranted(java.lang.String)
areAnyGranted
org.springframework.faces.security.FaceletsAuthorizeTagUtils
boolean areAnyGranted(java.lang.String)
areNotGranted
org.springframework.faces.security.FaceletsAuthorizeTagUtils
boolean areNotGranted(java.lang.String)
isAllowed
org.springframework.faces.security.FaceletsAuthorizeTagUtils
boolean isAllowed(java.lang.String, java.lang.String)
i want add new tag authentication…how can i add that
pls help me
first of all, your site is very helpful . It makes things a whole lot easier for me.
I m trying your first example , i am unable to get the authentication object .
“Authentication auth = SecurityContextHolder.getContext().getAuthentication();”
i am getting null value. I m following your code , but i m not able to figure out how to resolve this. Following is the error message generated for me.
I think you mean RequestMapping as “/welcome” instead of “/login” that you have put. You will get hold of principal (USer) only after successful authentication.
- K. Arun
Come ti permetti ? ;) mkyoung sa il fatto suo ;)
This is my login function