回答:
本当の違いは、Spring Expression Language(SpEL)で@PreAuthorize動作できることです。あなたはできる:
SecurityExpressionRoot。アクセスメソッドの引数(デバッグ情報またはカスタムでのコンパイルが必要ParameterNameDiscoverer):
@PreAuthorize("#contact.name == principal.name")
public void doSomething(Contact contact)
MethodSecurityExpressionHandlerしてとして設定<global-method-security><expression-handler ... /></...>)。@PreAuthorizeは異なり、それよりも強力です@Secured。
古い
@Securedアノテーションでは、式を使用できませんでした。
Spring Security 3以降では、Spring Expression Language(SpEL)をサポートし、式ベースのアクセス制御を提供するため、より柔軟なアノテーション
@PreAuthorizeと@PostAuthorize(@PreFilterおよび@PostFilterと同様)が推奨されます。
@Secured("ROLE_ADMIN")アノテーションはと同じ@PreAuthorize ("hasRole('ROLE_ADMIN')")です。
これ
@Secured({"ROLE_USER","ROLE_ADMIN")は、ROLE_USER OR ROLE_ADMIN と見なされます。
したがって、AND条件を使用して表現することはできません
@Secured。同じことをで定義できます
@PreAuthorize("hasRole('ADMIN') OR hasRole('USER')")。これは理解しやすくなります。AND、OR、またはNOT(!)も表現できます。@PreAuthorize( "!isAnonymous()AND hasRole( 'ADMIN')")
"hasRole('ADMIN OR hasRole('USER')"か?
+-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
| | @Secured | @PreAuthorize |
+-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
| Spring EL expressions | Does'nt supports. | Supports |
+-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
| Multiple roles conjunctions with AND operator | Does'nt supports.(If there are multiple roles defined | Supports |
| |they will be automatically combined with OR operator) | |
+-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
| To enable annotation | Add following line to spring-security.xml | Add following line to spring-security.xml |
| | <global-method-security secured-annotations="enabled" /> | <global-method-security pre-post-annotations="enabled"/> |
+-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
| Example | @Secured({ROLE_ADMIN , ROLE_USER}) | @PreAuthorize("hasRole('ROLE_USER') and hasRole('ROLE_ADMIN')") |
| | public void addUser(UserInfo user){...} | public void addUser(UserInfo user){...} |
+-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+