【问题标题】:Spring security does not parse xmlSpring Security 不解析 xml
【发布时间】:2014-01-02 16:19:34
【问题描述】:

我有一个问题,我无法使用 spring mvc 3.2.4 和 spring security 3.1.4。他们抛出无法解析 spring-security.xml 的异常。这是我的示例代码:

控制器:

    @Controller
public class HomeController {

    @RequestMapping("/home")
    public ModelAndView getHome() {
        String string = "Congrats ! You are done with your first Spring Security configuration !";
        return new ModelAndView("home", "string", string);
    }

}

web.xml

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

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

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

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

spring-security.xml:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="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-3.2.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security.xsd">

    <http auto-config="true">
        <intercept-url pattern="/home*" access="ROLE_ADMIN" />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="admin" password="admin" authorities="ROLE_ADMIN" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

mvc-dispatcher-servlet.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="com.beingjavaguys.controller" />

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

</beans>

例外:

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/spring-security.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/spring-security.xml]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)

【问题讨论】:

  • 文件真的是你所说的/WEB-INF/spring-security.xml吗?
  • @SotiriosDelimanolis:是的

标签: java xml spring spring-mvc spring-security


【解决方案1】:

你的错误很清楚:

FileNotFoundException: Could not open ServletContext resource [/WEB-INF/spring-security.xml]

既然你说文件在上述位置,它可能是以下之一:

  • 文件在获取字节范围锁的应用程序中打开,阻止 Spring 进程访问它。尝试关闭所有编辑器应用程序。

  • 您正在查看 spring-security.xml 的不同副本。 在您的根目录中搜索该文件的所有副本。您可能正在查看完全不同的目录,因此您可能会看到该文件但实际上并未引用它。

  • 文件已损坏。尝试将内容复制并粘贴到新文件中并保存。

【讨论】:

    猜你喜欢
    • 2014-05-09
    • 2016-03-05
    • 2019-04-14
    • 2017-06-09
    • 2015-02-02
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    相关资源
    最近更新 更多