How can I show the username of a user that logged in on the header of every page, I try using the following methods
1) this spring security tags like this:
first I create a variable security like this
<#assign security=JspTaglibs["http://www.springframework.org/security/tags"] />
and then I try to get the username using this authentication
tag method that I investigated
<@security.authentication property="principal.username">
</@security.authentication>
but I get this error
FreeMarker template error:
org.springframework.beans.NotReadablePropertyException: Invalid property 'principal.username' of bean class [org.springframework.security.authentication.UsernamePasswordAuthenticationToken]: Bean property 'principal.username' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
2) I tried getting the username in the controllers and put it in the view like this
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
String name = auth.getName(); //get logged in username
map.addAttribute("username" , name);
that worked but I have too many controllers and I don't want to update all my controllers and put those lines in all my controllers.
those are my versions
<spring.version>4.2.5.RELEASE</spring.version>
<spring.security.version>4.1.0.RELEASE</spring.security.version>
<artifactId>spring-security-taglibs</artifactId>
<version>4.0.4.RELEASE</version>
<artifactId>freemarker</artifactId>
<version>2.3.21</version>
Any solution is welcome thanks