从 thymeleaf 访问 Shiro 主要收集数据

Accessing Shiro principal collection data from thymleaf

在 Spring 启动应用程序中使用 Shiro spring boot starter 1.4.0,但无法弄清楚如何将 shiro principalCollection 数据提取到我的 thymeleaf 模板中以用于其他表达式等。 .

我在 shiro 中存储了一些轻量级用户数据,并希望使用当前用户的名字和上次登录时间显示自定义消息

例如:

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

我确实转储了会话属性以查看 shiro 将主体存储为什么,我在会话中看到了这个:

org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY = john.doe@somewhere.com,{userID=1,firstName=John, lastName=Doe, loginAttempts=0, lastLoginTime=1 月 22 日星期一 09:30:47 2018 年美国东部时间}

我的代码使用 HashMap 并将其添加到主体中...

这可能吗?

编辑:我想另一种选择就是不为此目的使用 shiro principalCollection,而是将我自己的对象放入会话中,然后从 thymeleaf 中使用它。

看看 thymeleaf-extras-shiro,它可能正是您要查找的内容。否则,我猜你需要使用 principalCollection.getPrimaryPrincipal()