springauthenticationspring-bootsession-variablesprincipal

Avoid to get user informations inside every method/controller Spring Boot


I'm using Spring Boot, and I want to show inside my navbar the name and surname of the logged user.

To do that I have to call the principal inside every method of each controller and then I have to add the information to the model. I think there is a better way to do that. I think about the session attribute. Is it correct?

So my question is: Is the session attribute the best choice? If yes How can I do it?


Solution

  • You can use something along these lines directly in your jsp: <%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %> <security:authorize access="isAuthenticated()"> authenticated as <security:authentication property="principal.username" /> </security:authorize> Taken from baeldung