Get authenticated user entity Spring MVC

2019-07-31 15:02发布

问题:

I use Spring MVC + Spring Security in my project. And I need get user entity after user authenticates and across his session. For example, in order to print his name on my welcome page like: Welcome, ${user.firstName}! or get his list of companies on another page:

<c:forEach items="${user.userCompanies}" var="company">
    ${company.name}
</c:forEach>

etc. Could you help me to solve this issue?

回答1:

Check the taglib provided by Spring Security.
To get the username, use:

<sec:authentication property="principal.username" />