2016-02-01 18 views
6

私はsec:authentication="name"sec:authorize="hasAnyRole(...)"を使用して、独自のHTMLタグの結果を生成する方法を知っているが、今のような表現でそれらを使用したい:sec:thymeleaf式で[何か]を使うには?

th:if="${hasAnyRole('ROLE_USER', 'ROLE_ADMIN') and someotherboolexpression}" 

はこれを行う方法はありますか?

答えて

10

thymeleaf-extras-springsecurityモジュールを使用すると、式ユーティリティオブジェクトを使用して、th:ifの中にSpringセキュリティの認証式を使用できます。実際に

<div th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')') and #authorization.expression('...') }"> 
    This will only be displayed if authenticated user has role ROLE_ADMIN. 
</div> 

あなたはタグライブラリを使用しているかのようにあなたがsec:authenticationsec:authorizeを使用できるように、この新しいモジュールによって追加の方言は、デフォルトの接頭辞としてsecを使用しています。

<div sec:authorize="hasRole('ROLE_ADMIN')"> 
    This will only be displayed if authenticated user has role ROLE_ADMIN. 
</div> 

<div sec:authentication="name"> 
    The value of the "name" property of the authentication object should appear here. 
</div> 

は、あなたがしなければならないのは、私は感謝、探していたまさにあなたのテンプレートエンジンの設定

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"> 
    ... 
    <property name="additionalDialects"> 
     <set> 
      <!-- Note the package would change to 'springsecurity3' if you are using that version --> 
      <bean class="org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect"/> 
     </set> 
    </property> 
    ... 
</bean> 
+0

をする方言を追加することです! – DiegoSahagun

+2

@Bnrdo Javaクラスを使って同じことを行う方法は? –

関連する問題