Java 使用自定义bean测试预授权注释
我试图在Spring Boot中测试我的Java 使用自定义bean测试预授权注释,java,spring,spring-boot,junit,spring-security,Java,Spring,Spring Boot,Junit,Spring Security,我试图在Spring Boot中测试我的预授权服务方法注释。我已经设法让它计算SPeL表达式,但是在引用自定义bean时它失败了 我正在测试的服务方法: @PreAuthorize(“hasPermission(#eventId.id(),@eventTypePermission.target,@eventTypePermission.write())) public EventTypeId retrieveEventTypeIdForEventId(EventId EventId){ 返回nu
预授权服务方法注释。我已经设法让它计算SPeL表达式,但是在引用自定义bean时它失败了
我正在测试的服务方法:
@PreAuthorize(“hasPermission(#eventId.id(),@eventTypePermission.target,@eventTypePermission.write()))
public EventTypeId retrieveEventTypeIdForEventId(EventId EventId){
返回null;
}
测试代码:
@配置
@EnableGlobalMethodSecurity(securedEnabled=true,Prespenabled=true)
类TestConfig扩展了GlobalMethodSecurity配置{
@凌驾
受保护的MethodSecurityExpressionHandler createExpressionHandler(){
DefaultMethodSecurityExpressionHandler expressionHandler=
新的DefaultMethodSecurityExpressionHandler();
setPermissionEvaluator(新PermissionEvaluatorProxyTest());
返回表达式处理程序;
}
@豆子
公共事件类型权限(){
返回新的EventTypePermission();
}
}
@组成部分
@Slf4j
类PermissionEvaluatorProxyTest实现PermissionEvaluator{
@自动连线
公共许可EvaluatorProxyTest(){
}
@凌驾
公共许可(
身份验证、对象目标类型、对象权限){
返回true;
}
@凌驾
公共许可(
身份验证、可序列化的targetId、字符串targetType、对象权限){
返回true;
}
}
@导入({TestConfig.class})
@RunWith(SpringRunner.class)
@SpringBootTest(类={EventUpdater.class})
公共类EventUpdateTest{
@自动连线
私有事件更新程序;
@蚕豆
私有事件存储库;
@蚕豆
私有事件类型权限事件类型权限;
@以前
公共作废设置(){
}
@试验
@WithMockUser(username=“test”)
公开无效测试(){
eventUpdater.retrieveEventTypeIdForEventId(新的EventId(UUID.randomUUID());
}
}
错误是:
java.lang.IllegalArgumentException: Failed to evaluate expression 'hasPermission(#eventId.id(), @eventTypePermission.target, @eventTypePermission.write())'
...
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1057E: No bean resolver registered in the context to resolve access to bean 'eventTypePermission'
深入研究,它失败了,因为SPeL表达式使用的bean解析器实例为null。有没有想过为什么会出现这种情况,以及如何设置bean解析器,使其能够返回EventTypePermission
的实例
我还尝试将@SpringBootTest(classes={EventUpdater.class})
更改为@SpringBootTest(classes={EventUpdater.class,EventTypePermission.class})
,但没有成功。通过在配置类上显式设置应用程序上下文使其正常工作:
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
class TestConfig extends GlobalMethodSecurityConfiguration {
private ApplicationContext applicationContext;
@Autowired
public TestConfig(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
DefaultMethodSecurityExpressionHandler expressionHandler =
new DefaultMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(new PermissionEvaluatorProxyTest());
expressionHandler.setApplicationContext(applicationContext);
return expressionHandler;
}
}
还必须更新SpringTestClass
注释以包含EventTypePermission
:
@SpringBootTest(classes={EventUpdater.class,EventTypePermission.class})
,而不是在TestConfig
类中定义Bean。通过显式设置配置类上的应用程序上下文使其工作:
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
class TestConfig extends GlobalMethodSecurityConfiguration {
private ApplicationContext applicationContext;
@Autowired
public TestConfig(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
DefaultMethodSecurityExpressionHandler expressionHandler =
new DefaultMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(new PermissionEvaluatorProxyTest());
expressionHandler.setApplicationContext(applicationContext);
return expressionHandler;
}
}
还必须更新SpringTestClass
注释以包含EventTypePermission
:
@SpringBootTest(classes={EventUpdater.class,EventTypePermission.class})
,而不是在TestConfig
类中定义Bean