31

在我的代码中使 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");
}

我在 stackoverflow 中发现了一个与会话失效 IllegalStateException: getAttribute: Session already invalidated相关的问题 ,但在我的情况下我无法从中得到太多。

有人可以帮助我在发生此错误时如何解决此问题,当控件转到 Spring 代码时,因此我无法解决此问题?

4

3 回答 3

1

我从来没有使用过 WebLogic,但是从这里的 JavaDoc中我可以看出,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,如果调用方法没有可用的会话,它可能会返回null值,因此您也需要处理null:

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

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

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

于 2019-05-08T19:16:10.443 回答
1

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

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

于 2021-10-15T12:28:22.507 回答
-2

尝试使用 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");
}

我不认为这会有所作为,但值得一试。

于 2013-08-28T14:47:56.453 回答