2016-08-11 8 views
3

ログインしているユーザーが特定のストームパスグループのメンバーであるかどうかを確認しようとしています。Thymeleafを使用して特定のストームパスグループメンバーシップを確認する

以下の作品が、私は(擬似コード)groups.isMemberOf('https://api.stormpath.com/v1/accounts/mYgRrOuP1234/groups')

を見つけようとしています一方、それは間違っている属性(groups.href)

<div th:text="${#lists.contains(account.groups.href, 'https://api.stormpath.com/v1/accounts/eXaMpLe12345/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 - 通常

関連する問題