Search in sources :

Example 1 with FileSessionDataStore

use of org.eclipse.jetty.server.session.FileSessionDataStore in project jetty.project by eclipse.

the class TestServer method main.

public static void main(String[] args) throws Exception {
    ((StdErrLog) Log.getLog()).setSource(false);
    String jetty_root = "../../..";
    // Setup Threadpool
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setMaxThreads(100);
    // Setup server
    Server server = new Server(threadPool);
    server.manage(threadPool);
    // Setup JMX
    MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    server.addBean(mbContainer);
    server.addBean(Log.getLog());
    // Common HTTP configuration
    HttpConfiguration config = new HttpConfiguration();
    config.setSecurePort(8443);
    config.addCustomizer(new ForwardedRequestCustomizer());
    config.addCustomizer(new SecureRequestCustomizer());
    config.setSendDateHeader(true);
    config.setSendServerVersion(true);
    // Http Connector
    HttpConnectionFactory http = new HttpConnectionFactory(config);
    ServerConnector httpConnector = new ServerConnector(server, http);
    httpConnector.setPort(8080);
    httpConnector.setIdleTimeout(30000);
    server.addConnector(httpConnector);
    // Handlers
    HandlerCollection handlers = new HandlerCollection();
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
    // Add restart handler to test the ability to save sessions and restart
    RestartHandler restart = new RestartHandler();
    restart.setHandler(handlers);
    server.setHandler(restart);
    // Setup context
    HashLoginService login = new HashLoginService();
    login.setName("Test Realm");
    login.setConfig(jetty_root + "/tests/test-webapps/test-jetty-webapp/src/main/config/demo-base/etc/realm.properties");
    server.addBean(login);
    File log = File.createTempFile("jetty-yyyy_mm_dd", "log");
    NCSARequestLog requestLog = new NCSARequestLog(log.toString());
    requestLog.setExtended(false);
    requestLogHandler.setRequestLog(requestLog);
    server.setStopAtShutdown(true);
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/test");
    webapp.setParentLoaderPriority(true);
    webapp.setResourceBase("./src/main/webapp");
    webapp.setAttribute("testAttribute", "testValue");
    File sessiondir = File.createTempFile("sessions", null);
    if (sessiondir.exists())
        sessiondir.delete();
    sessiondir.mkdir();
    sessiondir.deleteOnExit();
    DefaultSessionCache ss = new DefaultSessionCache(webapp.getSessionHandler());
    FileSessionDataStore sds = new FileSessionDataStore();
    ss.setSessionDataStore(sds);
    sds.setStoreDir(sessiondir);
    webapp.getSessionHandler().setSessionCache(ss);
    contexts.addHandler(webapp);
    ContextHandler srcroot = new ContextHandler();
    srcroot.setResourceBase(".");
    srcroot.setHandler(new ResourceHandler());
    srcroot.setContextPath("/src");
    contexts.addHandler(srcroot);
    server.start();
    server.join();
}
Also used : DefaultSessionCache(org.eclipse.jetty.server.session.DefaultSessionCache) StdErrLog(org.eclipse.jetty.util.log.StdErrLog) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ForwardedRequestCustomizer(org.eclipse.jetty.server.ForwardedRequestCustomizer) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) HashLoginService(org.eclipse.jetty.security.HashLoginService) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) NCSARequestLog(org.eclipse.jetty.server.NCSARequestLog) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) FileSessionDataStore(org.eclipse.jetty.server.session.FileSessionDataStore) File(java.io.File)

Example 2 with FileSessionDataStore

use of org.eclipse.jetty.server.session.FileSessionDataStore in project spring-boot by spring-projects.

the class JettyServletWebServerFactory method configureSession.

private void configureSession(WebAppContext context) {
    SessionHandler handler = context.getSessionHandler();
    SameSite sessionSameSite = getSession().getCookie().getSameSite();
    if (sessionSameSite != null) {
        handler.setSameSite(HttpCookie.SameSite.valueOf(sessionSameSite.name()));
    }
    Duration sessionTimeout = getSession().getTimeout();
    handler.setMaxInactiveInterval(isNegative(sessionTimeout) ? -1 : (int) sessionTimeout.getSeconds());
    if (getSession().isPersistent()) {
        DefaultSessionCache cache = new DefaultSessionCache(handler);
        FileSessionDataStore store = new FileSessionDataStore();
        store.setStoreDir(getValidSessionStoreDir());
        cache.setSessionDataStore(store);
        handler.setSessionCache(cache);
    }
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) DefaultSessionCache(org.eclipse.jetty.server.session.DefaultSessionCache) SameSite(org.springframework.boot.web.server.Cookie.SameSite) Duration(java.time.Duration) FileSessionDataStore(org.eclipse.jetty.server.session.FileSessionDataStore)

Aggregations

DefaultSessionCache (org.eclipse.jetty.server.session.DefaultSessionCache)2 FileSessionDataStore (org.eclipse.jetty.server.session.FileSessionDataStore)2 File (java.io.File)1 Duration (java.time.Duration)1 MBeanContainer (org.eclipse.jetty.jmx.MBeanContainer)1 HashLoginService (org.eclipse.jetty.security.HashLoginService)1 ForwardedRequestCustomizer (org.eclipse.jetty.server.ForwardedRequestCustomizer)1 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)1 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)1 NCSARequestLog (org.eclipse.jetty.server.NCSARequestLog)1 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)1 Server (org.eclipse.jetty.server.Server)1 ServerConnector (org.eclipse.jetty.server.ServerConnector)1 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)1 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)1 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)1 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)1 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)1 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)1 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)1