2012-08-25 39 views
41

我正在從參考資料中學習spring security。版本3.1.2.RELEASE。至於說在我已經配置security:http標籤這樣獲取異常:未定義名爲'springSecurityFilterChain'的bean

安全的context.xml

<security:http auto-config="true"> 
     <security:intercept-url pattern="/**" access="ROLE_USER"/> 
    </security:http> 

的web.xml

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath*:*-context.xml</param-value> 
    </context-param> 

    <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> 

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

    <servlet> 
    <servlet-name>security</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>security</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 

安全servlet.xml中

<context:component-scan base-package="com.pokuri.security.mvc.controllers"/> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
     <property name="prefix" value="/WEB-INF/page/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

但我在啓動應用程序時遇到此異常。如果我刪除安全配置我的春天web應用程序工作正常。我在stackoverflow中經歷了同樣的問題。但沒有運氣。

+0

將其添加到「根應用程序上下文」或「DispatcherServlet應用程序上下文」。您可以通過擴展'AbstractAnnotationConfigDispatcherServletInitializer'輕鬆完成此操作。 – smwikipedia

回答

62

我認爲你的問題的原因可能是因爲你啓動你的web應用程序時你的xml配置文件沒有被加載。

爲了解決這個問題,你應該指定這樣在web.xml中所有的XML配置文件:

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

如果在classpath中你的配置文件(不是WEB-INF文件夾或它的子文件夾),那麼你可以以這種方式指定配置文件的列表;

... 
<param-value> 
    classpath:applicationContext.xml, 
    classpath:spitter-security.xml 
</param-value> 
... 

,你也需要添加特殊的聽衆,將加載您的配置文件:

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

但我有web.xml中的上下文參數和偵聽器配置 – Pokuri

+0

是的這是我的上下文參數的問題。我已經給參數值作爲類路徑*:* - context.xml,這是不正確的方式來獲取security-context.xml。因此將通配符更改爲classpath:**/* - context.xml。現在一切工作正常 – Pokuri

+2

注意:我必須將其添加到根應用程序上下文(而不是應用程序servlet上下文)。 –

3

添加此你的web.xml

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

<!-- Creates the Spring Container shared by all Servlets and Filters --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

     <!-- filter declaration for Spring Security --> 
<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> 
10

我剛添加的bean定義中的applicationContext .xml as spring問:

<bean id="springSecurityFilterChain" class="org.springframework.web.filter.DelegatingFilterProxy"/> 
相關問題