2013-09-28 48 views
1

我正在尝试构建一个带有客户端和其他安全项的maven项目的Web应用程序。我的Maven依赖关系是正确的,但我无法在安全模块中加载资源。如何从不同的maven项目导入spring资源?

这是我的web.xml中(src /主/ web应用/ 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> 

在我的根,我的context.xml尝试从另一个行家模块导入其他资源:

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

回答

2

使用META-INF作为弹簧文件位置不是一个好的做法。我认为它不是类路径的一部分。在Maven项目中,通常会将其添加到资源文件夹下。

例如src/main/resources/spring/

移动文件,并与下面的配置

<beans:import 
    resource="classpath:/spring/appServlet/servlet-security.xml" /> 
</beans:beans> 
尝试