Search code examples
thymeleafshiro

Accessing Shiro principal collection data from thymleaf


Using Shiro spring boot starter 1.4.0 in Spring boot application and can't figure out how i can extract shiro principalCollection data into my thymeleaf templates to use in other expressions etc...

I store some lightweight user data in shiro and wanted to display a custom message using current users first name and last login time

for example:

<p th:text="#{welcome.lastlogin.message(${???.firstName},${???.lastLoginTime})}">..Hello, user, how are you today?..</p>

I did dump the session attribute to see what shiro is storing the principals as and i see this in session:

org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY = [email protected],{userID=1,firstName=John, lastName=Doe, loginAttempts=0, lastLoginTime=Mon Jan 22 09:30:47 EST 2018}

My code uses a HashMap and adds it to the principals...

Is this even possible?

edit: I guess the other option is simply not use shiro principalCollection for this purpose and put my own object in session and use it from thymeleaf instead.


Solution

  • Take a look at thymeleaf-extras-shiro, It might do exactly what you are looking for. Otherwise, I'm guessing you would need to use principalCollection.getPrimaryPrincipal()