繁体   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