How to display current logged-in user's information in all templates including view managed by WebMvcConfigurerAdapter in Spring Security application
It's quite easy to accomplish this, thanks to a hint from Balaji Krishnan.
Basically, I had to add the Thymeleaf Spring Security integration module to my build.gradle file as follows:
compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity3")
Then in my template I just used the following markup:
<span th:text ="${#authentication.getPrincipal().getUser().getFirstName()}"></span>
When using Spring Security 4 and Thymeleaf 3:
<span th:text="${#authentication.getPrincipal().getUsername()}"></span>
When using Spring boot 2.2.1.
For the maven, Add these lines to the pom.xml
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
In the thymeleaf
<span th:text="${#authentication.getPrincipal().getUsername()}"></span>
<span th:text="${#authentication.getPrincipal().authorities}"></span>