Skip to content

Commit

Permalink
Persist Jetty Session in DB
Browse files Browse the repository at this point in the history
  • Loading branch information
jcardus committed Jul 6, 2021
1 parent 979e44b commit 20f3d5f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/main/java/org/traccar/config/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,13 @@ private Keys() {
"web.sameSiteCookie",
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.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/traccar/database/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public class DataManager {

private DataSource dataSource;

public DataSource getDataSource() {
return dataSource;
}

private boolean generateQueries;

private final boolean forceLdap;
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/org/traccar/web/WebServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
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;
Expand All @@ -35,6 +40,7 @@
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;
Expand Down Expand Up @@ -172,10 +178,22 @@ private void initApi(Config config, ServletContextHandler servletHandler) {
}

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);
Expand Down

0 comments on commit 20f3d5f

Please sign in to comment.