2014-01-31 51 views
4

我需要春天的安全與春天3.1.1建立了一個小型應用程序集成,在之後的點3.2.1 this教程,得到這個錯誤跟蹤:沒有名爲springSecurityFilterChain bean被定義

gen 31, 2014 3:08:41 PM org.apache.catalina.core.StandardContext filterStart 
Grave: Exception starting filter springSecurityFilterChain 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1097) 
    at org.springframework.web.filter.DelegatingFilterProxy.initDelegate(DelegatingFilterProxy.java:326) 
    at org.springframework.web.filter.DelegatingFilterProxy.initFilterBean(DelegatingFilterProxy.java:236) 
    at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:194) 
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:281) 
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262) 
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:107) 
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4775) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5452) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) 
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633) 
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:656) 
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1635) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:744) 

gen 31, 2014 3:08:41 PM org.apache.catalina.core.StandardContext startInternal 
Grave: Error filterStart 
gen 31, 2014 3:08:41 PM org.apache.catalina.core.StandardContext startInternal 
Grave: Context [/mmasgis] startup failed due to previous errors 
gen 31, 2014 3:08:41 PM org.apache.catalina.core.ApplicationContext log 
Informazioni: Closing Spring root WebApplicationContext 
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Closing Root WebApplicationContext: startup date [Fri Jan 31 15:08:40 CET 2014]; root of context hierarchy 
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.s[email protected]204ed39b: defining beans [dataSource]; root of factory hierarchy 
gen 31, 2014 3:08:41 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc 
Grave: The web application [/mmasgis] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. 
gen 31, 2014 3:08:41 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads 
Grave: The web application [/mmasgis] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. 
gen 31, 2014 3:08:41 PM org.apache.catalina.startup.HostConfig deployDirectory 
Informazioni: Deploying web application directory /home/arpho/programmi/sts/springsource/vfabric-tc-server-developer-2.9.3.RELEASE/base-instance/webapps/manager 
gen 31, 2014 3:08:41 PM org.apache.catalina.startup.HostConfig deployDirectory 
Informazioni: Deploying web application directory /home/arpho/programmi/sts/springsource/vfabric-tc-server-developer-2.9.3.RELEASE/base-instance/webapps/ROOT 
gen 31, 2014 3:08:41 PM org.apache.coyote.AbstractProtocol start 
Informazioni: Starting ProtocolHandler ["http-bio-8080"] 
gen 31, 2014 3:08:41 PM org.apache.catalina.startup.Catalina start 
Informazioni: Server startup in 2497 ms 

我有提到其他線程但沒有解決方案。有人可以糾正我的配置問題嗎?

謝謝你的時間。

這是我web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
      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_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.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> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>mmasgisServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mmasgisServlet</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <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> 
</web-app> 

這是我的root-context.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <!-- Root Context: defines shared resources visible to all other web components --> 

    <bean id="dataSource" name="dataSource" 
       class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="username" value="root"></property> 
     <property name="password" value="password"></property> 
     <property name="driverClassName"> 
      <value>com.mysql.jdbc.Driver</value> 
     </property> 
     <property name="url" 
          value="jdbc:mysql://localhost:3306/mmasgis"> 
     </property> 
    </bean> 

    <beans xmlns="http://www.springframework.org/schema/beans" 
       xmlns:security="http://www.springframework.org/schema/security" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
            http://www.springframework.org/schema/security 
            http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 
    </beans> 
</beans> 

回答

2

我不知道你是否已經給出了web.xml文件已完成,但你必須有至少在該文件中:

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

之後,我認爲你至少需要在你的根-context.xml文件1個HTTP配置(我不知道這個):

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

最有可能你缺少從配置的<http>元素。 你需要創建Spring Security的最低配置,使用安全命名空間:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:sec="http://www.springframework.org/schema/security" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/security 
     http://www.springframework.org/schema/security/spring-security-3.1.xsd 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> 

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

</beans:beans> 

而且 - 你的XML配置 - 如果這是一個單一的文件,那麼你並不需要聲明<beans xmlns...元素兩次。

希望這有幫助 - 我進入了一些額外的細節on my blog如果你需要深入挖掘。

+0

什麼是Java配置等效? – Samuel

5

確保安全xml由ContextLoaderListener而不是DispatcherServlet加載。 DelegatingFilterProxy只會查看bean委託給的根applicationcontext(由ContextLoaderListener加載)。 (請參閱spring的文檔here

如果您只有一個調度程序servlet,則可以全局定義應用程序上下文,並將調度程序servlet參數留空。

例子:

<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-context.xml, 
        /WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
    </context-param> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>mmasgisServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value></param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
0

我最近也遇到了這個問題,並添加<http>元素解決的主要問題,但我的上下文初始化還是失敗了。通過我的Tomcat 8錯誤日誌挖掘,我發現埋在下面一行:

Did you forget to add a global <authentication-manager> element to your configuration 
(with child <authentication-provider> elements)? Alternatively you can use the 
authentication-manager-ref attribute on your <http> and <global-method-security> elements. 

添加這種額外的元素,這是我Spring Security的文檔中找到,解決了這個問題:

<authentication-manager> 
    <authentication-provider> 
     <user-service> 
      <user name="jimi" password="jimispassword" authorities="ROLE_USER, ROLE_ADMIN" /> 
      <user name="bob" password="bobspassword" authorities="ROLE_USER" /> 
     </user-service> 
    </authentication-provider> 
</authentication-manager> 

只是想因爲這個問題是相關的,並且Spring Security的其他人可能會碰到它。

相關問題