I have built a simple login system for my JSF/Primefaces site using a sessionMap. In my main view, ParliamentManager which is a SessionScoped bean I have:
@PostConstruct
public void postInit() {
logger.log(Level.INFO,"Session started");
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Map<String, Object> sessionMap = externalContext.getSessionMap();
user = (User) sessionMap.get("user");
logger.log(Level.INFO, "user trader is: {0}",
new Object[]{user.getTrader().getTraderId().toString()});
userId = user.getUserId();
In my login view which is a RequestScoped ManagedBean I have:
public void login(ActionEvent event) {
RequestContext requestContext = RequestContext.getCurrentInstance();
FacesContext context = FacesContext.getCurrentInstance();
boolean loggedIn = false;
User user = request.findUser(username);
if(user.getPassword().equals(this.getPassword()))
{
loggedIn = true;
context.getExternalContext().invalidateSession();
context.getExternalContext().getSession(true);
context.getExternalContext().getSessionMap().put("user", user);
logger.log(Level.INFO, "put user {0}",
new Object[]{user.getUserId()});
logger.log(Level.INFO, "session id {0}",
new Object[]{context.getExternalContext().getSessionId(false)});
requestContext.addCallbackParam("loggedIn", loggedIn);
}
}
In my login page I simply redirect to the main index page:
<script type="text/javascript">
function handleLoginRequest(xhr, status, args) {
if(args.validationFailed || !args.loggedIn) {
PF('dlg').jq.effect("shake", {times:5}, 100);
}
else {
PF('dlg').hide();
$('#loginLink').fadeOut();
window.location = "http://localhost:8080/mysite/index.xhtml"
}
}
This works ok if I have two users logged in (one in a browser session, another in an incognito session). However if I add a third user (another invognito session) I get a ViewExpiredException when submitting my ajax request within a form. I thought that I could have a large amount of sessions as I'd expect many users so I'm not clear on why I get the exception in these cirumstances. Am I associating the user and session incorrectly or could there be some other issue causing the exception?
Thanks, Zobbo