Two separate Spring contexts for one webapp

I want to use two different Spring web contexts , each of which has its own contextConfig, Spring servlet and filter, which should be mapped to different URLs. I have

  • Standard Grails project mapped to '/'
  • And the existing Spring webapp that I want to map to /extra/

I know that I can deploy both in the same Tomcat, but I am looking for a way to make one application (one war , etc.), since it can simplify the process of deployment and development.

These applications should not use beans or anything else, should be completely separated. Both have DispatcherServlet and DispatcherFilter (and both use Spring Security, but a different configuration)

How can I customize web.xml for such a webapp?

I tried to add a new filter:

 <filter> <filter-name>extraSpringSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>contextAttribute</param-name> <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.extraSpring</param-value> </init-param> <init-param> <param-name>targetBeanName</param-name> <param-value>extraSecurityFilterBean</param-value> </init-param> </filter> <filter-mapping> <filter-name>extraSpringSecurityFilterChain</filter-name> <url-pattern>/extra/*</url-pattern> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> </filter-mapping> 

and Spring dispatcher servlet:

 <servlet> <servlet-name>extraSpring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>springConfigLocation</param-name> <param-value>classpath:extra-spring-web.xml</param-value> </init-param> </servlet> 

Where:

  • two xml contexts in the classpath (inside the exra library library):
    • extra-spring -web.xml
    • extra- spring -security.xml ( !!! how to configure it?)
  • extra-spring -security.xml
    • pretty standard spring security configuration
    • configured bean extraSecurityFilterBean
    • have beans dependency on -web context (but this is not required)

Now it works midnight:

  • as I can see from the logs, the extraSpring servlet successfully loads beans from extra-spring-web.xml
  • but after accessing url / /extra/ I got a NoSuchBeanDefinitionException : No bean named "extraSecurityFilterBean" is defined.

So the question is, how can I define the context for DelegatingFilterProxy ? I even tried adding these files to the main context (contextConfigLocation param), this is not what I am looking for, but it did not work.

I looked at the sources of DelegatingFilterProxy, but it is not clear to me how it loads the context.

+7
source share
3 answers

According to my comment on the question, if the security filter chain is defined in extra-spring-security.xml , you need to make sure that this file is loaded with your optional DispatcherServlet in addition to extra-spring-web.xml or the <import> ing file security from -web or configure it as:

 <servlet> <servlet-name>extraSpring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:extra-spring-web.xml classpath:extra-spring-security.xml </param-value> </init-param> </servlet> 

You also need to make sure that the security filter in the Grails application is not applied to the URI /extra , how you do it depends on whether you use annotations, records in the RequestMap database, etc.

+2
source

If the modules are completely separate: the easiest way is to pack them as two different webapps. Dozens of different spring-based applications can run on the same application server - even on a modest development machine - without any problems.

+1
source

A few questions

  • What does your Spring security configuration look like?
  • I am confused why the error says "No bean with the name" apiservSecurityFilterChain "is defined", but in web.xml you placed only extraSpringSecurityFilterChain links (the bean names must match or some important configuration is missing).

Possible answer

I assume that the problem is that the filter name must match the name of the Spring Security bean (it cannot be known exactly without seeing the Spring security configuration used). The default value used in the Spring Security namespace is springSecurityFilterChain, so try using web.xml instead instead (note that extraSpringSecurityFilterChain is changed to springSecurityFilterChain):

 <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>contextAttribute</param-name> <param-value>org.springframework.web.servlet.FrameworkServlet.CONTEXT.extraSpring</param-value> </init-param> <init-param> <param-name>targetBeanName</param-name> <param-value>extraSecurityFilterBean</param-value> </init-param> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/extra/*</url-pattern> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> </filter-mapping> 
0
source

All Articles