java.lang.IllegalStateException:getAttribute:会话已经失效

Posted

技术标签:

【中文标题】java.lang.IllegalStateException:getAttribute:会话已经失效【英文标题】:java.lang.IllegalStateException: getAttribute: Session already invalidated 【发布时间】:2013-08-29 20:31:16 【问题描述】:

在我的代码中使 portletRequest 会话无效后,我收到以下异常

Aug 27, 2013 7:07:13 AM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet xyzapplication Servlet threw exception java.lang.IllegalStateException: getAttribute: Session already invalidated
    at org.apache.catalina.session.StandardSession.getAttribute(StandardSession.java:1165)
    at org.apache.catalina.session.StandardSessionFacade.getAttribute(StandardSessionFacade.java:122)
    at com.liferay.portal.servlet.SharedSessionWrapper.getAttribute(SharedSessionWrapper.java:75)
    at com.liferay.portlet.PortletSessionImpl.getAttribute(PortletSessionImpl.java:55)
    at org.springframework.web.portlet.context.PortletRequestAttributes.updateAccessedSessionAttributes(PortletRequestAttributes.java:256)
    at org.springframework.web.context.request.AbstractRequestAttributes.requestCompleted(AbstractRequestAttributes.java:48)
    at org.springframework.web.portlet.FrameworkPortlet.processRequest(FrameworkPortlet.java:552)
    at org.springframework.web.portlet.FrameworkPortlet.processAction(FrameworkPortlet.java:460)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:70)
    at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:48)
    at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:73)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:593)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:530)
    at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:534)
    at com.liferay.portlet.InvokerPortletImpl.invokeAction(InvokerPortletImpl.java:579)
    at com.liferay.portlet.InvokerPortletImpl.processAction(InvokerPortletImpl.java:294)
    at com.liferay.portal.action.LayoutAction.processPortletRequest(LayoutAction.java:944)
    at com.liferay.portal.action.LayoutAction.processLayout(LayoutAction.java:688)
    at com.liferay.portal.action.LayoutAction.execute(LayoutAction.java:249)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    at com.liferay.portal.struts.PortalRequestProcessor.process(PortalRequestProcessor.java:176)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at com.liferay.portal.servlet.MainServlet.callParentService(MainServlet.java:560)
    at com.liferay.portal.servlet.MainServlet.service(MainServlet.java:537)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:163)
    at com.liferay.portal.servlet.filters.secure.SecureFilter.processFilter(SecureFilter.java:294)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:57)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:108)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:73)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
    at com.liferay.portal.servlet.FriendlyURLServlet.service(FriendlyURLServlet.java:138)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:163)
    at com.liferay.portal.servlet.filters.strip.StripFilter.processFilter(StripFilter.java:335)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:57)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:108)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:163)
    at com.liferay.portal.servlet.filters.gzip.GZipFilter.processFilter(GZipFilter.java:123)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:57)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:108)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:163)
    at com.liferay.portal.servlet.filters.secure.SecureFilter.processFilter(SecureFilter.java:294)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:57)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:108)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:163)
    at com.liferay.portal.servlet.filters.i18n.I18nFilter.processFilter(I18nFilter.java:241)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:57)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:108)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:163)
    at com.liferay.portal.servlet.filters.autologin.AutoLoginFilter.processFilter(AutoLoginFilter.java:246)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:57)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:108)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:163)
    at com.liferay.portal.servlet.filters.sso.ntlm.NtlmPostFilter.processFilter(NtlmPostFilter.java:83)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:57)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:108)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:163)
    at com.liferay.portal.sharepoint.SharepointFilter.processFilter(SharepointFilter.java:80)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:57)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:108)
    at com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:163)
    at com.liferay.portal.servlet.filters.virtualhost.VirtualHostFilter.processFilter(VirtualHostFilter.java:216)
    at com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:57)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:108)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:187)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:95)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:738)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:206)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:108)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:167)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:95)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:167)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:95)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:116)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDirectCallFilter(InvokerFilterChain.java:187)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:95)
    at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:73)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at com.googlecode.psiprobe.Tomcat60AgentValve.invoke(Tomcat60AgentValve.java:30)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722) 

我的会话失效代码如下: 顺便说一下,这个方法是从另一个带有@ActionMapping标签注解的方法调用的。

 private void exit(PortletRequest request, ActionResponse response) 
    logger.debug("Entering exit");
    String exitURL = Constants.EXIT_URL;
    request.getPortletSession().invalidate();
    try 
        response.sendRedirect(exitURL);
     catch (IOException ex) 
        logger.error("Error: IOException: ", ex.getMessage());
        throw new RuntimeException(ex);
    

    logger.debug("Exiting exit");

我在 *** 中发现了一个与会话失效有关的已经存在的问题 IllegalStateException: getAttribute: Session already invalidated 但就我而言,我无法从中得到太多。

当这个错误发生时,当控制转到 Spring 代码并且我无法解决这个问题时,有人可以帮我解决这个问题吗?

【问题讨论】:

您可以尝试将SessionStatus status 添加到您的方法签名中,然后执行status.setCompleted(),而不是通过请求使会话无效吗?另外,请在执行response.sendRedirect() 后尝试使无效。 request.getPortletSession().invalidate()的执行点没有出现错误;甚至在 response.sendRedirect(exitURL); 的时候也没有。它发生在我的控制超出我的书面代码时,即当控制回到弹簧时。所以我猜上述语句的执行顺序并不重要。 会话范围的 bean 会导致任何问题吗?从某种意义上说,我必须分别使它们无效吗?如果是,我应该什么时候使它们无效,我怎样才能使它们无效? 从堆栈跟踪来看,似乎异常发生在 LifeRay 的 servlet 过滤器之一中。我的猜测是 LifeRay 在您已经使会话无效后试图访问它。我建议尝试上面提到的 SessionStatus 和 .setCompleted()。由于它不直接作用于 Session,它可能会延迟实际的会话失效,直到所有进程都完成之后。 是的,我相信这正是正在发生的事情,现在我不明白如何解决您的问题,您能否将示例代码与您的答案相关联? 【参考方案1】:

尝试改用 SessionStatus:

private void exit(PortletRequest request, ActionResponse response, SessionStatus status) 
    logger.debug("Entering exit");
    String exitURL = Constants.EXIT_URL;
    status.setComplete();  // instead of request.getPortletSession().invalidate()
    try 
        response.sendRedirect(exitURL);
     catch (IOException ex) 
        logger.error("Error: IOException: ", ex.getMessage());
        throw new RuntimeException(ex);
    

    logger.debug("Exiting exit");

我认为这不会产生影响,但值得一试。

【讨论】:

我不确定这些信息是否有帮助,但我认为值得提供这些信息: 1. 我创建了会话范围的 bean,并使用代理创建 () 2. 使会话失效的方法是从默认带有actionmapping注解的方法中调用的。【参考方案2】:

我从未使用过 WebLogic,但从 JavaDoc here 我可以看出,如果您想使现有会话无效,那么调用 getPortletSession() 已经是错误的想法,因为它在 JavaDocs 中说过:

Returns the current portlet session or, if there is no current session,
creates one and returns the new session.

因此,它会创建一个新会话,如果您只想使现有会话无效,这不是您想要的。

你需要的方法是getPortletSession(boolean create),调用的时候需要传入false,如果调用的方法没有可用的session,可能会返回null值,所以你也需要处理null:

PortletSession current = request.getPortletSession();
if (current != null) try 
    current.invalidate();
 catch (IllegalStateException ignored) 
    // ok: session is already invalidated

我们需要捕获这个异常,因为从我看到的in JavaDocs 来看,无法确定会话是否已经失效。所以无论如何我们只是无效,如果 IllegalStateException 被抛出,则忽略它,因为会话已经无效。

另外,不要忘记在使会话无效之前调用response.sendRedirect(exitURL);,我的直觉告诉我应该在此之前完成。

【讨论】:

这里的response.sendRedirect(exitURL)有什么用? 来自主题启动器的代码 sn-p。他需要发送重定向响应。您可以在 JavaDoc 中阅读更多相关信息:portals.apache.org/pluto/portlet-2.0-apidocs/javax/portlet/…【参考方案3】:

无论如何,如果你想这样做,你可以做到。你有一个解决方案。 确保在请求线程内运行此方法,spring 为每个请求分配一个线程。如果您在请求之外,您将无法从请求中得到任何东西。如果您使用过@Async,或者如果您创建了own threadthread-pool 来执行此方法,则无法并行执行。我认为应该是您面临的问题。解决方案是您可以将自己的类创建为Cloneable 并克隆它。要了解更多信息,请查看此答案。

https://***.com/a/68937755/12553450

【讨论】:

以上是关于java.lang.IllegalStateException:getAttribute:会话已经失效的主要内容,如果未能解决你的问题,请参考以下文章