Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two separate Spring contexts for one webapp

I want to use two different Spring web contexts, each have own contextConfig, spring servlet and filter, that should be mapped to different urls. I have a

  1. Standard Grails project, mapped to '/'
  2. And an existing Spring webapp, that I want to map to /extra/

I know that I can deploy both into one Tomcat, but I'm looking for a way of making one app (one war, etc), because It can simplify our deployment and development process.

This applications don't need to share beans or anything, should be completely separate. Both have DispatcherServlet and DispatcherFilter (and both are using Spring Security, but different configuration)

How I can configure web.xml for such webapp?

I've tried to add 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 context xml in classpath (inside exra library jar):
    • extra-spring-web.xml
    • extra-spring-security.xml (!!! how I should configure it?)
  • extra-spring-security.xml
    • is pretty standard Spring Security config
    • have configured bean extraSecurityFilterBean
    • have dependecy to beans from -web context (but it's not required to be)

It's semi-working now:

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

So, the question, how I can define context for DelegatingFilterProxy? I even tried to add this files into main context (contextConfigLocation param), it's not what i'm looking for, but it didn't work.

I've taken a look into DelegatingFilterProxy sources, but it's not clear for me how it loads the context.

like image 249
Igor Artamonov Avatar asked Jun 25 '12 06:06

Igor Artamonov


2 Answers

As per my comment on the question, if the security filter chain is defined in extra-spring-security.xml then you need to ensure that that file is loaded by your extra DispatcherServlet in addition to extra-spring-web.xml either by <import>ing the -security file from the -web one or configuring 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 will also need to ensure that the security filter in the Grails application doesn't apply to /extra URIs, exactly how you do this depends on whether you're using annotations, database RequestMap entries etc.

like image 169
Ian Roberts Avatar answered Sep 28 '22 00:09

Ian Roberts


If the modules are completely separate: the easiest way is to package them as two different webapp. Tens of different spring-based apps can run in one appserver -even on a modest developer machine- without issues.

like image 27
Gergely Szilagyi Avatar answered Sep 28 '22 01:09

Gergely Szilagyi