Spring SpEL并将环境属性传递给SpEL方法
我试图使用Spring的表达式语言(SpEL)将Spring SpEL并将环境属性传递给SpEL方法,spring,spring-security,spring-el,Spring,Spring Security,Spring El,我试图使用Spring的表达式语言(SpEL)将应用程序.yml文件中的属性传递到SpEL方法调用中 我在一个Spring引导和Spring安全环境中,我正试图在@PreAuthorize注释中这样做。我能够调用方法hasAuthority(),而不会出现如下问题: @PreAuthorize("hasAuthority('APP_USER')") 这个很好用。它验证用户是否拥有APP\u user授权令牌。但是,我想将此值作为属性外部化到配置中。这不起作用: @PreAuthorize("h
应用程序.yml
文件中的属性传递到SpEL方法调用中
我在一个Spring引导和Spring安全环境中,我正试图在@PreAuthorize
注释中这样做。我能够调用方法hasAuthority()
,而不会出现如下问题:
@PreAuthorize("hasAuthority('APP_USER')")
这个很好用。它验证用户是否拥有APP\u user
授权令牌。但是,我想将此值作为属性外部化到配置中。这不起作用:
@PreAuthorize("hasAuthority(#systemProperties.get('app.auth.readToken'))")
我也试过了
@PreAuthorize("hasAuthority(#environment( app.auth.readToken ))")
及
那么,如何使用SpEL将应用程序属性作为SpEL方法参数传递?这是可能的吗?您可以使用
@PropertyResolver
访问注释中的PropertyResolver
@PreAuthorize("hasRole(@propertyResolver.getProperty('app.auth.readToaken'))")
编辑: 如果这不起作用,您可以在配置中提供properties
@Bean
来加载属性。然后只访问该bean,而不是propertyResolver
。下面是加载yml
文件的示例
@Bean
public Properties properties() {
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("application.yml")); //the yml file
return yaml.getObject();
}
并在注释中使用
@PreAuthorize("hasRole(@properties.getProperty('app.auth.readToaken'))")
您可以使用
@PropertyResolver
访问注释中的PropertyResolver
@PreAuthorize("hasRole(@propertyResolver.getProperty('app.auth.readToaken'))")
编辑: 如果这不起作用,您可以在配置中提供properties
@Bean
来加载属性。然后只访问该bean,而不是propertyResolver
。下面是加载yml
文件的示例
@Bean
public Properties properties() {
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("application.yml")); //the yml file
return yaml.getObject();
}
并在注释中使用
@PreAuthorize("hasRole(@properties.getProperty('app.auth.readToaken'))")
嗯,我得到了一个
NoSuchBeanDefinitionException:没有名为'propertyResolver'的bean可用
,这很奇怪,因为我能够使用@Value
并在类的其他地方映射属性。Spring 4+中的bean命名是否不同?@heez我提供了另一个选项,将属性加载到@bean
。是的:)第二种方法是我所做的变通方法。我最终使用@Value
加载属性,然后使用bean方法访问该字段。嗯,我得到一个NoSuchBeanDefinitionException:没有名为'propertyResolver'的bean可用
,这很奇怪,因为我能够使用@Value
并映射类中其他地方的属性。Spring 4+中的bean命名是否不同?@heez我提供了另一个选项,将属性加载到@bean
。是的:)第二种方法是我所做的变通方法。我最终使用@Value
加载属性,然后使用bean方法访问该字段。