cometdbayeux

Storing User Credientials in the Bayeux Server


I'd like to store the clients UserName and SessionId when a client subscribes to a particular channel. When i override canHandshake() i can get the user credentials using the following:

userName = (String) authentication.get("userName");
sessionId = (String) authentication.get("sessionId");

Just wondering how i can store these credentials and later retrieve them? I've had a look at the authentication documentation here and it just mentions linking the authentication data to the session. Is this the Bayeux Server side session??

Thanks


Solution

  • The "linking" can be done in several ways.

    You can link this information in an external map via:

    @Override
    public boolean canHandshake(BayeuxServer server, ServerSession session, ServerMessage message)
    {
        ...
        Map<String, Object> authentication = ...;
        map.put((String)authentication.get("userName"), session);
        ...
    }
    

    where the map can be a java.util.ConcurrentHashMap<String, ServerSession> field in the security policy itself, or in another object such as a user service.

    For simpler use cases, the userName can be linked directly to the session in this way:

    session.setAttribute("userName", authentication.get("userName"));
    

    Or you can use both techniques.

    This is the updated link for the authentication how-to, and you can find the latest comprehensive CometD documentation at http://docs.cometd.org.