【发布时间】:2016-10-28 01:18:20
【问题描述】:
简单的问题,我的控制器中有一个@Service 类@Autowired。
尝试在我的控制器中的其中一种方法上设置一点安全性。所以为了简单起见,我这样做是为了测试
@PreAuthorize("@myService.helloThere()")
public void someControllerMethod() {
...
}
但真的没有成功。在方法调用期间出现异常。
java.lang.IllegalArgumentException:无法评估表达式 '@myService.helloThere()'
我在这里缺少 EL 的东西吗?
更新
只添加最后一个由异常引起的
原因: org.springframework.expression.spel.SpelEvaluationException: EL1057E:(pos 1): 没有在上下文中注册的 bean 解析器来解析 访问 bean 'dummyServiceImpl'
现在我不明白为什么我使用 @Autowired 时无法在 StandardEvaluationContext 中访问它?
更新 2
由于我在自定义 GlobalMethodSecurityConfiguration 扩展类中连接了自己的角色层次结构,因此默认情况下 DefaultMethodSecurityExpressionHandler 没有设置 applicationContext。我不确定为什么这是设计使然,或者我遗漏了一些明显的东西。我搜索了参考页面并找到了另一个帮助我解决问题的SO thread。我正在发布更新的安全配置。
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class GlobalMethodSecurityConfig extends GlobalMethodSecurityConfiguration {
@Autowired
ApplicationContext applicationContext; //added this
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
final DefaultMethodSecurityExpressionHandler handler = new DefaultMethodSecurityExpressionHandler();
handler.setApplicationContext(applicationContext); //added this
RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER_MANAGER > ROLE_USER");
handler.setRoleHierarchy(roleHierarchy);
return handler;
}
}
【问题讨论】:
-
@myService.helloThere()要写什么逻辑?您为什么不希望用户只具有某些特定角色? -
你少了一颗豆子。
-
@XtremeBiker 例如确保经过身份验证的用户不能添加更高权限的用户。有角色可以解决等式的一半,但我想我想提取一些极端情况。
-
@RomanC 你是什么意思?我的班级是自动接线的吗?
-
@HajderRabiee 如果它是自动接线的,那么你可以。
标签: spring spring-security spring-boot spelevaluationexception