Support

get current user in velocity

  • 1.  get current user in velocity

    Posted 06-14-2017 10:53
    A little bit of reading spring and hibernate documentation with a little bit of qualified guessing brought the following result. The output of that velocity report contains some steps of the guessing, maybe this helps in similar situations.

    #set($securityContextHolder = $applicationContext.getClassLoader().loadClass("org.springframework.security.core.context.SecurityContextHolder"))
    $securityContextHolder.getContext().getAuthentication()
    </br>
    </br>
    $securityContextHolder.getContext().getAuthentication().getPrincipal()
    </br>
    </br>
    $securityContextHolder.getContext().getAuthentication().getPrincipal().getUser()
    </br>
    </br>
    $securityContextHolder.getContext().getAuthentication().getPrincipal().getUser().getUserName()
    </br>
    </br>
    $securityContextHolder.getContext().getAuthentication().getPrincipal().getUser().getLastName()
    </br>
    </br>
    $securityContextHolder.getContext().getAuthentication().getPrincipal().getUser().getFirstName()
    </br>
    </br>
    $securityContextHolder.getContext().getAuthentication().getPrincipal().getUser().getId()
    </br>
    </br>
    $securityContextHolder.getContext().getAuthentication().getPrincipal().getUser().getEmail()
    </br>
    </br>
    $securityContextHolder.getContext().getAuthentication().getPrincipal().getUser().getRoles()
    </br>
    </br>
    $securityContextHolder.getContext().getAuthentication().getDetails()

    The how to get there used the following link:
    Get User details from Session in Spring Security
    The guessing was that the UserDetails are at the end a user object being accessible with getUser providing a user object.

    ------------------------------
    Harald Hotz-Behofsits
    Frequentis AG
    ------------------------------