mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
store sessions in database
This commit is contained in:
@@ -4,7 +4,6 @@ EXPOSE 8082
|
|||||||
|
|
||||||
RUN mkdir -p /commafeed/data
|
RUN mkdir -p /commafeed/data
|
||||||
VOLUME /commafeed/data
|
VOLUME /commafeed/data
|
||||||
ENV CF_SESSION_PATH=/commafeed/data/sessions
|
|
||||||
ENV CF_DATABASE_URL=jdbc:h2:/commafeed/data/db
|
ENV CF_DATABASE_URL=jdbc:h2:/commafeed/data/db
|
||||||
|
|
||||||
COPY commafeed-server/config.yml.example config.yml
|
COPY commafeed-server/config.yml.example config.yml
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ public class CommaFeedApplication extends Application<CommaFeedConfiguration> {
|
|||||||
Injector injector = Guice.createInjector(new CommaFeedModule(hibernateBundle.getSessionFactory(), config, environment.metrics()));
|
Injector injector = Guice.createInjector(new CommaFeedModule(hibernateBundle.getSessionFactory(), config, environment.metrics()));
|
||||||
|
|
||||||
// session management
|
// session management
|
||||||
environment.servlets().setSessionHandler(config.getSessionHandlerFactory().build());
|
environment.servlets().setSessionHandler(config.getSessionHandlerFactory().build(config.getDataSourceFactory()));
|
||||||
|
|
||||||
// support for "@SecurityCheck User user" injection
|
// support for "@SecurityCheck User user" injection
|
||||||
environment.jersey().register(new SecurityCheckFactoryProvider.Binder(injector.getInstance(UserService.class)));
|
environment.jersey().register(new SecurityCheckFactoryProvider.Binder(injector.getInstance(UserService.class)));
|
||||||
|
|||||||
@@ -1,24 +1,22 @@
|
|||||||
package com.commafeed.frontend.session;
|
package com.commafeed.frontend.session;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
import javax.servlet.SessionTrackingMode;
|
import javax.servlet.SessionTrackingMode;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.server.session.DatabaseAdaptor;
|
||||||
import org.eclipse.jetty.server.session.DefaultSessionCache;
|
import org.eclipse.jetty.server.session.DefaultSessionCache;
|
||||||
import org.eclipse.jetty.server.session.FileSessionDataStore;
|
import org.eclipse.jetty.server.session.JDBCSessionDataStore;
|
||||||
import org.eclipse.jetty.server.session.SessionCache;
|
import org.eclipse.jetty.server.session.SessionCache;
|
||||||
import org.eclipse.jetty.server.session.SessionHandler;
|
import org.eclipse.jetty.server.session.SessionHandler;
|
||||||
|
|
||||||
|
import com.codahale.metrics.MetricRegistry;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.google.common.collect.ImmutableSet;
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
|
||||||
|
import io.dropwizard.db.DataSourceFactory;
|
||||||
import io.dropwizard.util.Duration;
|
import io.dropwizard.util.Duration;
|
||||||
|
|
||||||
public class SessionHandlerFactory {
|
public class SessionHandlerFactory {
|
||||||
|
|
||||||
@JsonProperty
|
|
||||||
private String path = "sessions";
|
|
||||||
|
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
private Duration cookieMaxAge = Duration.days(30);
|
private Duration cookieMaxAge = Duration.days(30);
|
||||||
|
|
||||||
@@ -31,26 +29,24 @@ public class SessionHandlerFactory {
|
|||||||
@JsonProperty
|
@JsonProperty
|
||||||
private Duration savePeriod = Duration.minutes(5);
|
private Duration savePeriod = Duration.minutes(5);
|
||||||
|
|
||||||
public SessionHandler build() {
|
public SessionHandler build(DataSourceFactory dataSourceFactory) {
|
||||||
SessionHandler sessionHandler = new SessionHandler() {
|
SessionHandler sessionHandler = new SessionHandler();
|
||||||
{
|
|
||||||
// no setter available for maxCookieAge
|
|
||||||
_maxCookieAge = (int) cookieMaxAge.toSeconds();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
SessionCache sessionCache = new DefaultSessionCache(sessionHandler);
|
|
||||||
sessionHandler.setSessionCache(sessionCache);
|
|
||||||
FileSessionDataStore dataStore = new FileSessionDataStore();
|
|
||||||
sessionCache.setSessionDataStore(dataStore);
|
|
||||||
|
|
||||||
sessionHandler.setHttpOnly(true);
|
sessionHandler.setHttpOnly(true);
|
||||||
sessionHandler.setSessionTrackingModes(ImmutableSet.of(SessionTrackingMode.COOKIE));
|
sessionHandler.setSessionTrackingModes(ImmutableSet.of(SessionTrackingMode.COOKIE));
|
||||||
sessionHandler.setMaxInactiveInterval((int) maxInactiveInterval.toSeconds());
|
sessionHandler.setMaxInactiveInterval((int) maxInactiveInterval.toSeconds());
|
||||||
sessionHandler.setRefreshCookieAge((int) cookieRefreshAge.toSeconds());
|
sessionHandler.setRefreshCookieAge((int) cookieRefreshAge.toSeconds());
|
||||||
|
sessionHandler.getSessionCookieConfig().setMaxAge((int) cookieMaxAge.toSeconds());
|
||||||
|
|
||||||
dataStore.setDeleteUnrestorableFiles(true);
|
SessionCache sessionCache = new DefaultSessionCache(sessionHandler);
|
||||||
dataStore.setStoreDir(new File(path));
|
sessionHandler.setSessionCache(sessionCache);
|
||||||
|
|
||||||
|
JDBCSessionDataStore dataStore = new JDBCSessionDataStore();
|
||||||
dataStore.setSavePeriodSec((int) savePeriod.toSeconds());
|
dataStore.setSavePeriodSec((int) savePeriod.toSeconds());
|
||||||
|
sessionCache.setSessionDataStore(dataStore);
|
||||||
|
|
||||||
|
DatabaseAdaptor adaptor = new DatabaseAdaptor();
|
||||||
|
adaptor.setDatasource(dataSourceFactory.build(new MetricRegistry(), "sessions"));
|
||||||
|
dataStore.setDatabaseAdaptor(adaptor);
|
||||||
|
|
||||||
return sessionHandler;
|
return sessionHandler;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user