【问题标题】:What's the difference between @Secured and @PreAuthorize in spring security 3?spring security 3 中的@Secured 和@PreAuthorize 有什么区别?
【发布时间】:2011-09-11 12:57:21
【问题描述】:

我不清楚 spring security 之间有什么区别:

 @PreAuthorize("hasRole('ROLE_USER')")
 public void create(Contact contact)

还有

@Secured("ROLE_USER")
public void create(Contact contact)

我知道 PreAuthorize 可以与 spring el 一起使用,但在我的示例中,有真正的区别吗?

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    真正的区别在于@PreAuthorize 可以与Spring Expression Language (SpEL) 一起使用。你可以:

    • SecurityExpressionRoot 的访问方法和属性。
    • 访问方法参数(需要使用调试信息或自定义 ParameterNameDiscoverer 进行编译):

      @PreAuthorize("#contact.name == principal.name")
      public void doSomething(Contact contact)
      
    • (高级功能)添加您自己的方法(覆盖MethodSecurityExpressionHandler并将其设置为<global-method-security><expression-handler ... /></...>)。

    【讨论】:

    • 不知道这个,但看起来很棒! :D
    【解决方案2】:

    简单地说, @PreAuthorize@Secured 更新。

    所以我说最好使用@PreAuthorize,因为它是“基于表达式”的,您可以使用 hasRole、hasAnyRole、permitAll 等表达式。

    要了解表达式,请参阅这些example expressions

    【讨论】:

      【解决方案3】:

      如果您想仅在用户具有 Role1 Role2 时访问该方法,那么您将不得不使用 @PreAuthorize

      @PreAuthorize("hasRole('ROLE_role1') and hasRole('ROLE_role2')")
      

      使用

      @Secured({"role1", "role2"}) // is treated as an OR
      

      【讨论】:

        【解决方案4】:

        @PreAuthorize 不同,它比@Secured 更强大。

        • 旧的@Secured 注释不允许使用表达式。

        • 从 Spring Security 3 开始,更灵活的注解 @PreAuthorize@PostAuthorize(以及 @PreFilter 和 @PostFilter) 是首选,因为它们支持 Spring 表达式 语言 (SpEL) 并提供基于表达式的访问控制。

        • @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')"没有错误吗?
        【解决方案5】:
        +-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
        |                                               |                         @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){...}                         |
        +-----------------------------------------------+----------------------------------------------------------+-----------------------------------------------------------------+
        

        【讨论】:

          猜你喜欢
          • 2016-07-18
          • 2019-04-18
          • 2011-01-23
          • 2018-01-18
          • 1970-01-01
          • 2019-11-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多