【问题标题】:Expression in annotation using fields of class in Spring Security使用 Spring Security 中的类字段在注释中表达
【发布时间】:2014-11-25 15:52:37
【问题描述】:

我使用方法级别的安全性。在课堂上我注释了一些方法,表达式使用了这个类的字段。但我看到 SpEL 异常,我无法引用它们。 这是该类的部分代码。在表达式中,我想使用字段 repPrefix,但我收到它是未知变量的异常。

@Component("c2rTableManager")
@Scope("prototype")
public class C2RTableManager implements TableManager {
     private final TableManager tableManager;
     private final String repPrefix;

     @Autowired
     private SecurityInfoService securityInfoService;

     public C2RTableManager(TableManager tableManager, String repository) {
          this.tableManager = tableManager;
          this.repPrefix = repository + "__";
     }

     ...some methods

     @Override
     @PreAuthorize("hasRole('DBA') || hasPermission(repPrefix + #table, 'TABLE', 'DELETE_TABLE')")
     public void dropTable(String table) throws InterruptedException, IOException {
          tableManager.dropTable(table);
     }

     ...other methods
}

如果我用另一种方式写,表达式根本不会被评估。不明白为什么。

@Component("c2rTableManager")
@Scope("prototype")
public class C2RTableManager implements TableManager {
     private final TableManager tableManager;
     private final String repPrefix;

     @Autowired
     private SecurityInfoService securityInfoService;

     public C2RTableManager(TableManager tableManager, String repository) {
          this.tableManager = tableManager;
          this.repPrefix = repository + "__";
     }

     ...some methods

     @Override

     public void dropTable(String table) throws InterruptedException, IOException {
          dropTable(table, repPrefix);
     }

     @PreAuthorize("hasRole('DBA') || hasPermission(#repPrefix + #table, 'TABLE', 'DELETE_TABLE')")
     public void dropTable(String table, String repPrefix) throws InterruptedException, IOException {
          tableManager.dropTable(table);
     }

     ...other methods
}

如何使用该类的字段值为类的方法编写表达式?

【问题讨论】:

  • 究竟是什么:SPEL 异常?
  • 诸如未知变量之类的东西,无法评估等等。
  • 如果您使用接口 AOP 代理,来自同一实例的方法调用将不会评估任何注释。
  • 我对AOP一无所知。但是我可以从同一个实例进行方法调用评估注释吗?如果我克隆实例并调用所需的方法,可以吗? this.clone().dropTable(table, repPrefix);

标签: java spring spring-security spring-el


【解决方案1】:

我没有足够的声誉来添加评论。 来自 http://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html

的 Spring Security 文档

这里我们实际上使用了一个方法参数作为表达式的一部分 判断当前用户是否有“admin”权限 给定的联系。内置的 hasPermission() 表达式链接到 Spring Security ACL 模块通过应用程序上下文,如 我们将在下面看到。您可以按名称访问任何方法参数 表达式变量,前提是您的代码具有调试信息 编译进去。

请强调最后一句话。检查以下两点:

  • 您是否编译了带有调试标志的类?
  • 您是否使用此声明启用了方法级别的安全性:<global-method-security pre-post-annotations="enabled"/>

【讨论】:

  • 我不知道如何使用调试进行编译,因为我使用 Maven 构建应用程序并在 NetBeans 中调试它们。当然启用了这个声明,其他表达式正常工作。但是为什么有些表达式被评估(第一个例子),而有些不是(第二个例子)?
【解决方案2】:

我需要将 bild 声明为 public

private final String repPrefix;

并写带有指向此链接的注释

@PreAuthorize("hasRole('DBA') || hasPermission(repPrefix + #table, 'TABLE', 'DELETE_TABLE')")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-19
    • 2011-07-16
    • 2014-08-22
    • 2015-05-30
    • 2017-06-19
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多