Ok, I will try that. Thanks for your answer.
In the meantime, I had experimented a little and I found that I could
use a cookie to store the current user/encoded password combination
and, if the failure case occurs, I "re-authenticate" the user from the
information in the cookie and bind the user to the new session. This
works -- I don't really understand, why the problem occured in the
first place, but at least I do have a workaround now. For those
interested, here is the relevant snippet:
Call this, whenever a user authenticates (or in a more general case:
Whenever you assign data to a session):
private final void updateCookies(String login, String hash) {
Cookie cLogin = new Cookie("userName", login);
Cookie cPwd = new Cookie("userPassword", hash);
int maxAge;
String path = getThreadLocalRequest().getContextPath();
maxAge = 60 * 60 * 24 * 30; // 1 month
cLogin.setMaxAge(maxAge);
cPwd.setMaxAge(maxAge);
cLogin.setPath(path);
cPwd.setPath(path);
getThreadLocalResponse().addCookie(cLogin);
getThreadLocalResponse().addCookie(cPwd);
}
To read the cookie-data:
protected AuthUser getAuthUserFromCookies () {
Cookie[] cookies = getThreadLocalRequest().getCookies();
String loginName = null;
String password = null;
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
String name = cookie.getName();
String value = cookie.getValue();
if (name.equalsIgnoreCase("userName")) {
loginName = value;
} else if (name.equalsIgnoreCase("userPassword")) {
password = value;
}
}
}
System.err.println("Examining cookie: " + loginName + ", " +
password);
// Snip: The following omitted code authenticates the user using a
password hash
return successfullyAuthenticatedUser;
}
So now, it's down to the failure case:
public final synchronized UserSession getUserSession() throws
SessionExpiredException {
HttpSession session = getSession();
synchronized (session) {
UserBinding userBinding = (UserBinding)session.getAttribute
(SESSION_USER);
if(userBinding == null) {
System.err.println("Session cookie expired; re-authenticating
user...");
AuthUser user = getAuthUserFromCookies();
if (user != null) {
UserSession userSession = new UserSession(user, session.getId());
bindUserToSession(userSession, session);
// Be sure to update the
cookie here, again; otherwise you'll run into problems
// the next time the session
is different.
updateCookies(user.getLoginName(), user.getPassword());
}
userBinding = (UserBinding)session.getAttribute(SESSION_USER);
if (userBinding == null) {
throw new SessionExpiredException("Session expired!");
}
}
return userBinding.getUserSession();