diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2021-07-06 14:03:00 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-06 14:03:00 -0700 |
commit | 6b4a699fd8098a37358ace28e1c2142321ab67b0 (patch) | |
tree | 6d88b62a990cf64f0cff718514b0e93e921374eb /src/main/java/org | |
parent | 827a781e49c74e6573a407119fd5519050b091e3 (diff) | |
parent | 1ec2196ddcc778cb3d4ef220a18f97165f4a175a (diff) | |
download | trackermap-server-6b4a699fd8098a37358ace28e1c2142321ab67b0.tar.gz trackermap-server-6b4a699fd8098a37358ace28e1c2142321ab67b0.tar.bz2 trackermap-server-6b4a699fd8098a37358ace28e1c2142321ab67b0.zip |
Merge pull request #4712 from jcardus/patch-2
Persist Session in Database
Diffstat (limited to 'src/main/java/org')
-rw-r--r-- | src/main/java/org/traccar/config/Keys.java | 7 | ||||
-rw-r--r-- | src/main/java/org/traccar/database/DataManager.java | 4 | ||||
-rw-r--r-- | src/main/java/org/traccar/web/WebServer.java | 17 |
3 files changed, 28 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/config/Keys.java b/src/main/java/org/traccar/config/Keys.java index 167627b1d..ef0edbac3 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_PERSIST_SESSION = new ConfigKey<>( + "web.persistSession", + 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 04c320839..604edfedc 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,6 +178,17 @@ public class WebServer { } private void initSessionConfig(Config config, ServletContextHandler servletHandler) { + if (config.getBoolean(Keys.WEB_PERSIST_SESSION)) { + DatabaseAdaptor databaseAdaptor = new DatabaseAdaptor(); + databaseAdaptor.setDatasource(Context.getDataManager().getDataSource()); + JDBCSessionDataStoreFactory jdbcSessionDataStoreFactory = new JDBCSessionDataStoreFactory(); + jdbcSessionDataStoreFactory.setDatabaseAdaptor(databaseAdaptor); + SessionHandler sessionHandler = servletHandler.getSessionHandler(); + SessionCache sessionCache = new DefaultSessionCache(sessionHandler); + sessionCache.setSessionDataStore(jdbcSessionDataStoreFactory.getSessionDataStore(sessionHandler)); + sessionHandler.setSessionCache(sessionCache); + } + int sessionTimeout = config.getInteger(Keys.WEB_SESSION_TIMEOUT); if (sessionTimeout > 0) { servletHandler.getSessionHandler().setMaxInactiveInterval(sessionTimeout); |