简体   繁体   English

Spring Security无法解析XML

[英]Spring security does not parse xml

I have a problem that I can't using spring mvc 3.2.4 and spring security 3.1.4. 我有一个问题,我不能使用spring mvc 3.2.4和spring security 3.1.4。 They throw and exception that they can't parse spring-security.xml. 他们抛出了一个例外,他们无法解析spring-security.xml。 This is my sample code: 这是我的示例代码:

Controller: 控制器:

    @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 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: 弹簧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 MVC-调度-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>

Exception: 例外:

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)

Your error is quite clear: 您的错误非常清楚:

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

Since you say that the file is in the said location, it could be one of the following: 由于您说文件位于所述位置,因此它可能是以下之一:

  • The file is open in an application that acquires a byte-range lock , preventing the Spring process from accessing it. 该文件在获取字节范围锁的应用程序中打开 ,从而阻止Spring进程访问该文件。 Try closing all editor applications. 尝试关闭所有编辑器应用程序。

  • You're looking at a different copy of spring-security.xml. 您正在查看spring-security.xml的其他副本。 Search your root directory for all copies of the file. 在根目录中搜索文件的所有副本。 You could be looking at a different directory entirely, so you may be seeing the file but it's not actually referenced. 您可能会完全查看其他目录,因此您可能会看到该文件,但实际上并未引用该文件。

  • File is corrupted. 文件已损坏。 Try to copy and paste the contents into a new file and save it. 尝试将内容复制并粘贴到新文件中并保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM