Javalin: Persistent Sessions

Created on 23 Aug 2018  路  8Comments  路  Source: tipsy/javalin

Is there any way to work with persistent sessions?
I mean that when a user logged in, I use ctx.request().getSession() methods and when the server is restarted all this atributes are gone and the users have to logged in again. Right now, my little web is under development so I restarted many times a day and I'm wondering if this is posible with Javalin, due to Jetty supports this
Thanks alot!

FEATURE REQUEST

Most helpful comment

This has been released as part of 2.1.0: https://javalin.io/news/2018/08/27/javalin-2.1.0-released.html

All 8 comments

You can create a custom Jetty server and tell Javalin to use it by calling app.server(myServer). Let me know if this works for you.

Edit: Just had a look at this, it requires some work.

@Cadiducho, I've added the option to configure the session handler: https://github.com/tipsy/javalin/commit/9a785392ec2df3064812bf38f57c33a4d9e32646

Man, you're awesome, thank you for this.

If either of you know how to setup redis/memcache/mongo etc with embedded jetty I'd appreciate an example.

Here's an example for MongoDB:

```
fun mongoSessionHandler() = SessionHandler().apply {
sessionCache = DefaultSessionCache(this).apply {
sessionDataStore = MongoSessionDataStoreFactory().apply {
connectionString = "mongodb://:@:/session_test"
dbName = "session_test"
collectionName = "sessions"
}.getSessionDataStore(sessionHandler)
}
}

Similar methods can be used with JDBC to use with sql database

fun sqlSessionHandler() = SessionHandler().apply {
    sessionCache = DefaultSessionCache(this).apply {
        sessionDataStore = JDBCSessionDataStoreFactory().apply {
            databaseAdaptor = DatabaseAdaptor().let {
                 it.setDriverInfo("com.mysql.jdbc.Driver", 
                  "jdbc:mysql://<adr>:<port>/<db>?user=<user>&password=<pass>")
            }
        }.getSessionDataStore(sessionHandler)
    }
}

Edit: I'm not sure if this code is 100% correct in kotlin :sweat_smile:

@Cadiducho Pretty close at least. Does this cover your session-needs though?

This has been released as part of 2.1.0: https://javalin.io/news/2018/08/27/javalin-2.1.0-released.html

Was this page helpful?
0 / 5 - 0 ratings