簡體   English   中英

Spring Security RESTful基於路徑的角色控制

[英]Spring security RESTful path based role control

例如

/user/{userId}/* # Only user with userId and admin can access
/order/{orderId}/* # Only the order owner of orderId and admin can access

當前解決方案@Current注釋是自定義注入,它與傳遞給服務器的token有關。 @PathVariable("user-id") UserEntity user是通過Spring-Data的路徑獲得的

@PreAuthorize("#user.id == #u?.id")
public UserDTO access(@P("user") @Current UserEntity requestUser,
                      @P("u") @PathVariable("user-id") UserEntity user)

@PreAuthorize("#user.id == #uid && (#order == null || #order?.user?.id == #uid)")
public Message access(@Current @P("user") UserEntity user,
                      @PathVariable("user-id") @P("uid") Long uid,
                      @PathVariable("order-id") @P("order") OrderEntity order)

我們有太多注釋,是否有任何簡單的方法來配置它們?

試過了

  1. 使用.antMatchers("/user/[0-9]+/*").hasRole("ROLE_USER")無法自定義用戶檢查。
  2. AOP太復雜,不能基於url。

我建議您使用方法安全性來實現細粒度的邏輯來加強資源訪問。 我認為基於網址的身份驗證僅對簡單的用例有效。

如果授權邏輯需要幾行代碼,我還建議使用帶有自定義批注的AOP來實現方法安全性(而不是使用@PreAuthorize )。

例如,您可以攔截帶注釋的方法調用:

@Before("@annotation(your.annotations.AllowedToOwner) && @annotation(ann)")
public void checkOwner(JoinPoint joinPoint, AllowedToOwner ann) throws Throwable {

    // check owner, throws AccessDeniedException if check fails...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM