【问题标题】:spring web, security + web.xml + mvc dispatcher + Bean is created twicespring web, security + web.xml + mvc dispatcher + Bean 创建两次
【发布时间】:2013-11-18 12:36:11
【问题描述】:

我的 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>
<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>/api/secure/*</url-pattern>
</filter-mapping>

[编辑]

在我添加了弹簧安全之后,我得到了错误!

java.lang.IllegalStateException:未找到 WebApplicationContext:否 ContextLoaderListener 注册了吗?

然后我添加了

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/mvc-dispatcher-servlet.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

然后它似乎工作正常,但随后 1)问题是bean被创建了两次!

如果我只删除它:

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

但是留下&lt;listener&gt; 然后Web 应用程序根本不会运行

[额外]

完整的 Web.xml 如下:

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>

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

    <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>/api/secure/*</url-pattern>
    </filter-mapping>

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

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

</web-app>

这是我的 mvc-dispatcher-servlet.xml

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

    <mvc:annotation-driven/>
    <context:annotation-config/>
    <context:component-scan base-package="com.ge.wtracker"/>
    <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>

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

    <!--Java Persistence API config-->
    <jpa:repositories base-package="com.ge.wtracker.repository"/>

    <!--JPA and Database Config-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="defaultPersistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
        <property name="driverClassName" value="${database.driverClassName}"/>
        <property name="url" value="${database.url}"/>
        <property name="username" value="${database.username}"/>
        <property name="password" value="${database.password}"/>
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
        <property name="testWhileIdle" value="true"/>
        <property name="timeBetweenEvictionRunsMillis" value="1800000"/>
        <property name="numTestsPerEvictionRun" value="3"/>
        <property name="minEvictableIdleTimeMillis" value="1800000"/>
        <property name="validationQuery" value="SELECT 1"/>
    </bean>

    <!--Spring Security-->
    <security:http create-session="stateless" entry-point-ref="restAuthenticationEntryPoint" authentication-manager-ref="authenticationManager">
        <security:intercept-url pattern="/api/secure/**" access="ROLE_USER" />
        <security:custom-filter ref="customRestFilter" position="BASIC_AUTH_FILTER" />
    </security:http>
    <!-- Configures the authentication entry point that returns HTTP status code 401 -->
    <bean id="restAuthenticationEntryPoint" class="com.ge.wtracker.web.security.RestAuthenticationEntryPoint">
        <property name="realmName" value="Not Authorized" />
    </bean>
    <security:authentication-manager alias="authenticationManager">
        <security:authentication-provider ref="restAuthenticationProvider" />
    </security:authentication-manager>

    <!--The customRestFilter responsibles for retrieving and manipulating any request data to pass to the authentication
        provider to authenticate.-->
    <bean id="customRestFilter" class="com.ge.wtracker.web.security.RestSecurityFilter">
        <constructor-arg name="authenticationManager" ref="authenticationManager" />
        <constructor-arg name="authenticationEntryPoint" ref="restAuthenticationEntryPoint" />
    </bean>
    <bean id="restAuthenticationProvider" class="com.ge.wtracker.web.security.RestAuthenticationProvider" />

</beans>

【问题讨论】:

  • 如果您离开监听器,它不会运行指定哪个错误?未找到 WebApplicationContext:未注册 ContextLoaderListener?
  • 没有错误,只返回响应码:404 响应消息:未找到
  • 请出示 bean 被创建两次的证据。发布您的日志。
  • 我在 dubug 模式下运行,并将断点放在我的“RestSecurityFilter”构造函数中,该构造函数扩展了 GenericFilterBean。如果我在 web.xml 中有 它会运行两次,如果我删除它们,它会运行一次!

标签: java spring spring-mvc spring-security


【解决方案1】:

在 servlet 容器生命周期中,容器首先初始化 ServletContextListener,然后是 FilterServlet 实例。

Spring Web 应用程序通常加载两个上下文:根上下文和调度程序 servlet 上下文。 ContextLoaderListener 类是一个 ServletContextListener,它加载应用程序(或根)上下文。它通过context-param 标识要加载的文件,名称为contextConfigLocation,如下面的web.xml 中给出的

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

或者,默认情况下,通过在/WEB-INF/applicationContext.xml 查找文件。由于您已将 /WEB-INF/mvc-dispatcher-servlet.xml 指定为 contextConfigLocation,因此将加载该上下文。

完成后,容器会初始化DispatcherServlet,它还会加载上下文。它通过名称为contextConfigLocationinit-param 元素标识文件加载,如下面的web.xml 中给出的

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/some-random-location.xml</param-value>
    </init-param>
</servlet>

或者,默认情况下,通过在/WEB-INF/name-of-your-servlet-servlet.xml 查找文件。换句话说,它获取&lt;servlet-name&gt; 元素的值并将-servlet.xml 附加到它并在WEB-INF 中查找它。

由于您没有指定名称为contextConfigLocationinit-paramDispatcherServlet 将在/WEB-INF/mvc-dispatcher-servlet.xml 加载上下文文件,因为它的名称是mvc-dispatcherDispatcherServlet 加载的上下文可以访问ContextLoaderListener 加载的 bean,这就是我们称其为根上下文(以及其他子上下文)的原因。

所有这一切都表明您的ContextLoaderListener 和您的DispatcherServlet 都通过从/WEB-INF/mvc-dispatcher-servlet.xml 的同一文件中加载XmlWebApplicationContext 来创建自己的ApplicationContext 副本。

确定您认为应该对整个应用程序可用的 bean 或配置,并将它们放入将由 ContextLoaderListener 加载的文件中。确定您认为应该可用于DispatcherServlet 的 bean 或配置,并将它们放入其上下文文件中。

【讨论】:

    【解决方案2】:

    http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch16s02.html

    框架将在 DispatcherServlet 初始化时寻找 WEB-INF 目录下的 [servlet-name]-servlet.xml 文件 您的 Web 应用程序并创建在那里定义的 bean(覆盖 在全局中以相同名称定义的任何 bean 的定义 范围)。

    所以你可以删除上下文参数:

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

    【讨论】:

      【解决方案3】:

      Spring MVC 然后创建一个新的 any-name.xml 并放置特定于上下文的 bean,例如 Spring-security.xml 和你的安全 bean 加载。

      新建的xml会是这样的

      <?xml version="1.0" encoding="UTF-8"?>
      <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"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:task="http://www.springframework.org/schema/task"
      xsi:schemaLocation="
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-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/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/task
      http://www.springframework.org/schema/task/spring-task-3.1.xsd">
      
       <mvc:annotation-driven/>
        <context:component-scan base-package="com"/>
      <task:annotation-driven/> 
      <bean id="propertyConfigurer"
      class="org.springframework.beans.factory.
      config.PropertyPlaceholderConfigurer">
       <property name="location" value=""/>
          <property name="locations">
                 <list>
                     <value>/WEB-INF/jdbc.properties</value>
                 </list>
              </property>
      </bean>
      <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
              <property name="driverClassName" value="${jdbc.driverClassName}"/>
              <property name="url" value="${jdbc.databaseurl}"/>
              <property name="username" value="${jdbc.username}"/>
              <property name="password" value="${jdbc.password}"/>            
      </bean>
      </beans>
      

      现在将其包含在 web.xml 中

        <context-param>
         <param-name>contextConfigLocation</param-name>
          <param-value>  
            /WEB-INF/login-security.xml,
            /WEB-INF/application-context.xml
         </param-value>
       </context-param>
      

      希望这会对你有所帮助。

      【讨论】:

        猜你喜欢
        • 2017-04-17
        • 2023-03-02
        • 2020-07-02
        • 2016-03-18
        • 2021-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多