2017-12-22 17 views
0

メソッドのセキュリティ式を格納するクラスがあります。Thymeleaf `sec:authorize`属性でSpring EL式を使用する方法

public final class MethodSecurityExpressions { 
    public static final String USER = "hasRole('USER')"; 
} 
私はこのようにそれを使用しましたコントローラーで

、Thymeleafのテンプレートで

@PreAuthorize(MethodSecurityExpressions.USER) 
@GetMapping("path/to/list") 
public String list(Model model) { 
    return "list"; 
} 

私は現在、以下のことをやっている、

<ul sec:authorize="hasRole('USER')"> 
    <li>...</li> 
</ul> 

が、私はこのような何かをしたいです、

<ul sec:authorize="#{MethodSecurityExpressions.USER}"> 
    <li>...</li> 
</ul> 

私は使用していますスプリングブート1.5.8。私はJSP tag library documentationThymeleaf documentationを読んだことがあり、何か有望なものは見つけられません。

これが可能かどうか、またはこれを達成するための同様の方法を知っている人はいますか?

答えて

1

sec:authorize属性は、スプリングセキュリティ式を評価します。この式は実際、SpringSecurity固有のルートオブジェクトで評価されるSpring EL式です。

<div sec:authorize="${hasRole('#{T(org.example.MethodSecurityExpressions).USER)}'"> 
</div> 

クラスMethodSecurityExpression

package org.example; 

public final class MethodSecurityExpressions { 
    public static final String USER = "USER"; 
} 

出典:

https://github.com/thymeleaf/thymeleaf-extras-springsecurity https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/html/expressions.html

をそのための適切な解決策は次のようになります
関連する問題