spring-projects / spring-security

Spring Security
http://spring.io/projects/spring-security
Apache License 2.0
8.77k stars 5.89k forks source link

Moving from Spring Boot 1.5.9 to 1.5.10 breaking tests #4995

Closed edeandrea closed 6 years ago

edeandrea commented 6 years ago

I've been working on a project where I just moved from Spring Boot 1.5.9 to 1.5.10 (which also upgraded spring-webmvc from 4.3.13 to 4.3.14 and spring-security from 4.2.3 to 4.2.4 and am getting

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mvcHandlerMappingIntrospector' available: 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.

Unlike the examples described in https://jira.spring.io/browse/SPR-16301 I don't have any initializers or anything like that, nor do I have any classes that use @EnableWebMvc or that implement WebMvcConfigurer.

I'm actually seeing this issue in some unit tests where I am testing my security configuration. I am trying to test a filter thats part of the springSecurityFilterChain. I am building a library that is used by applications. Upgrading our reference application from 1.5.9 to 1.5.10 seems to be fine, but our unit tests for our library are now failing.

My test class simply uses

@RunWith(SpringRunner.class)
@ActiveProfiles("header-user-filter-tests")
@WebAppConfiguration
@SpringBootTest(classes = { MyTestClass.Config.class })

at the class level and then a nested inner Config class:

@Configuration
@ImportAutoConfiguration({ SomeCustomAutoConfigurationClass.class, SomeCustomSecurityAutoConfigurationClass.class, org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class })
@Profile("header-user-filter-tests")
public static class Config {
    @Bean
    public UserDetailsService userDetailsService() {
        UserDetailsService userDetailsService = Mockito.mock(UserDetailsService.class);

        // @formatter:off
        BDDMockito
            .given(userDetailsService.loadUserByUsername(BDDMockito.anyString()))
            .willReturn(User.withUsername("user").password("n/a").authorities(new GrantedAuthority[0]).build());
        // @formatter:off

        return userDetailsService;
    }
}

The SomeCustomAutoConfigurationClass just does a component scan of some of our library classes and also has an @ConfigurationProperties annotation.

The SomeCustomSecurityAutoConfigurationClass class has the @EnableWebSecurity annotation as well as it extends WebSecurityConfigurerAdapter.

I've tried a number of different things, including manually defining the mvcHandlerMappingIntrospector bean in my configuration but nothing seems to work.

Any help would be appreciated!

rwinch commented 6 years ago

Thanks for the report. Can you please describe how your tests are now failing? It would also be valuable to see your test that is failing. Are you using MockMvc by chance? If so it might be related to https://jira.spring.io/browse/SPR-16097

edeandrea commented 6 years ago

I put together an entire sample project which demonstrates this issue:

https://github.com/edeandrea/spring-boot-upgrade-issue

The README.md file thats there details the tests and how to switch back to Spring Boot 1.5.9 fixes the tests.

Note that the code & configuration there has been "dumbed down" to show a simplistic example. In reality our actual code/classes are far more complicated & the security configuration is far more complicated. I tried to obfuscate my company's project with as little as possible needed to demonstrate this particular issue.

rwinch commented 6 years ago

Thanks for the sample project. The reason is because you are using Spring Security's MVC matcher which now requires the HandlerMappingIntrospector Bean that Spring MVC exposes. Since your test did not import Spring MVC configuration, it was failing. It is unfortuante that this broke passivity, but it was done to help ensure that a misconfiguration does not occur (which your test had).

You can find a Pull Request to fix the issue at https://github.com/edeandrea/spring-boot-upgrade-issue/pull/1

rwinch commented 6 years ago

I'm closing this as invalid. Please reopen if you still believe there to be an issue.

edeandrea commented 6 years ago

Thank you @rwinch I will take a look on Monday when I get back to work. I appreciate the fast response! Have a great weekend!

edeandrea commented 6 years ago

I verified this fix works in my project. Thanks again for the help!

rwinch commented 6 years ago

Glad to hear it! I'm glad to hear that this solved your issue. Thanks for the fast follow up :)