@SpringBootTest(webEnvironment = NONE) + @EnableWebSecurity causes exception
See original GitHub issueAfter upgrade Spring Boot from 1.5.8.RELEASE to 1.5.10.RELEASE some of my previously working tests started to fail with very strange message.
***************************
APPLICATION FAILED TO START
***************************
Description:
Method springSecurityFilterChain in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration required a bean named 'A Bean named mvcHandlerMappingIntrospector of type org.springframework.web.servlet.handler.HandlerMappingIntrospector is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.' that could not be found.
Action:
Consider defining a bean named 'A Bean named mvcHandlerMappingIntrospector of type org.springframework.web.servlet.handler.HandlerMappingIntrospector is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.' in your configuration.
Consider defining a bean named ‘A Bean named mvcHandlerMappingIntrospector of type org.springframework.web.servlet.handler.HandlerMappingIntrospector is required to use MvcRequestMatcher. Please ensure Spring Security & Spring MVC are configured in a shared ApplicationContext.’
Very cool name for the bean I should say! 😃
Apart to the problem that description somehow became part of bean name, I can understand that probably the root problem is that with @SpringBootTest(webEnvironment = NONE)
@EnableWebSecurity
somehow can’t be fully initialized.
But why it stopped to work in 1.5.10 (it worked in 1.5.8 and 1.5.9).
So, is it regression or I need to explicitly exclude all web-related beans from the test configuration from now? 😞
MVCE: https://github.com/xak2000/mvc-handler-interceptor-bug
Maybe #10943 is related. I think this commit causes the error (if it was backported to 4.x). But I’m not sure what to think about it.
Maybe we should register mvcHandlerMappingIntrospector
in test context even when webEnvironment = NONE
to skip this check, maybe it is bad… Or maybe the configuration bean, which extends WebSecurityConfigurerAdapter
should be excluded from test context automatically. Or maybe it should be fixed somehow on spring-security side… But inability to throw away all WEB-related stuff in just one line (webEnvironment = NONE
) is not very convinient. Especially if this worked before.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (9 by maintainers)
OK, so here’s what I think is happening. Running with
@SpringBootTest(webEnvironment = NONE)
means that theWebMvcAutoConfiguration
doesn’t kick in. This meansWebMvcConfigurationSupport
isn’t registered and nomvcHandlerMappingIntrospector
bean is created.The update to Spring Security means that you’re now getting a hard failure. I presume that previously the matcher would have failed if it were invoked, but obviously it never would be in such a test.
Since this is your own
@Configuration
class, I don’t think there’s much that we can do in Spring Boot to stop this error. The easiest quick fix is to guard yourSecurityConfig
with@ConditionalOnWebApplication
.@rwinch Is there any way to make those introspection lookups more lenient?
OK, I’m sorry to say I don’t think there’s much we can do directly to help with this. Adding
@ConditionalOnWebApplication
in your own code seems to be the only option.