【问题标题】:Spring SPEL object reference failing to "dereference" method input argument objectSpring SPEL 对象引用未能“取消引用”方法输入参数对象
【发布时间】:2016-09-29 19:53:02
【问题描述】:

我正在使用具有 ACL 级别权限的 Spring Security(版本 4.0.3.RELEASE) - 我正在尝试使用 @PreAuthorize 注释并调用 SPEL 方法,将 PreAuthorize ACL 的权限应用于我的 CrudRepository 接口中的删除方法“hasPermission”(见下文)。 hasPermission 使用我在多个论坛、教程和参考文档中看到的哈希表示法引用输入参数,但输入参数(我要删除的对象)没有被 SPEL 解析器拾取或支持框架。所以,换句话说,我想删除名为“apiKey”的对象,它是下面的输入参数。我使用“#apiKey”在 SPEL 注释中引用它,但是当我在方法 org.springframework.security.acls.AclPermissionEvaluator.hasPermission(...) 中放置断点时,“domainObject”输入参数为 NULL。

我对 Spring 和 ACL 还很陌生,所以我肯定做错了什么。我尝试在 Spring SPEL Parser 中放置断点并在运行时遍历,但我找不到基于 SPEL 令牌解析反射参数的代码段。我认为它“隐藏”在本机方法或其他东西中。

我也尝试过使用 SPEL 参考语法。似乎没有其他东西可以正确解析,所以我认为“#argument_name”是正确的语法——只是 SPEL 未能取消引用参数对象。

带有对象引用(“#apiKey”)的我的 CrudRepository 代码不起作用:

public interface ApiKeyRepository extends CrudRepository<ApiKey, Long> {
    @Override
    @PreAuthorize("hasPermission(#apiKey, 'DELETE')")
    void delete(ApiKey apiKey);
}

Spring Security 代码 应该 接收我的 apiKey 对象,但得到的是 domainObject 的 NULL 值:

public class AclPermissionEvaluator implements PermissionEvaluator {
    ...
    public boolean hasPermission(Authentication authentication, Object domainObject, Object permission) {
        if (domainObject == null) {
            return false; //<== This is where I'm getting access denied from
        }
        ...

关于为什么 SPEL 无法通过 apiKey 输入参数正确引用的任何想法?提前致谢。

【问题讨论】:

    标签: java spring spring-security acl spring-el


    【解决方案1】:

    感谢THIS POST 引用了THIS DOC,我发现了我的问题:
    显然,如果不使用 arg 上的 @Param 注释,您就无法在接口中对方法参数进行对象引用。所以,这是我更新的代码来解决这个问题:

    public interface ApiKeyRepository extends CrudRepository<ApiKey, Long> {
        @Override
        @PreAuthorize("hasPermission(#apiKey, 'DELETE')")
        void delete(@Param("apiKey") ApiKey apiKey);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 2020-08-04
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多