我已经开始使用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
”
而在javadoc中 DefaultWebInvocationPrivilegeEvaluator
,我们可以看到一个 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(身份验证身份验证,Object 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模块。
有关详细说明,请参阅此处 链接