Benito Ciaro Benito Ciaro - 7 months ago 139
Java Question

spring DelegatingFilterProxy can't find filter-name bean in web.xml when using AbstractSecurityWebApplicationInitializer

My web.xml looks like this

<!--listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener-->

<filter>
<filter-name>webContextFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>webContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


I commented out the JBoss
SpringContextLoaderListener
above to avoid an
IllegalStateException
because I am using a subclass of
AbstractSecurityWebApplicationInitializer
elsewhere (from a mandatory library) which itself creates the root application context.

When I run this under tomcat7, I get a


SEVERE: Exception starting webContextFilter
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'webContextFilter' is defined


The
applicationContext.xml
contains the appropriate
component-scan
definitions, so that's not the problem.

I am guessing maybe the context loader from
AbstractSecurityWebApplicationInitializer
is not yet started when DelegatingFilterProxy is created??

How can I fix this?

EDIT1: The
webContextFilter
bean is in the right package, i.e. subpackage of the one listed in
context:component-scan
, and the bean is defined as

@Component("webContextFilter")
public class WebContextFilter extends OncePerRequestFilter {
// ...
}


The bean is in a WEB-INF/lib/xyz.jar. This all used to work under tomcat6 when the JBoss resteasy
SpringContextLoaderListener
was uncommented in web.xml.

EDIT2: Perhaps my question boils down to this: when using
AbstractSecurityWebApplicationInitializer
to create the root context, how do I specify additional filters, since the ones in web.xml don't seem to work?

Answer

The problem was that the applicationContext.xml was not being read, so I added @ImportResource annotation to the SecurityConfig class definition.

@Configuration
@ImportResource("/WEB-INF/applicationContext.xml")
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}