【问题标题】:How to resolve properties from property file in PreAuthorize?如何从 PreAuthorize 中的属性文件中解析属性?
【发布时间】:2013-09-20 05:28:53
【问题描述】:

在我的test.properties 文件中,我有一个名为devmode 的密钥。 在@PreAuthorize 内执行${devmode} 失败。

@PreAuthorize("${devmode}")

如何在 PreAuthorize 中获取我的属性值?


我正在加载这样的属性文件:
<context:property-placeholder location="/WEB-INF/test.properties" />

像这样使用<security:http use-expressions="true"> 中的值:<security:intercept-url pattern="/api/dev/**" access="${devmode}" />
这也有效:
@Value(${devmode}) String myVar;
所以我真的不明白为什么它不应该工作。

我要做什么:
我以前有这个:

@PreAuthorize("#key == 'mysecretkey'")

它奏效了。但是,我不想在代码中拥有该密钥,而是在我的 .properties 文件中。

【问题讨论】:

标签: java spring spring-security spring-el


【解决方案1】:

假设 devmode 变量解析为角色名称,试试这个:

@PreAuthorize("hasRole('${devmode}')")

【讨论】:

  • 尝试将("hasRole('ROLE_ADMIN')") 更改为"hasRole('${test.key}')",并将 ROLE_ADMIN 作为 test.key 属性。使用 test.key 时不起作用,否则起作用。没有错误信息
  • 试试@PreAuthorize("hasRole('${'test.key'}')")。 Spring 表达式语言支持在表达式中嵌套单引号。
  • @Ritesh 在 pos12 上与 Unexpected token. Expected 'rparen())' but was 'identifier' 一起崩溃。
  • 好的。根据Literal expressions要将单引号本身放在字符串中,请使用两个单引号字符。也许可以尝试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
  • 2013-11-15
  • 1970-01-01
  • 2016-04-16
  • 2016-04-02
  • 2015-06-28
  • 1970-01-01
相关资源
最近更新 更多