title: Why does my ApplicationContext fail to load when I enable web security?
body: <p>I have a Spring Boot application that I would like to add Spring Security too. So I've added the following configuration, which starts and runs just fine:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
      @Resource
      private Environment environment;
  
      @Bean
      public PasswordEncoder passwordEncoder(){
          return new BCryptPasswordEncoder();
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          if(environment.acceptsProfiles("live")) {
              http.requiresChannel().anyRequest().requiresSecure();
          }
          http.csrf().disable();
      }
  }
  </code></pre>
  
  <p>The problem is that my functional tests fail to load the application context. All functional tests extend from the following configuration:</p>
  
  <pre><code>@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = MyConfiguration.class)
  @WebAppConfiguration
  @IntegrationTest
  class ContextInitializer extends GebReportingSpec {
  }
  </code></pre>
  
  <p>When the tests attempts to load the application, it throws the following stack trace:</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultServletHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
      at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:117)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:689)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
      at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:103)
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
      at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
      at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
      at org.spockframework.spring.SpringTestContextManager.prepareTestInstance(SpringTestContextManager.java:49)
      at org.spockframework.spring.SpringInterceptor.interceptSetupMethod(SpringInterceptor.java:42)
      at org.spockframework.runtime.extension.AbstractMethodInterceptor.intercept(AbstractMethodInterceptor.java:28)
      at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87)
      at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:471)
      at org.spockframework.runtime.BaseSpecRunner.runSetup(BaseSpecRunner.java:375)
      at org.spockframework.runtime.BaseSpecRunner.runSetup(BaseSpecRunner.java:370)
      at org.spockframework.runtime.BaseSpecRunner.doRunIteration(BaseSpecRunner.java:323)
      at org.spockframework.runtime.BaseSpecRunner$6.invoke(BaseSpecRunner.java:309)
      at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:88)
      at org.spockframework.runtime.extension.builtin.AbstractRuleInterceptor$1.evaluate(AbstractRuleInterceptor.java:37)
      at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
      at org.spockframework.runtime.extension.builtin.TestRuleInterceptor.intercept(TestRuleInterceptor.java:38)
      at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87)
      at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:471)
      at org.spockframework.runtime.BaseSpecRunner.runIteration(BaseSpecRunner.java:288)
      at org.spockframework.runtime.BaseSpecRunner.initializeAndRunIteration(BaseSpecRunner.java:278)
      at org.spockframework.runtime.BaseSpecRunner.runSimpleFeature(BaseSpecRunner.java:269)
      at org.spockframework.runtime.BaseSpecRunner.doRunFeature(BaseSpecRunner.java:263)
      at org.spockframework.runtime.BaseSpecRunner$5.invoke(BaseSpecRunner.java:246)
      at org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:480)
      at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:463)
      at org.spockframework.runtime.BaseSpecRunner.runFeature(BaseSpecRunner.java:238)
      at org.spockframework.runtime.BaseSpecRunner.runFeatures(BaseSpecRunner.java:188)
      at org.spockframework.runtime.BaseSpecRunner.doRunSpec(BaseSpecRunner.java:98)
      at org.spockframework.runtime.BaseSpecRunner$1.invoke(BaseSpecRunner.java:84)
      at org.spockframework.runtime.BaseSpecRunner.invokeRaw(BaseSpecRunner.java:480)
      at org.spockframework.runtime.BaseSpecRunner.invoke(BaseSpecRunner.java:463)
      at org.spockframework.runtime.BaseSpecRunner.runSpec(BaseSpecRunner.java:76)
      at org.spockframework.runtime.BaseSpecRunner.run(BaseSpecRunner.java:67)
      at org.spockframework.runtime.Sputnik.run(Sputnik.java:63)
      at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:105)
      at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:56)
      at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:64)
      at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
      at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
      at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
      at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
      at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
      at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:106)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
      at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
      at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
      at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
      at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'defaultServletHandlerMapping' threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
      ... 78 common frames omitted
  Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
      at org.springframework.util.Assert.notNull(Assert.java:115)
      at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.&lt;init&gt;(DefaultServletHandlerConfigurer.java:53)
      at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:426)
      at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$4bbac173.CGLIB$defaultServletHandlerMapping$25(&lt;generated&gt;)
      at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$4bbac173$$FastClassBySpringCGLIB$$59db630c.invoke(&lt;generated&gt;)
      at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
      at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:318)
      at org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$4bbac173.defaultServletHandlerMapping(&lt;generated&gt;)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
      ... 79 common frames omitted
  </code></pre>
  
  <p>Seems like I need to let the Integration test context loader know about the spring security somehow. </p>
  
  <p>EDIT:</p>
  
  <p>I've narrowed the problem down to a converter that pulls in a PasswordEncoder. Removing the converter and using the PasswordEncoder directly in the controller causes the problem to go away. Also, removing the Security Configuration will allow me to use the Converter just fine. </p>
  
  <p>Here is a link to a repository I made that replicates the issue:
  <a href="https://github.com/StephenTurley/SpringBootSecurityTest.git" rel="nofollow">https://github.com/StephenTurley/SpringBootSecurityTest.git</a></p>
  
  <p>The last commit causes the error. Not sure why I can't use spring converters and Spring security. Any ideas? </p>