Multiple config files for Spring Security

2019-07-27 06:30发布

问题:

I'm quite new to all things Spring, and right now I'm developing an application that uses Spring, Spring MVC and Spring Security.

My problem is that I'm using two dispatcher Servlets, one for /csm/*.html and another one for *.html and I'd like to have one Spring Security configuration file per servlet.

Is this possible at all?, if so, could you point me to an example?.

回答1:

This answer relates to springframework 2.5.6, it might have changed in later versions.

use the pattern /WEB-INF/[servlet-name]-servlet.xml or specify it in the web.xml like this:

<servlet>
    <servlet-name>handler</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <!-- override default name {servlet-name}-servlet.xml -->
    <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-myconfig.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

If you do not set the contextConfigLocation it defaults to handler-servlet.xml (at least in this example).

application wide stuff belongs into /WEB-INF/applicationContext.xml. But you also can change the default and even add multiple files:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        WEB-INF/spring-dao-hibernate.xml,
        WEB-INF/spring-services.xml,
        WEB-INF/spring-security.xml
    </param-value>
</context-param>

you can find a more specific answer on the spring website, the documentation is quite good.