【问题标题】:PageExpiredException, migration to wicket 1.5 not workingPageExpiredException,迁移到 wicket 1.5 不起作用
【发布时间】:2023-03-10 19:17:01
【问题描述】:

当我的用户会话超时时,我正在尝试为特定的错误页面提供服务。 为此,我在我的应用程序的 init 方法上配置了错误页面。 但这东西行不通。

我在 1 分钟内设置了会话 tiemout,之后什么也没发生,我查看了日志,但 wicket 没有抛出任何 PageExpiredException。

当会话超时时检票口简单地将其记录为: 会话未绑定:C061F4F21C41EDF13C66795DAC9EDD02 删除会话中 ID 为“C061F4F21C41EDF13C66795DAC9EDD02”的页面的数据

这是我的 customApplication 中的 init 方法

protected void init() {
    super.init();

    this.getApplicationSettings().setPageExpiredErrorPage(SessionExpiredPage.class);

    ...
    ...
}

我的 SessionExpiredPage.class

public class SessionExpiredPage extends TecnoAccionPage {

    public SessionExpiredPage() {
        this.setOutputMarkupId(true);
        this.add(new Label("title", "Sesión Expirada"));
        CSSLoader.get().appendCssUntil(this, SessionExpiredPage.class);
    }
}

我有一个 AbstractRequestCycleListener 的自定义实现,我覆盖了 OnException 方法但是,当我的会话到期时,我从不传入“onException”。

谢谢你,最好的问候。

【问题讨论】:

    标签: java exception-handling migration wicket wicket-1.5


    【解决方案1】:

    由于某种原因,wicket 没有抛出PageExpiredException,但它可以重建请求的页面,即使会话已过期。

    所以,有另一种方法来处理这个问题。 您必须在 AbstractRequestCycleListener 中覆盖 onRequestHandlerResolved 方法,以捕获所有传入请求,并检查传入的会话 ID 是否已过时。

    要检查这一点,您必须在应用中列出过期会话并捕获未绑定事件来管理它们。

    这将是这样的:

    public class YourApp extends WebApplication {
    
        //synchronized list with ids
        private List<String> unboundSessions = new CopyOnWriteArrayList<String>();
    
        @Override
        protected void init() {
            super.init();
    
            this.getApplicationSettings().setPageExpiredErrorPage(SessionExpiredPage.class);
    
            //add request listener
            getRequestCycleListeners().add(new AbstractRequestCycleListener() {
                public void onRequestHandlerResolved(RequestCycle cycle, IRequestHandler handler) {
                    if (handler instanceof IPageRequestHandler) {
                        HttpServletRequest request = (HttpServletRequest) cycle.getRequest().getContainerRequest();
    
                        String sessionId = request.getRequestedSessionId();
                        //check whether the requested session has expired
                        boolean expired = sessionId != null && !request.isRequestedSessionIdValid();
    
                        //if session is not valid and it was really expired
                        if (expired && unboundSessions.contains(sessionId)) {
                            //then remove it from unbound list
                            unboundSessions.remove(sessionId);
                            //and throw exception
                            throw new PageExpiredException("Expired");
                        }
                    }
                    super.onRequestHandlerResolved(cycle, handler);
                }
            });
            ...
        }
    
        //this method called when any session is invalidated, so check your manual invalidating calls (if you ever do them)
        @Override
        public void sessionUnbound(String sessionId) {
            super.sessionUnbound(sessionId);
            if (!unboundSessions.contains(sessionId)) {
                unboundSessions.add(sessionId);
            }
        }
    }
    

    未绑定会话列表需要我们知道,该用户的会话确实已过期,因为例如当用户在重新部署后刚刚打开我们的站点时,我们侦听器中的expired 变量也可能是true。他的会话取自他的 cookie,它可能已经过期,但是立即将他重定向到 SessionExpiredPage 会很奇怪。

    这看起来像是一种解决方法,但它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2013-02-21
      • 1970-01-01
      相关资源
      最近更新 更多