一种方法是定期检查服务器是否发生超时。您将不得不编写一个servlet方法来执行该检查,而不会更新服务器会话超时!当然这会导致很多服务器命中。 (不一定是坏的方法虽然!)
但是,也许我会用一个dfifferent解决方案,它试图保持一个计时器在客户端与服务器的定时器,例如大约同步
客户端:
import com.google.gwt.user.client.Timer;
public class ClientTimers {
private static final Timer SESSION_MAY_HAVE_EXPIRED_TIMER = new Timer() {
@Override
public void run() {
// Warn the user, that the session may have expired.
// You could then show a login dialog, etc...
}
};
public static void renewSessionTimer() {
// First cancel the previous timer
SESSION_MAY_HAVE_EXPIRED_TIMER.cancel();
// Schedule again in 5 minutes (maybe make that configurable?)
// Actually, let's subtract 10 seconds from that, because our timer
// won't be synchronized perfectly with the server's timer.
SESSION_MAY_HAVE_EXPIRED_TIMER.schedule(5 * 60 * 1000 - 10000);
}
}
我假设你的服务器会话超时被更新的每一个客户端执行与服务器,例如交互时间一个GWT-RPC调用(如果该会话还没有超时)。
所以在客户端,然后,我会还更新客户端计时器,以保持它的大致同步:
myService.performSomeAction(...) {
@Override
public void onSuccess(String result) {
ClientTimers.renewSessionTimer();
// remaining onSuccess handling
}
@Override
public void onFailure(Throwable caught) {
if (failedBecauseOfSessionTimeout()) {
// redirect to login
} else {
ClientTimers.renewSessionTimer();
// remaining onFailure handling...
}
}
}
不要忘记尤其是直接调用后renewSessionTimer()上的所有交互(登录)。
重要说明:对于所有安全检查,仅使用服务器会话。客户端“会话计时器”仅为用户提供便利。不要根据该定时器或任何类型的客户端会话进行安全/授权检查。