Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AbstractSecurityWebApplicationInitializer doesn't start in integration test

I have custom initialzer which extends AbstractSecurityWebApplicationInitializer. It adds couple filters to the beginning of the filter chain.

@Order(2)
public class SecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
    @Override
    protected void beforeSpringSecurityFilterChain(ServletContext servletContext) {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding(StandardCharsets.UTF_8.name());
        characterEncodingFilter.setForceEncoding(true);
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        insertFilters(servletContext, characterEncodingFilter, hiddenHttpMethodFilter);
    }
}

In production it works as expected. But I can't make it start (or better to say: make Spring load it) in integration test environment. Here is the snippet of the test:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {ApplicationConfiguration.class, FakeInMemoryMongoConfiguration.class, WebConfiguration.class})
@ActiveProfiles("test")
public class CarPartControllerIntegrationTest {
    @Autowired
    private WebApplicationContext wac;
    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        mockMvc = webAppContextSetup(wac).build();
    }
    ...
}

I.e., SecurityApplicationInitializer.beforeSpringSecurityFilterChain() is not called during the test. For now I decided to manually add filters in the test's filter chain. But I think there should be a better solution.

Please help make the world a bit better :) Thanks!

like image 377
aka_sh Avatar asked Apr 06 '26 20:04

aka_sh


1 Answers

You need to configure Spring Security for your your tests if you are using MockMvc. You can inject the filter chain and then add it using the builder pattern.

This blog post describes in detail how to set this up and the different testing options with Spring Security. http://spring.io/blog/2014/05/23/preview-spring-security-test-web-security

For your specific example you could do this:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {ApplicationConfiguration.class, FakeInMemoryMongoConfiguration.class, WebConfiguration.class})
@ActiveProfiles("test")
public class CarPartControllerIntegrationTest {
    @Autowired
    private WebApplicationContext wac;

    @Autowired
    private Filter springSecurityFilterChain;

    private MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
          mockMvc = MockMvcBuilders
                  .webAppContextSetup(wac)
                  .addFilters(springSecurityFilterChain)
                  .build();
    }
}
like image 179
cshannon Avatar answered Apr 08 '26 08:04

cshannon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!