From 979e44b4d4fc77785a2672e3b795fb806c067f47 Mon Sep 17 00:00:00 2001 From: jcardus Date: Mon, 5 Jul 2021 19:33:14 -0400 Subject: Update WebServer.java --- src/main/java/org/traccar/web/WebServer.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/traccar/web/WebServer.java b/src/main/java/org/traccar/web/WebServer.java index 04c320839..441ec009c 100644 --- a/src/main/java/org/traccar/web/WebServer.java +++ b/src/main/java/org/traccar/web/WebServer.java @@ -175,6 +175,7 @@ public class WebServer { int sessionTimeout = config.getInteger(Keys.WEB_SESSION_TIMEOUT); if (sessionTimeout > 0) { servletHandler.getSessionHandler().setMaxInactiveInterval(sessionTimeout); + } String sameSiteCookie = config.getString(Keys.WEB_SAME_SITE_COOKIE); -- cgit v1.2.3 From 20f3d5f01fdc1088f2069869c61dd9da525a7c22 Mon Sep 17 00:00:00 2001 From: jcardus Date: Tue, 6 Jul 2021 03:57:40 +0100 Subject: Persist Jetty Session in DB --- src/main/java/org/traccar/config/Keys.java | 7 +++++++ src/main/java/org/traccar/database/DataManager.java | 4 ++++ src/main/java/org/traccar/web/WebServer.java | 20 +++++++++++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 167627b1d..c542b7099 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -1198,6 +1198,13 @@ public final class Keys { "web.sameSiteCookie", Collections.singletonList(KeyType.GLOBAL)); + /** + * Enables persisting Jetty session to the database + */ + public static final ConfigKey WEB_SESSION_PERSISTED = new ConfigKey<>( + "web.sessionPersisted", + Collections.singletonList(KeyType.GLOBAL)); + /** * Output logging to the standard terminal output instead of a log file. */ diff --git a/src/main/java/org/traccar/database/DataManager.java b/src/main/java/org/traccar/database/DataManager.java index 15137ad91..de6da8f23 100644 --- a/src/main/java/org/traccar/database/DataManager.java +++ b/src/main/java/org/traccar/database/DataManager.java @@ -73,6 +73,10 @@ public class DataManager { private DataSource dataSource; + public DataSource getDataSource() { + return dataSource; + } + private boolean generateQueries; private final boolean forceLdap; diff --git a/src/main/java/org/traccar/web/WebServer.java b/src/main/java/org/traccar/web/WebServer.java index 441ec009c..354e12695 100644 --- a/src/main/java/org/traccar/web/WebServer.java +++ b/src/main/java/org/traccar/web/WebServer.java @@ -26,6 +26,11 @@ import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.gzip.GzipHandler; +import org.eclipse.jetty.server.session.DatabaseAdaptor; +import org.eclipse.jetty.server.session.DefaultSessionCache; +import org.eclipse.jetty.server.session.JDBCSessionDataStoreFactory; +import org.eclipse.jetty.server.session.SessionCache; +import org.eclipse.jetty.server.session.SessionHandler; import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; @@ -35,6 +40,7 @@ import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.servlet.ServletContainer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.traccar.Context; import org.traccar.api.DateParameterConverterProvider; import org.traccar.config.Config; import org.traccar.api.AsyncSocketServlet; @@ -172,10 +178,22 @@ public class WebServer { } private void initSessionConfig(Config config, ServletContextHandler servletHandler) { + boolean sessionPersisted = config.getBoolean(Keys.WEB_SESSION_PERSISTED); + if (sessionPersisted) { + DatabaseAdaptor databaseAdaptor = new DatabaseAdaptor(); + databaseAdaptor.setDatasource(Context.getDataManager().getDataSource()); + JDBCSessionDataStoreFactory jdbcSessionDataStoreFactory = new JDBCSessionDataStoreFactory(); + jdbcSessionDataStoreFactory.setDatabaseAdaptor(databaseAdaptor); + SessionHandler sessionHandler = new SessionHandler(); + SessionCache sessionCache = new DefaultSessionCache(sessionHandler); + sessionCache.setSessionDataStore(jdbcSessionDataStoreFactory.getSessionDataStore(sessionHandler)); + sessionHandler.setSessionCache(sessionCache); + servletHandler.setSessionHandler(sessionHandler); + } + int sessionTimeout = config.getInteger(Keys.WEB_SESSION_TIMEOUT); if (sessionTimeout > 0) { servletHandler.getSessionHandler().setMaxInactiveInterval(sessionTimeout); - } String sameSiteCookie = config.getString(Keys.WEB_SAME_SITE_COOKIE); -- cgit v1.2.3 From cd6b59c61f1c9319fe09b0ab1b8b3e39f5a1df62 Mon Sep 17 00:00:00 2001 From: jcardus Date: Tue, 6 Jul 2021 18:29:36 +0100 Subject: use default session handler --- src/main/java/org/traccar/web/WebServer.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/traccar/web/WebServer.java b/src/main/java/org/traccar/web/WebServer.java index 354e12695..1f6af8b2d 100644 --- a/src/main/java/org/traccar/web/WebServer.java +++ b/src/main/java/org/traccar/web/WebServer.java @@ -26,11 +26,7 @@ import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.gzip.GzipHandler; -import org.eclipse.jetty.server.session.DatabaseAdaptor; -import org.eclipse.jetty.server.session.DefaultSessionCache; -import org.eclipse.jetty.server.session.JDBCSessionDataStoreFactory; -import org.eclipse.jetty.server.session.SessionCache; -import org.eclipse.jetty.server.session.SessionHandler; +import org.eclipse.jetty.server.session.*; import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; @@ -184,11 +180,10 @@ public class WebServer { databaseAdaptor.setDatasource(Context.getDataManager().getDataSource()); JDBCSessionDataStoreFactory jdbcSessionDataStoreFactory = new JDBCSessionDataStoreFactory(); jdbcSessionDataStoreFactory.setDatabaseAdaptor(databaseAdaptor); - SessionHandler sessionHandler = new SessionHandler(); + SessionHandler sessionHandler = servletHandler.getSessionHandler(); SessionCache sessionCache = new DefaultSessionCache(sessionHandler); sessionCache.setSessionDataStore(jdbcSessionDataStoreFactory.getSessionDataStore(sessionHandler)); sessionHandler.setSessionCache(sessionCache); - servletHandler.setSessionHandler(sessionHandler); } int sessionTimeout = config.getInteger(Keys.WEB_SESSION_TIMEOUT); -- cgit v1.2.3 From 41857253c39a9c207b8288b02e09317d1c0f5628 Mon Sep 17 00:00:00 2001 From: jcardus Date: Tue, 6 Jul 2021 18:31:58 +0100 Subject: use single class imports --- src/main/java/org/traccar/web/WebServer.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/traccar/web/WebServer.java b/src/main/java/org/traccar/web/WebServer.java index 1f6af8b2d..01ccf80bc 100644 --- a/src/main/java/org/traccar/web/WebServer.java +++ b/src/main/java/org/traccar/web/WebServer.java @@ -26,7 +26,11 @@ import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ErrorHandler; import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.gzip.GzipHandler; -import org.eclipse.jetty.server.session.*; +import org.eclipse.jetty.server.session.DatabaseAdaptor; +import org.eclipse.jetty.server.session.DefaultSessionCache; +import org.eclipse.jetty.server.session.JDBCSessionDataStoreFactory; +import org.eclipse.jetty.server.session.SessionCache; +import org.eclipse.jetty.server.session.SessionHandler; import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; -- cgit v1.2.3 From c82a19b1943f8784d5c10ece50ffab06d0596020 Mon Sep 17 00:00:00 2001 From: jcardus Date: Tue, 6 Jul 2021 18:45:59 +0100 Subject: inline if --- src/main/java/org/traccar/web/WebServer.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/traccar/web/WebServer.java b/src/main/java/org/traccar/web/WebServer.java index 01ccf80bc..0157c075c 100644 --- a/src/main/java/org/traccar/web/WebServer.java +++ b/src/main/java/org/traccar/web/WebServer.java @@ -178,8 +178,7 @@ public class WebServer { } private void initSessionConfig(Config config, ServletContextHandler servletHandler) { - boolean sessionPersisted = config.getBoolean(Keys.WEB_SESSION_PERSISTED); - if (sessionPersisted) { + if (config.getBoolean(Keys.WEB_SESSION_PERSISTED)) { DatabaseAdaptor databaseAdaptor = new DatabaseAdaptor(); databaseAdaptor.setDatasource(Context.getDataManager().getDataSource()); JDBCSessionDataStoreFactory jdbcSessionDataStoreFactory = new JDBCSessionDataStoreFactory(); -- cgit v1.2.3 From 1ec2196ddcc778cb3d4ef220a18f97165f4a175a Mon Sep 17 00:00:00 2001 From: jcardus Date: Tue, 6 Jul 2021 21:54:48 +0100 Subject: I wasn't ve happy with the name myself... --- src/main/java/org/traccar/config/Keys.java | 4 ++-- src/main/java/org/traccar/web/WebServer.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index c542b7099..ef0edbac3 100644 --- a/src/main/java/org/traccar/config/Keys.java +++ b/src/main/java/org/traccar/config/Keys.java @@ -1201,8 +1201,8 @@ public final class Keys { /** * Enables persisting Jetty session to the database */ - public static final ConfigKey WEB_SESSION_PERSISTED = new ConfigKey<>( - "web.sessionPersisted", + public static final ConfigKey WEB_PERSIST_SESSION = new ConfigKey<>( + "web.persistSession", Collections.singletonList(KeyType.GLOBAL)); /** diff --git a/src/main/java/org/traccar/web/WebServer.java b/src/main/java/org/traccar/web/WebServer.java index 0157c075c..604edfedc 100644 --- a/src/main/java/org/traccar/web/WebServer.java +++ b/src/main/java/org/traccar/web/WebServer.java @@ -178,7 +178,7 @@ public class WebServer { } private void initSessionConfig(Config config, ServletContextHandler servletHandler) { - if (config.getBoolean(Keys.WEB_SESSION_PERSISTED)) { + if (config.getBoolean(Keys.WEB_PERSIST_SESSION)) { DatabaseAdaptor databaseAdaptor = new DatabaseAdaptor(); databaseAdaptor.setDatasource(Context.getDataManager().getDataSource()); JDBCSessionDataStoreFactory jdbcSessionDataStoreFactory = new JDBCSessionDataStoreFactory(); -- cgit v1.2.3