【问题标题】:How concatenate two string in Spring Expression Language (SpEL)如何在 Spring 表达式语言 (SpEL) 中连接两个字符串
【发布时间】:2014-07-30 01:15:21
【问题描述】:

在我的 Spring 应用程序中,出于安全目的,我的控制器和服务类中的方法具有此注释:

@PreAuthorize("hasPermission(#user, 'cadastra')")

第二个参数,权限,应该是这样的格式:

<<action_name>>_<<class_name>>

考虑到类名由this.getClass().getName() 持有,我应该使用什么表达式来完成此操作?

【问题讨论】:

  • 你的意思是这不起作用:@PreAuthorize("hasPermission(#user, 'cadastra_' + this.class.name)")?
  • 我就是这样尝试的,但不起作用。在此之前,我也尝试使用:@PreAuthorize("hasPermission(#user, 'cadastra_#this.class.name')")unsuccessfully。
  • 这两个建议的组合也许是:@PreAuthorize("hasPermission(#user,'cadastra_'+#this.class.name)")
  • 是的,我也尝试过,但在所有情况下都被拒绝访问。有什么方法可以验证这个字符串的最终值是接收的吗?

标签: spring spring-mvc spring-security spring-el


【解决方案1】:

我终于解决了这个问题。我在控制器中添加了一个新方法:

public String getName() {
    String nome_classe = entityClass.getSimpleName();
    System.out.println("getName nome_class = "+nome_classe);
    return nome_classe;
}

现在我以这种方式使用注释:

@PreAuthorize("hasPermission(#user, 'cadastra_'+#this.this.name)")

【讨论】:

    【解决方案2】:

    要在 Spring EL 中连接两个字符串,请使用 concat function 。请参阅此处了解更多详细信息: Spring EL docs

    例如,我使用了以下内容:

        @PreAuthorize("hasRole('ROLE_'.concat(this.class.simpleName))")
    

    【讨论】:

      猜你喜欢
      • 2013-12-10
      • 2014-01-14
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      • 2016-06-27
      相关资源
      最近更新 更多