20

我正在尝试将弹簧安全性添加到常规 JSF 应用程序中。经过反复尝试,我在tomcat启动时出现以下错误。

这是整个堆栈跟踪。任何帮助表示赞赏。

编辑:在进一步的测试中,我注意到以下堆栈跟踪,在第二组关于 BeanFactory 未初始化或已关闭的严重异常之前

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderList
ener
java.lang.NoSuchMethodError: org.springframework.web.context.support.XmlWebApplicationContext.getEnvironment()Lorg/springframework/
core/env/ConfigurableEnvironment;
        at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:87)
        at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicat
ionContext.java:130)
        at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:46
7)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4205)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4704)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)
        at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1315)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1061)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
        at org.apache.catalina.core.StandardService.start(StandardService.java:525)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

After this error, the mojarra 2.1.4 is initialized before the BeanFactory exception shows up

SEVERE: Exception sending context destroyed event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
        at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:171)
        at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1048)
        at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1022)
        at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:970)
        at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:556)
        at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:142)
        at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4245)
        at org.apache.catalina.core.StandardContext.stop(StandardContext.java:4886)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4750)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)
        at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
        at org.apache.catalina.core.StandardService.start(StandardService.java:525)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

我的 web.xml 是

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">

    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
         /WEB-INF/applicationContext.xml
         /WEB-INF/applicationContext-security.xml
       </param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>false</param-value>
    </context-param>
    <context-param>
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>com.icesoft.faces.debugDOMUpdate</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>com.icesoft.faces.concurrentDOMViews</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>com.icesoft.faces.synchronousUpdate</param-name>
        <param-value>true</param-value>
    </context-param>

    <!-- Faces Servlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <!-- <load-on-startup>1</load-on-startup> -->
    </servlet>
    <servlet>
    <servlet-name>Resource Servlet</servlet-name>
    <servlet-class>com.icesoft.faces.webapp.CompatResourceServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
<!-- Extension Mapping -->
    <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
    <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/icefaces/*</url-pattern>
    </servlet-mapping>
    <session-config>
    <session-timeout>90</session-timeout>
    </session-config>


    <servlet-mapping>
    <servlet-name>Resource Servlet</servlet-name>
        <url-pattern>/xmlhttp/*</url-pattern>
    <url-pattern>/resources/*</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>/*</url-pattern>
    </filter-mapping>

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


<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

而我的 applicationContext-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.xsd">

<!--  key configuration here is an entry point to be used by security intercepts -->
<http realm="Sample Realm" entry-point-ref="authenticationEntryPoint" auto-config="false">

    <custom-filter ref="sessionManagementFilter" before="SESSION_MANAGEMENT_FILTER" />
    <!-- any role that is used to protect a directory, can be multiples -->
    <intercept-url pattern='/secure/**' access='ROLE_READER' />

    <!-- enable form login to use UsernamePasswordAuthenticationFilter [/j_spring_security_check] -->
    <form-login login-page="/general/logins/htmlLogin.faces"  
                    authentication-failure-url="/general/logins/loginFailed.jsf"/>

    <!-- logout page uses the default LogoutFilter, no changes are needed as IT accepts a GET call... -->
    <!-- here is an example logout link:
    <a href="#{request.contextPath}/j_spring_security_logout">Logout</a> -->
        <logout logout-url="/j_spring_security_logout"
                logout-success-url="/general/main.jsf"
                invalidate-session="true"/>
    </http>

    <beans:bean id="authenticationEntryPoint"
        class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
        <beans:property name="loginFormUrl" value="/general/logins/login.jsf" />
    </beans:bean>

    <!-- test with this before you hook up your LDAP or other Authentication Manager -->
    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="joe.blow@gr.am" password="pass1234" authorities="ROLE_READER"/>
                <user name="tester@gr.com" password="pass5678" authorities="ROLE_READER"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>

    <beans:bean id="sessionManagementFilter" class="org.springframework.security.web.session.SessionManagementFilter">
        <beans:constructor-arg name="securityContextRepository" ref="httpSessionSecurityContextRepository" />
        <beans:property name="invalidSessionUrl" value="/general/logins/sessionExpired.jsf" />
        <!-- this permits redirection to session timeout page from javascript/ajax or http -->
        <beans:property name="redirectStrategy" ref="jsfRedirectStrategy" />
    </beans:bean>

    <beans:bean id="jsfRedirectStrategy" class="com.palm.safe.spring.security.JsfRedirectStrategy"/>
    <beans:bean id="httpSessionSecurityContextRepository" class="org.springframework.security.web.context.HttpSessionSecurityContextRepository"/>

</beans:beans>
4

7 回答 7

12

我在以下代码中看到了这段代码applicationContext-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.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.0.xsd">

您可能使用了不正确的 spring 版本。例如,如果您在库中使用 spring-xxx(spring-security-web.jar 或 spring-security-core.jar)版本 3.1.xx,则会出现此错误。尝试将代码applicationContext-security.xml3.0.xsd更改为3.1.xsd(代码中的版本号必须与库文件夹中的版本匹配)。

希望这有帮助。

于 2012-12-10T14:04:07.833 回答
6

您需要添加RequestContextListener到您的web.xml功能,即将 HTTP 请求对象绑定到为该请求提供服务的线程。这使得请求和会话范围的 bean 在调用链的下游可用。

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
于 2012-09-10T03:40:04.317 回答
5

我遇到了同样的问题,结果证明 maven spring 依赖链存在问题。依赖 org.springframework spring-context 是使用 3.0.5 版本导入的,而我使用的整个 spring 版本是 3.2.2。在我的 pom.xml 中显式添加依赖项解决了它。

我通过在运行服务器时使用 -verbose:class jvm 参数检测到问题,并检查 XmlWebApplicationContext 继承树中的 jar 类是从中加载的。

于 2013-05-23T06:44:09.453 回答
4

在从 applicationcontext.xml 中的特定块中删除评论时,我错过了删除 --> “评论结束”。我没有注意到它,并且收到了这个错误。

后来删除它,一切正常!

于 2013-05-30T18:21:03.467 回答
2

我也遇到了这个错误,问题是,通过剪切和粘贴,我无意中在我的 spring-servlet.xml 中列出了同一个 bean 两次:

<bean id="pathInfoHelper" class="org.nationalhistory.util.PathInfoHelper">
</bean>

<bean id="imageIndexer" class="org.nationalhistory.util.ImageIndexer"/>

<bean id="pathInfoHelper" class="org.nationalhistory.util.PathInfoHelper">
</bean>

我在这里查看并在上面 om39a 的回答的帮助下找到了这个(谢谢!)。我刚刚编辑了文件,并且知道我有可能无意中粘贴了一些额外(或更少)的东西。然而,这个复式不是错误所指的 bean!

于 2016-05-25T08:04:57.627 回答
0

我遇到了这个问题,直到我从服务器的部署中删除了有问题的项目(在 JBoss Dev Studio 中,右键单击服务器并在服务器视图中“删除”该项目),然后执行以下操作:

  1. 在没有部署任何项目的情况下重新启动 JBoss EAP 6.1 服务器。
  2. 服务器启动后,我将相关项目添加到服务器。

在此之后,只需通过选择服务器而不是项目本身来重新启动服务器(在调试或运行模式下)。

这似乎刷新了任何以前的设置/状态/内存/导致问题的任何内容,并且我不再收到错误。

于 2014-07-16T07:12:46.777 回答
0

在你的 pom.xml 中使用这个依赖

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework.version}</version> </dependency>

于 2015-05-30T21:53:49.890 回答