当前位置: 首页 > 知识库问答 >
问题:

使用MockMVC和@ComponentScan未找到springSecurityFilterChain bean

陈野
2023-03-14

我正在使用spring MockMVC测试spring rest控制器。我能够执行模拟http请求并验证响应。我的设置自动连接WebApplicationContext:

@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 Security测试指南Spring security and test mockmvc的集成部分,我似乎只需要调用org。springframework。安全测验网状物servlet。设置。MockMvcBuilders中的springSecurity()。apply()函数调用。

这不起作用,我得到:

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,它确实是空的。

我怀疑我需要配置我的组件扫描以包含Spring的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吗?


共有1个答案

令狐宏浚
2023-03-14

我假设您必须在某个地方有一个扩展WebSecurity配置适配器的配置类?这是设置Spring Security性所需的配置。

您需要在测试配置的组件扫描中包含此类。

 类似资料:
  • 我试图运行一个简单的Spring Boot测试,我得到的错误表明它不能在运行时MockMvc。文档表明我使用了正确的注释,并且我使用start.spring.io创建了我的pom.xml。不知道为什么会有问题。 错误: 测试代码 pom.xml

  • 你好,我试图学习野蝇和springboose一个非常简单的应用程序使用eclipse。项目名称是springboo-test。包括主方法类在内的所有类都在同一个包中。 主方法类称为'App',其代码如下: 以下是服务器日志: 11:36:57281信息[org.wildfly.extension.undertow](服务器服务线程池--68)WFLYUT0021:注册的web上下文:'/sprin

  • 我试图创建我的第一个Spring后端,简单的应用程序,存储音乐专辑。不幸的是,我创建的endpoint没有一个工作(我使用postman测试了它们,请求返回404,没有找到)。以下是我所有与项目相关的文件/代码: 我所尝试的: 结果: 正在添加应创建数据库的dataSource方法。 @bean@ConfigurationProperties(前缀=“spring.DataSource”)publ

  • 我使用springboot和maryadb数据库进行训练。当我测试数据恢复时,我在邮递员中收到这样一条消息: 。我在复制粘贴中尝试了几个教程,我总是有相同的消息。我也会把控制台中的消息。提前谢谢你的帮助。 控制器 服务 回应的 模型 应用属性 安慰

  • 我有一个脚本叫做在我的主文件夹中。 当我导航到这个文件夹,并输入,我得到 . 当我使用时,我得到 。 为什么会发生这种情况,我如何解决它?

  • 我在为ios编译应用程序时遇到了这个问题 这是整个堆栈跟踪: 在调试模式下在iPhone Xs Max上启动lib / main.dart...正在运行的吊舱安装...运行 Xcode 构建...Xcode 构建完成。 2,4 秒 无法构建 iOS 应用程序 Xcode 构建的错误输出: ↳ ** 构建失败 ** Xcode的输出: ↳ === BUILD TARGET Firebase Auth