我们目前在环境中有不同的角色,例如在开发中我们有名为USR和ADM的角色,而在生产中它们使用全名,例如USER、ADMIN和ADMINISTRATOR。
我解决这个问题的想法是使用一个属性文件和一个占位符作为滚动名,例如,这是我的属性文件:
role.user='USER'
role.admin='ADMIN', 'ADMINISTRATOR'在我的AppConfig中,我在类的顶部添加了以下注释:
@PropertySource("classpath:roles.properties")
public class AppConfig {
}在我的服务中,我现在使用:
@PreAuthorize("hasAnyRole(${role.admin})")
public Item deleteItem(int id) {
}然而,这导致了以下例外情况:
Caused by: org.springframework.expression.spel.SpelParseException: EL1043E:(pos 12): Unexpected token. Expected 'rparen())' but was 'lcurly({)'因为它说它不删除大括号,所以我还尝试了以下方法:@PreAuthorize("hasAnyRole(role.admin)"),结果是:
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 11): Property or field 'role' cannot be found on object of type 'org.springframework.security.access.expression.method.MethodSecurityExpressionRoot' - maybe not public?至少表达式本身现在看起来是有效的,但它似乎不是查看属性文件no,而是查看特定类的属性。
有人想办法解决这个问题吗?或者,是否有另一个/更好的解决方案来解决特定于环境的角色?
发布于 2014-11-07 07:48:39
@PreAuthorize中使用的Spring只具有:
见这个答案https://stackoverflow.com/a/3786581/883859
您可以通过@访问其他bean
public interface RoleNameGetter {
String getSuperUserRole();
...
}和
@Configuration
@PropertySource("classpath:xyz.properties")
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
public class Configuration {
@Autowired
protected Environment env;
@Bean
RoleNameGetter roleNameGetter() {
return new RoleNameGetter() {
@Override
public String getSuperUserRole() {
return env.getProperty("superuser_role_name");
}
};
}允许访问@PreAuthorize中使用的Spring中的属性文件中的角色名,如下所示:
@PreAuthorize("hasAnyAuthority(@roleNameGetter.getSuperUserRole(),@roleNameGetter.getNormalUserRole())")
public void end() {...}https://stackoverflow.com/questions/25500874
复制相似问题