簡體   English   中英

Spring安全檢查用戶是否可以訪問提到的url

[英]Spring security check if user has access to mentioned url

我已經開始使用spring security,經過大量研究后我無法找到答案:

如果我明確要檢查用戶A是否可以訪問東西B.我可以使用JSP標記支持Spring Security檢查 - 檢查web url是否安全/受保護

<sec:authorize url="stuff/B">

但是如果我想在控制器(java類)中檢查相同的東西呢。 我在這里找不到任何彈簧函數來檢查登錄用戶是否可以訪問提到的URL( https://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html

來自javadoc的提示:

要使用此標記,還必須在應用程序上下文中有一個WebInvocationPrivilegeEvaluator實例。 如果您使用命名空間,將自動注冊。 這是DefaultWebInvocationPrivilegeEvaluator一個實例,“

DefaultWebInvocationPrivilegeEvaluator的javadoc中,我們可以看到應該執行該作業的isAllowed方法:

// privilegeEvaluator is a WebInvocationPrivilegeEvaluator "autowired"
boolean allowed = privilegeEvaluator.isAllowed("/stuff/B", yourAuthentication);

為什么不使用這樣的注釋:

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

注釋是Spring 3+的標准方法

您正在查找正確的位置,您附加的鏈接提到您需要的內容。 由於您希望在控制器上進行訪問控制並按用戶(而非角色)進行檢查,因此可以使用帶有“hasPermission”表達式的“@PreAuthorize”注釋或類似方法。

您可以在此處查看基於表達式的訪問控制, 此處還可以查看自定義安全表達式示例,以便您自定義解決方案。

1)首先我們需要知道用戶是否可以輸入URL。 使用WebInvocationPrivilegeEvaluator可以非常輕松地實現這一點。

privilegeEvaluator.isAllowed(contextPath, url, "GET", currentUser);

2)現在我們需要確定用戶是否可以訪問處理程序方法

private boolean isAllowedByAnnotation(Authentication currentUser, HandlerMethod method) {
    PreInvocationAuthorizationAdvice advice = new ExpressionBasedPreInvocationAdvice();
    PreInvocationAuthorizationAdviceVoter voter = new PreInvocationAuthorizationAdviceVoter(advice);

    MethodSecurityExpressionHandler expressionHandler = new DefaultMethodSecurityExpressionHandler();
    PrePostInvocationAttributeFactory factory = new ExpressionBasedAnnotationAttributeFactory(expressionHandler);
    PrePostAnnotationSecurityMetadataSource metadataSource = new PrePostAnnotationSecurityMetadataSource(factory);

    Class<?> controller = method.getBeanType();
    MethodInvocation mi = MethodInvocationUtils.createFromClass(controller, method.getMethod().getName());
    Collection<ConfigAttribute> attributes = metadataSource.getAttributes(method.getMethod(), controller);

    return PreInvocationAuthorizationAdviceVoter.ACCESS_GRANTED == voter.vote(currentUser, mi, attributes);
}

我們可以創建一個自定義的PermissionEvaluator並使用

hasPermission(身份驗證身份驗證,對象domainObject,對象權限)。

  @Override
  protected MethodSecurityExpressionHandler createExpressionHandler() {
    final DefaultMethodSecurityExpressionHandler expressionHandler =
        new DefaultMethodSecurityExpressionHandler();
    expressionHandler.setPermissionEvaluator(new AclPermissionEvaluator(aclService()));
    return expressionHandler;
  }

 @Bean
  public aclServiceImpl aclService() {
    final AclServiceImpl mutableAclService = new AclServiceImpl 
        (authorizationStrategy(), grantingStrategy());
    return mutableAclService;
  }

AclServiceImpl是MutableAclService的實現

最明顯有用的注釋是@PreAuthorize ,它決定是否可以實際調用方法。 例如(來自“Contacts”示例應用程序)

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

這意味着只有角色為“ROLE_USER ”的用戶才能訪問。 顯然,使用傳統配置和所需角色的簡單配置屬性可以輕松實現相同的目標。 但是關於:

@PreAuthorize("hasPermission(#contact, 'admin')")
public void deletePermission(Contact contact, Sid recipient, Permission permission);

這里我們實際上使用方法參數作為表達式的一部分來決定當前用戶是否具有給定聯系人的“admin”權限。 內置的hasPermission()表達式通過應用程序上下文鏈接到Spring Security ACL模塊。

有關詳細說明,請參閱此鏈接

暫無
暫無

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

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