Spring Security Sping Boot 测试失败,没有类型为“org.springframework. Boot .autoconfigure.web.servlet.DispatcherServletPath”的合格Bean

elcex8rz  于 2023-10-20  发布在  Spring
关注(0)|答案(2)|浏览(133)

我有一个Sping Boot 应用程序,它在运行时可以工作,但是当我尝试用MockMVC做一些测试时,我得到了一个错误。该错误由SecurityConfig中的一行触发:

val publicUrls = arrayOf(
    "/telemetry/**",
    "/login",
    "/logout"
)

override fun configure(http: HttpSecurity) {
    http
        .csrf().ignoringAntMatchers(*publicUrls)
        .and()
        .authorizeRequests()
        .requestMatchers(EndpointRequest.to("status", "info")).permitAll()

        // The following line is the problem
        .requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll()   // <-- causes error

        .antMatchers(*publicUrls).permitAll()
        .antMatchers("/system/**").hasRole("SUPER")
        .antMatchers("/admin/**").hasRole("ADMIN")
        .anyRequest().authenticated()
    //.and()
    //.oauth2Login();
}

如果没有允许访问静态资源的行,则测试可以正常工作。有了这一行,我得到了下面的错误(带有堆栈跟踪)。如果我添加一个实现DispatcherServletPath的类,并使用@Component注解,那么我会得到一个错误expected single matching bean but found 2:
测试代码为:

@SpringBootTest
@AutoConfigureMockMvc
@WebAppConfiguration
@ComponentScan("com.controlj.monitor.service")
@RunWith(SpringJUnit4ClassRunner::class)
class SampleTest {

    @MockBean
    private lateinit var mockTelemetryService: TelemetryService
    @Autowired
    private lateinit var springSecurityFilterChain: FilterChainProxy

    private lateinit var mvc: MockMvc

    @Before
    fun setup() {
        mvc = MockMvcBuilders.standaloneSetup(TelemetryEndpoint(mockTelemetryService))
            .apply<StandaloneMockMvcBuilder>(SecurityMockMvcConfigurers.springSecurity(springSecurityFilterChain))
            .build()
    }

   @Test
    fun testTelemetryApi() {
        val postMapping = TelemetryEndpoint::post.findAnnotation<PostMapping>()?.value?.first()
        mvc.perform(post(postMapping, "badKey", 123456)
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().`is`(404))
   }
}

我该怎么办?

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' available

at org.springframework.beans.factory.support.StaticListableBeanFactory.getBean(StaticListableBeanFactory.java:172)
at org.springframework.test.web.servlet.setup.StubWebApplicationContext.getBean(StubWebApplicationContext.java:176)
at org.springframework.boot.security.servlet.ApplicationContextRequestMatcher.lambda$createContext$1(ApplicationContextRequestMatcher.java:95)
at org.springframework.boot.autoconfigure.security.servlet.StaticResourceRequest$StaticResourceRequestMatcher.initialized(StaticResourceRequest.java:140)
at org.springframework.boot.security.servlet.ApplicationContextRequestMatcher.getContext(ApplicationContextRequestMatcher.java:73)
at org.springframework.boot.security.servlet.ApplicationContextRequestMatcher.matches(ApplicationContextRequestMatcher.java:57)
at org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource.getAttributes(DefaultFilterInvocationSecurityMetadataSource.java:95)
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:197)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:124)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:133)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:182)
kknvjkwl

kknvjkwl1#

尝试给予您自己的DispatcherServletPath实现一个名称

@Component("dispatcherServlet")
public class Your_Dispatch_Servlet implements DispatcherServletPath {
}
r1zk6ea1

r1zk6ea12#

我为这件事纠结了很久。对我来说,一些测试的通过或失败取决于我如何运行它们,或者我是一起运行它们还是单独运行它们--这显然不是理想的。但似乎是我的安全配置中的同一行导致了我的问题。
我敢肯定,这不是最终的解决办法,但作为一个变通办法,现在我已经取代

PathRequest.toStaticResources().atCommonLocations()

new AntPathRequestMatcher("/webjars/**", "GET")

我只使用来自WebJars的静态资源,所以这足以让我暂时解决这个问题。我想问题其实出在别处我很想知道到底发生了什么。

相关问题