Java 使用MockMVC和@ComponentScan未找到springSecurityFilterChain bean
我正在使用SpringMockMVC测试SpringREST控制器。 我能够执行模拟http请求并验证响应。 我的设置自动连接WebApplicationContext:Java 使用MockMVC和@ComponentScan未找到springSecurityFilterChain bean,java,spring,unit-testing,spring-mvc,spring-security,Java,Spring,Unit Testing,Spring Mvc,Spring Security,我正在使用SpringMockMVC测试SpringREST控制器。 我能够执行模拟http请求并验证响应。 我的设置自动连接WebApplicationContext: @RunWith(SpringRunner.class) @WebAppConfiguration @ContextConfiguration public class MyTestClass { @Autowired private WebApplicationContext webAppContext; private
@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextConfiguration
public class MyTestClass {
@Autowired
private WebApplicationContext webAppContext;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webAppContext)
.apply(springSecurity()).build();
}
要告诉MockMVC要初始化并添加到上下文中的控制器,我有一个内部配置类:
@Configuration
@ComponentScan(
basePackages = {"com.acme"},
useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value={MyRestController.class, MyRestControllerDependency.class})
})
@EnableWebMvc
static class MyTestClassConfig implements WebMvcConfigurer {
@Bean
public MyClassRepository myClassRepository() {
return Mockito.mock(MyClassRepository.class);
}
这就像一个符咒。MockMVC启动了一个容器,其中包含加载MyRestController的上下文,并提供由MyRestController映射的URL。
现在,一些MyRestControllers方法被注释为:@PreAuthorize(“hasAuthority('ADMINISTRATOR')”)
,因此下一步就是测试它
阅读Spring安全测试指南的集成部分
似乎我所要做的就是调用MockMvcBuilders.apply()
函数调用中的org.springframework.security.test.web.servlet.setup.springSecurity()
这不起作用,我得到:
java.lang.IllegalStateException: springSecurityFilterChain cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.
我尝试向我的内部配置类添加bean定义,如下所示:
Filter springSecurityFilterChain() {
return new FilterChainProxy();
}
虽然这确实提供了springSecurityFilterChain,但实例是空的,当我运行测试时,我会得到一个NPE:
java.lang.NullPointerException
at org.springframework.security.web.FilterChainProxy.getFilters(FilterChainProxy.java:224)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
可以理解,通过直接实例化FilterChainProxy,它确实是空的
我怀疑我需要将我的组件扫描配置为包括springs的FilterChainProxy实例。如果这就是问题所在,我不知道应该包括哪些类。我尝试过包括所有课程:
includeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value={TenantAPI.class, TenantManager.class}),
@ComponentScan.Filter(type = FilterType.REGEX, pattern="^org\\.springframework\\.security.*")})
但是这会产生相同的错误:“确保一个名为springSecurity..的Bean”,就像我在没有额外的正则表达式过滤器的情况下运行测试一样
当我对让MockMVC实例化的内部类很挑剔时,有人知道如何获得springSecurityFilterChain吗 我假设您必须在某个地方有一个
@Configuration
类,该类扩展了websecurityconfigureadapter
?这是设置Spring安全性所需的配置
您需要在测试配置中的组件扫描中包含此类。是的。我昨天确实这么做了,但我躲开了,因为它能自动连接一堆东西。Spring抱怨它无法创建名为“somename”的bean没有资格。。。我今天会更努力的,谢谢你!