java - Why does my ApplicationContext fail to load when I enable web security? -
i have spring boot application add spring security too. i've added following configuration, starts , runs fine:
@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(); } }
the problem functional tests fail load application context. functional tests extend following configuration:
@contextconfiguration(loader = springapplicationcontextloader.class, classes = myconfiguration.class) @webappconfiguration @integrationtest class contextinitializer extends gebreportingspec { }
when tests attempts load application, throws following stack trace:
org.springframework.beans.factory.beancreationexception: error creating bean name 'defaultservlethandlermapping' defined in class path resource [org/springframework/boot/autoconfigure/web/webmvcautoconfiguration$enablewebmvcconfiguration.class]: bean instantiation via factory method failed; nested exception org.springframework.beans.beaninstantiationexception: failed instantiate [org.springframework.web.servlet.handlermapping]: factory method 'defaultservlethandlermapping' threw exception; nested exception java.lang.illegalargumentexception: servletcontext required configure default servlet handling @ org.springframework.beans.factory.support.constructorresolver.instantiateusingfactorymethod(constructorresolver.java:599) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.instantiateusingfactorymethod(abstractautowirecapablebeanfactory.java:1123) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbeaninstance(abstractautowirecapablebeanfactory.java:1018) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:510) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:305) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:301) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:196) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:772) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:835) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:537) @ org.springframework.boot.context.embedded.embeddedwebapplicationcontext.refresh(embeddedwebapplicationcontext.java:117) @ org.springframework.boot.springapplication.refresh(springapplication.java:689) @ org.springframework.boot.springapplication.run(springapplication.java:321) @ org.springframework.boot.test.springapplicationcontextloader.loadcontext(springapplicationcontextloader.java:103) @ org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontextinternal(defaultcacheawarecontextloaderdelegate.java:98) @ org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:116) @ org.springframework.test.context.support.defaulttestcontext.getapplicationcontext(defaulttestcontext.java:83) @ org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.injectdependencies(dependencyinjectiontestexecutionlistener.java:117) @ org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.preparetestinstance(dependencyinjectiontestexecutionlistener.java:83) @ org.springframework.test.context.testcontextmanager.preparetestinstance(testcontextmanager.java:228) @ org.spockframework.spring.springtestcontextmanager.preparetestinstance(springtestcontextmanager.java:49) @ org.spockframework.spring.springinterceptor.interceptsetupmethod(springinterceptor.java:42) @ org.spockframework.runtime.extension.abstractmethodinterceptor.intercept(abstractmethodinterceptor.java:28) @ org.spockframework.runtime.extension.methodinvocation.proceed(methodinvocation.java:87) @ org.spockframework.runtime.basespecrunner.invoke(basespecrunner.java:471) @ org.spockframework.runtime.basespecrunner.runsetup(basespecrunner.java:375) @ org.spockframework.runtime.basespecrunner.runsetup(basespecrunner.java:370) @ org.spockframework.runtime.basespecrunner.doruniteration(basespecrunner.java:323) @ org.spockframework.runtime.basespecrunner$6.invoke(basespecrunner.java:309) @ org.spockframework.runtime.extension.methodinvocation.proceed(methodinvocation.java:88) @ org.spockframework.runtime.extension.builtin.abstractruleinterceptor$1.evaluate(abstractruleinterceptor.java:37) @ org.junit.rules.testwatcher$1.evaluate(testwatcher.java:55) @ org.spockframework.runtime.extension.builtin.testruleinterceptor.intercept(testruleinterceptor.java:38) @ org.spockframework.runtime.extension.methodinvocation.proceed(methodinvocation.java:87) @ org.spockframework.runtime.basespecrunner.invoke(basespecrunner.java:471) @ org.spockframework.runtime.basespecrunner.runiteration(basespecrunner.java:288) @ org.spockframework.runtime.basespecrunner.initializeandruniteration(basespecrunner.java:278) @ org.spockframework.runtime.basespecrunner.runsimplefeature(basespecrunner.java:269) @ org.spockframework.runtime.basespecrunner.dorunfeature(basespecrunner.java:263) @ org.spockframework.runtime.basespecrunner$5.invoke(basespecrunner.java:246) @ org.spockframework.runtime.basespecrunner.invokeraw(basespecrunner.java:480) @ org.spockframework.runtime.basespecrunner.invoke(basespecrunner.java:463) @ org.spockframework.runtime.basespecrunner.runfeature(basespecrunner.java:238) @ org.spockframework.runtime.basespecrunner.runfeatures(basespecrunner.java:188) @ org.spockframework.runtime.basespecrunner.dorunspec(basespecrunner.java:98) @ org.spockframework.runtime.basespecrunner$1.invoke(basespecrunner.java:84) @ org.spockframework.runtime.basespecrunner.invokeraw(basespecrunner.java:480) @ org.spockframework.runtime.basespecrunner.invoke(basespecrunner.java:463) @ org.spockframework.runtime.basespecrunner.runspec(basespecrunner.java:76) @ org.spockframework.runtime.basespecrunner.run(basespecrunner.java:67) @ org.spockframework.runtime.sputnik.run(sputnik.java:63) @ org.gradle.api.internal.tasks.testing.junit.junittestclassexecuter.runtestclass(junittestclassexecuter.java:105) @ org.gradle.api.internal.tasks.testing.junit.junittestclassexecuter.execute(junittestclassexecuter.java:56) @ org.gradle.api.internal.tasks.testing.junit.junittestclassprocessor.processtestclass(junittestclassprocessor.java:64) @ org.gradle.api.internal.tasks.testing.suitetestclassprocessor.processtestclass(suitetestclassprocessor.java:50) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ org.gradle.messaging.dispatch.reflectiondispatch.dispatch(reflectiondispatch.java:35) @ org.gradle.messaging.dispatch.reflectiondispatch.dispatch(reflectiondispatch.java:24) @ org.gradle.messaging.dispatch.contextclassloaderdispatch.dispatch(contextclassloaderdispatch.java:32) @ org.gradle.messaging.dispatch.proxydispatchadapter$dispatchinginvocationhandler.invoke(proxydispatchadapter.java:93) @ com.sun.proxy.$proxy2.processtestclass(unknown source) @ org.gradle.api.internal.tasks.testing.worker.testworker.processtestclass(testworker.java:106) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ org.gradle.messaging.dispatch.reflectiondispatch.dispatch(reflectiondispatch.java:35) @ org.gradle.messaging.dispatch.reflectiondispatch.dispatch(reflectiondispatch.java:24) @ org.gradle.messaging.remote.internal.hub.messagehub$handler.run(messagehub.java:360) @ org.gradle.internal.concurrent.executorpolicy$catchandrecordfailures.onexecute(executorpolicy.java:54) @ org.gradle.internal.concurrent.stoppableexecutorimpl$1.run(stoppableexecutorimpl.java:40) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ java.lang.thread.run(thread.java:745) caused by: org.springframework.beans.beaninstantiationexception: failed instantiate [org.springframework.web.servlet.handlermapping]: factory method 'defaultservlethandlermapping' threw exception; nested exception java.lang.illegalargumentexception: servletcontext required configure default servlet handling @ org.springframework.beans.factory.support.simpleinstantiationstrategy.instantiate(simpleinstantiationstrategy.java:189) @ org.springframework.beans.factory.support.constructorresolver.instantiateusingfactorymethod(constructorresolver.java:588) ... 78 common frames omitted caused by: java.lang.illegalargumentexception: servletcontext required configure default servlet handling @ org.springframework.util.assert.notnull(assert.java:115) @ org.springframework.web.servlet.config.annotation.defaultservlethandlerconfigurer.<init>(defaultservlethandlerconfigurer.java:53) @ org.springframework.web.servlet.config.annotation.webmvcconfigurationsupport.defaultservlethandlermapping(webmvcconfigurationsupport.java:426) @ org.springframework.boot.autoconfigure.web.webmvcautoconfiguration$enablewebmvcconfiguration$$enhancerbyspringcglib$$4bbac173.cglib$defaultservlethandlermapping$25(<generated>) @ org.springframework.boot.autoconfigure.web.webmvcautoconfiguration$enablewebmvcconfiguration$$enhancerbyspringcglib$$4bbac173$$fastclassbyspringcglib$$59db630c.invoke(<generated>) @ org.springframework.cglib.proxy.methodproxy.invokesuper(methodproxy.java:228) @ org.springframework.context.annotation.configurationclassenhancer$beanmethodinterceptor.intercept(configurationclassenhancer.java:318) @ org.springframework.boot.autoconfigure.web.webmvcautoconfiguration$enablewebmvcconfiguration$$enhancerbyspringcglib$$4bbac173.defaultservlethandlermapping(<generated>) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ org.springframework.beans.factory.support.simpleinstantiationstrategy.instantiate(simpleinstantiationstrategy.java:162) ... 79 common frames omitted
seems need let integration test context loader know spring security somehow.
edit:
i've narrowed problem down converter pulls in passwordencoder. removing converter , using passwordencoder directly in controller causes problem go away. also, removing security configuration allow me use converter fine.
here link repository made replicates issue: https://github.com/stephenturley/springbootsecuritytest.git
the last commit causes error. not sure why can't use spring converters , spring security. ideas?
i suspect initialization issue fixed adding delegatingfilterproxy support in boot 1.3. sent pull request resolves issue updating spring boot 1.3.2.release. resolves few other dependency issues having.
Comments
Post a Comment