2016-08-11 31 views
3

我試圖確定登錄的用戶是否是特定Stormpath組的成員。使用Thymeleaf檢查特定的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> 

回答

5

通常的部分,你會想,讓服務器端(控制器)做繁重。

因此,在控制器,你可能有這樣的事情:

@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> 

結果:

enter image description here

完全披露:我是Stormapth的Java開發者Evangelis t

1

貌似推薦的方法是將其發送到Thymeleaf渲染之前做的賬戶組訪問的決心從控制器內。基本上你可以添加一個與你正在檢查的內容相關的模型屬性,然後在填充該屬性的Controller中進行檢查。在模板中,然後驗證屬性是否按照您的預期設置。

https://stormpath.com/blog/build-spring-boot-spring-security-app - 閱讀「春季安全訪問控制通過組成員」

相關問題