1

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

Bill_Data23
  • 659
  • 2
  • 14
  • 30
  • 1
    Instead of `principal.username` use `name` or `principal.name`. The `Principal` class indeed doesn't have a property `username` it has a property name. (Which would be easy to check if you check the api of `Principal`). – M. Deinum Jul 20 '16 at 14:19
  • thanks that were the problem, I used the following line to display the username `<@security.authentication property="name"> @security.authentication>`and it worked thanks, but I checked the API and they show me this example `` and that example didn't work it showed me error that put in my question. this is the API I checked http://docs.spring.io/spring-security/site/docs/3.0.x/reference/springsecurity-single.html#d0e6333 – Bill_Data23 Jul 20 '16 at 17:20
  • It's not related to your problem, but note that you are using a rather old FreeMarker version. That can cost you, because of less helpful error messages for example. – ddekany Jul 20 '16 at 20:05
  • Actually it looks like Freemarker isn't handling the expression properly, it should actually work. Have you tried a more recent version of Freemarker. Like 2.3.23 (not sure what the incubating 24 and 25 versions are...). – M. Deinum Jul 21 '16 at 06:18

2 Answers2

0

Apparently the expression doesn't resolve to the following Java call when used in Freemarker. (It has been too long for me to remember the exact syntax).

authentication.getPrincipal().getUsername();

This is also what the exception hints at that it cannot find the property named principal.username. Instead just write name which will get the name property of the current Authentication object.

<@security.authentication  property="name">
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
0

The following worked for me:

${Session.SPRING_SECURITY_CONTEXT.authentication.name}
holmis83
  • 15,922
  • 5
  • 82
  • 83