简体   繁体   中英

session handling in play framework

I have a web application build on Play framework and Scala. This application is connected to Cassandra db which has many key spaces. i have different users, each user belongs to a different unit(we can say each unit as a keyspace). let say i have usernames as retail and manufacture, if i login with username as retail it connects to retail keyspace and gives me the data from the table that belongs to retail keyspace and same if i login with username as manufacture it gives me the data from the table that belongs to manufacture key space.

Now the problem is if i login from chrome using retail it gives me the data from retail keyspace and if i open firefox and login using manufacture it connects to manufacture keyspace and retrieves data of that keyspace, now if i go back to the chrome where i have logged in with retail username and perform any action it retrieves data from manufacture keyspace instead of retail keyspace. The application has switched the keyspace, it always functions on the latest logged in username.How do i overcome this.

Sounds like you are using a singleton (a global variable) to store the one and only keyspace shared by all users, or at least one keyspace per user. What you want instead is to give each user session its own keyspace. There are several ways to save and restore user keyspace information per session:

  • Via a cookie
  • Via a Java Web Token (JWT)
  • Via a HashMap singleton, which has entries for each user session. You'll want a soft HashMap for this, so old entries self-purge; Google Guava has a good implementation.

The Play Framework documentation has information on this topic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM