使用 Thymeleaf 检查特定的 Stormpath 组成员

Use Thymeleaf to check for specific Stormpath group membership

我正在尝试确定登录用户是否是特定 Stormpath 组的成员。

以下有效,但它基于错误的属性 (groups.href)

<div th:text="${#lists.contains(account.groups.href, 'https://api.stormpath.com/v1/accounts/eXaMpLe12345/groups')}" />

而我正在尝试查找(伪代码)groups.isMemberOf('https://api.stormpath.com/v1/accounts/mYgRrOuP1234/groups')

我知道如何遍历组并打印出它们的所有数据,我可以看到我试图找到的组的 href,但我不知道如何通过它的 href 访问它。

<div th:if="${account.groups != null}">
    <ul>
       <li th:each="group : ${account.groups}" th:text="${group.href}">group details...</li>
    </ul>
</div>

看起来推荐的方法是在将其发送到 Thymeleaf 进行渲染之前,先从 Controller 中确定帐户组访问权限。基本上,您添加一个与您正在检查的内容相关的模型 属性,并在填充该 属性 的控制器中执行该检查。然后在模板中验证 属性 是否按预期设置。

https://stormpath.com/blog/build-spring-boot-spring-security-app - 阅读名为 "Spring Security Access Control By Group Membership"

的部分

通常,您希望让服务器端(控制器)完成繁重的工作。

所以,在控制器中,你可能有这样的东西:

@RequestMapping("/")
public String home(HttpServletRequest request, Model model) {

    Account account = AccountResolver.INSTANCE.getAccount(request);

    Group group = client.getResource("https://api.stormpath.com/v1/groups/qjWFpHyC5q6NdakGFCfrb", Group.class);

    if (account != null) {
        model.addAttribute("account", account);
        model.addAttribute("group", group);
        model.addAttribute("isMemberOfGroup", account.isMemberOfGroup(group.getHref()));
    }

    return "hello";
}

然后,在您的 thymeleaf 视图中,您可以:

    <div th:if="${account}">
        <h4 th:if="${isMemberOfGroup}" th:text="${account.fullName} + ' is a member of the ' + ${group.name} + ' group.'"></h4>
        <h4 th:text="'Account Store: ' + ${account.Directory.Name}"></h4>
        <h4 th:text="'Provider: ' + ${account.ProviderData.ProviderId}"></h4>
    </div>

结果:

完全披露:我是 Stormapth 的 Java 开发者布道师