1

我正在尝试使用一个带有客户端的 Maven 项目和其他带有安全性的项目来构建一个 Web 应用程序。我的 Maven 依赖项是正确的,但我无法在安全模块中加载资源。

这是我的 web.xml (src/main/webapp/WEB-INF/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>appServlet</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>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <mime-mapping>
        <extension>js</extension>
        <mime-type>application/x-javascript</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>properties</extension>
        <mime-type>application/text</mime-type>
    </mime-mapping>
    <mime-mapping>
        <extension>xml</extension>
        <mime-type>application/xml</mime-type>
    </mime-mapping>

    <!-- Enables Security -->
    <!-- <listener> <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class> 
        </listener> <filter> <filter-name>monitoring</filter-name> <filter-class>net.bull.javamelody.MonitoringFilter</filter-class> 
        </filter> <filter-mapping> <filter-name>monitoring</filter-name> <url-pattern>/*</url-pattern> 
        </filter-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 中,我尝试从另一个 Maven 模块导入另一个资源:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- <beans:bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <beans:property name="location" value="classpath:resources.properties"> </beans:property> 
    </beans:bean> -->
<beans:import
    resource="classpath:/META-INF/spring/appServlet/servlet-security.xml" />
</beans:beans>

我的 servlet-security.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">
 
    <http auto-config="true"  use-expressions="true">
        <intercept-url pattern="/login" access="permitAll" />
        <intercept-url pattern="/logout" access="permitAll" />
        <intercept-url pattern="/accessdenied" access="permitAll" />
        <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
        <form-login login-page="/login" default-target-url="/list" authentication-failure-url="/accessdenied" />
        <logout logout-success-url="/logout" />
    </http>
 
    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="vitorn" password="password1" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

当我在服务器中部署我的战争时,我得到了这个异常。我如何从另一个模块导入资源???

INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [META-INF/spring/appServlet/servlet-security.xml]
ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:/META-INF/spring/appServlet/servlet-security.xml]
Offending resource: ServletContext resource [/WEB-INF/spring/root-context.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/spring/appServlet/servlet-security.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/spring/appServlet/servlet-security.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
4

1 回答 1

3

使用 META-INF 作为 spring 文件位置不是一个好习惯。我认为它不是类路径的一部分。在 Maven 项目中,通常添加在资​​源文件夹下。

例如src/main/resources/spring/

移动您的文件并尝试使用以下配置

<beans:import
    resource="classpath:/spring/appServlet/servlet-security.xml" />
于 2013-09-28T15:39:16.597 回答