spring - Configuring RequestContextListener in SpringBoot -


i have spring-boot application uses spring-security. have request scoped bean want autowire 1 of custom filters in security filter chain, @ moment not working.

i understand config needed use request scoped beans outside of dispatcherservlet, , have read http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes-other have not had success yet:

for servlet 3.0+, can done programmatically via webapplicationinitializer interface.

(i using latest tomcat servlet 3+)

i have tried using both requestcontextlistener , requestcontextfilter (docs they, , dispatcherservlet, exact same thing), in both cases still errors because autowired object null:

my attempt register filter

@configuration @componentscan @enableautoconfiguration class application extends springbootservletinitializer  {      @override protected springapplicationbuilder configure( springapplicationbuilder application ) {         application.sources( application )     }      @override public void onstartup( servletcontext servletcontext ) throws servletexception {         super.onstartup( servletcontext )         servletcontext.addfilter("requestcontextfilter", new requestcontextfilter() ).addmappingforurlpatterns(null, false, "/*")     } 

my attempt register listener

@configuration @componentscan @enableautoconfiguration class application extends springbootservletinitializer  {      @override protected springapplicationbuilder configure( springapplicationbuilder application ) {         application.sources( application )     }      @override public void onstartup( servletcontext servletcontext ) throws servletexception {         super.onstartup( servletcontext )         servletcontext.addlistener( new requestcontextlistener() )      } 

am missing obvious? have had @ auto config source code spring boot , haven't come across yet.


update

i being idiot, had added filter in springsecurity configuration, inside configure() method:

http.addfilterbefore( new preauthfilter(), basicauthenticationfilter ) 

but hadn't registered new filter bean. per m. denium's comment below, didn't need additional config explicitly adding listener/filter, registering bean enough.

as detailed in update/comments, caused own stupidity.

spring-boot able autowire request/session scoped beans filter's outside of dispatcherservlet per spring's documentation, need add requestcontextlistener or requestcontextfilter enable functionality:

to support scoping of beans @ request, session, , global session levels (web-scoped beans), minor initial configuration required before define beans. (this initial setup not required standard scopes, singleton , prototype.) ...

if access scoped beans within spring web mvc, in effect, within request processed spring dispatcherservlet, or dispatcherportlet, no special setup necessary: dispatcherservlet , dispatcherportlet expose relevant state.

to handle this, needed register requestcontextlistener bean:

@bean public requestcontextlistener requestcontextlistener(){     return new requestcontextlistener(); }  

if don't register bean, error stating trying access request scope outside of dispatcherservlet.

the problem experienced(autowired objects not being injected) caused fact registering custom filter standard class instance, not spring managed bean:

http.addfilterbefore( new preauthfilter(), basicauthenticationfilter ) 

to solve this, moved creation of preauthfilter sepearte @bean method, @autowired functionality worked fine.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -