How do I access the username of a logged in user in Struts2?
I am using Spring Security and Struts2 in my web application project. I want to store the username for the logged in user in a field <s:hidden>
. Is it possible? If not, which is the best alternative?
<sec:authentication property="name" />
<s:hidden name="value" value="%{name}" />
Many thanks
+3
source to share
1 answer
You can get the username of the logged in user, in fact UserDetails
, using:
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Returns an object org.springframework.security.core.userdetails.UserDetails
. You can obtain username
, authorities
from the object UserDetails
.
+3
source to share