Spring Security 需要 ContextLoaderListener,但我正在使用注释配置,该怎么办?

5

我曾经在我的Spring Web应用程序中使用注释配置,然后不得不将XML与其混合使用,以便我可以使用Spring Security。我使用@ImportResource("security-config.xml")注释之一来注释配置类,以加载安全配置。配置bean被成功创建。我的web.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="com-timbuk2-webapp-compositor" 
         version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                             http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" >

    <!-- Spring Security Chain -->
    <filter>
         <filter-name>springSecurityFilterChain</filter-name>
         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
       <filter-name>springSecurityFilterChain</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>

     <!--  Character Encoding -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--  URL Rewrite -->
    <filter>
        <filter-name>urlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
        <init-param>
            <param-name>logLevel</param-name>
            <param-value>commons</param-value>
        </init-param>
        <init-param>
            <param-name>confPath</param-name>
            <param-value>/WEB-INF/conf/urlrewrite-config.xml</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>urlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- Listeners -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <!-- Context Parameters -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/conf/log4j-config.xml</param-value>
    </context-param>

    <!-- Servlets -->
    <servlet>
        <servlet-name>app-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.website.config</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>

    <!-- Servlet mappings -->
    <servlet-mapping>
        <servlet-name>app-dispatcher</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>


</web-app>

据我所知,springSecurityFilterChain需要ContextLoaderListener。然而,由于我的应用程序配置方式的原因,如果我添加以下内容:
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

当我在web.xml文件中增加应用程序配置后,我的应用程序没有初始化。是否有一种方法可以在我的注释配置中手动创建ContextLoaderListener?


你尝试过JavaConfigWebApplicationContext方法吗?它也应该可以工作。 - Timo Westkämper
3个回答

2
只需使用ContextLoaderListener创建一个根应用程序上下文即可。
<context-param>
    <description>The Spring configuration files.</description>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/application.xml</param-value>
</context-param>

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

在application.xml中,您还可以定义基于注释的配置。它将被DispatcherServlet定义的WebApplicationContext继承。
然后在application.xml中导入您的安全配置。因此,安全性将应用于配置中的所有ApplicationContexts。

我按照你说的做了,但是我把“/WEB-INF/spring/application.xml”改成了更具描述性的“/WEB-INF/conf/security-config.xml”。感谢你的帮助! - Matt W
有没有办法在不使用XML文件的情况下仅使用注释来注册Spring监听器类? - Mital Pritmani

0

我相信通过在web-app下进行以下添加,它可以正常工作。

<context-param>
  <param-name>contextClass</param-name>
  <param-value>
    org.springframework.config.java.context.JavaConfigWebApplicationContext
  </param-value>
</context-param>
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>com.website.config</param-value>
</context-param>

来源:http://blog.springsource.com/2008/03/26/spring-java-configuration-whats-new-in-m3/


0
不要忘记添加 "classpath:/",如果你的 root.xml 文件与 web.xml 目录不同。希望这能帮助未来的某个人。
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/appcontext-root.xml</param-value>
</context-param>

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接