我有一个运行时可以运行的Spring 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)
答案 0 :(得分:0)
尝试为您自己的DispatcherServletPath
实现指定名称
@Component("dispatcherServlet")
public class Your_Dispatch_Servlet implements DispatcherServletPath {
}
答案 1 :(得分:0)
多年来,我一直在为此扯头发。对我来说,某些测试通过或失败取决于我如何运行它们,或者是同时运行还是单独运行-这显然不是理想的选择。但是似乎我的安全配置中的同一行导致了我的问题。
我确定这不是最终解决办法,但是作为一种解决方法,我现在已经替换了
# Check for Windows
Vagrant::Util::Platform.windows?
# Check for MacOS
Vagrant::Util::Platform.darwin?
使用
PathRequest.toStaticResources().atCommonLocations()
我只使用WebJars中的静态资源,所以这足以使我暂时通过。我想这个问题实际上在其他地方。我很想知道真正发生了什么。