aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjcardus <asklocation.net@gmail.com>2021-07-06 03:57:40 +0100
committerjcardus <asklocation.net@gmail.com>2021-07-06 03:57:40 +0100
commit20f3d5f01fdc1088f2069869c61dd9da525a7c22 (patch)
treee5cbac7a472d4ab5b285d59f2d3db7499ef97a0b
parent979e44b4d4fc77785a2672e3b795fb806c067f47 (diff)
downloadtrackermap-server-20f3d5f01fdc1088f2069869c61dd9da525a7c22.tar.gz
trackermap-server-20f3d5f01fdc1088f2069869c61dd9da525a7c22.tar.bz2
trackermap-server-20f3d5f01fdc1088f2069869c61dd9da525a7c22.zip
Persist Jetty Session in DB
-rw-r--r--src/main/java/org/traccar/config/Keys.java7
-rw-r--r--src/main/java/org/traccar/database/DataManager.java4
-rw-r--r--src/main/java/org/traccar/web/WebServer.java20
3 files changed, 30 insertions, 1 deletions
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
@@ -1199,6 +1199,13 @@ public final class Keys {
Collections.singletonList(KeyType.GLOBAL));
/**
+ * Enables persisting Jetty session to the database
+ */
+ public static final ConfigKey<Boolean> WEB_SESSION_PERSISTED = new ConfigKey<>(
+ "web.sessionPersisted",
+ Collections.singletonList(KeyType.GLOBAL));
+
+ /**
* Output logging to the standard terminal output instead of a log file.
*/
public static final ConfigKey<Boolean> LOGGER_CONSOLE = new ConfigKey<>(
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);