Example usage for javax.servlet SessionTrackingMode COOKIE

List of usage examples for javax.servlet SessionTrackingMode COOKIE

Introduction

In this page you can find the example usage for javax.servlet SessionTrackingMode COOKIE.

Prototype

SessionTrackingMode COOKIE

To view the source code for javax.servlet SessionTrackingMode COOKIE.

Click Source Link

Usage

From source file:org.utb.project.ServletInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    WebApplicationContext webApplicationContext = getWebContext();
    servletContext.setSessionTrackingModes(new HashSet<SessionTrackingMode>() {
        {/*from  w  w w  .ja va  2 s.c om*/
            add(SessionTrackingMode.COOKIE);
        }
    });
    servletContext.addListener(new ContextLoaderListener(webApplicationContext));

    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("ProyectoSoftware",
            new DispatcherServlet(webApplicationContext));
    dispatcher.setAsyncSupported(true);
    dispatcher.setLoadOnStartup(0);
    dispatcher.addMapping("/*");
}

From source file:com.thoughtworks.go.server.web.WebappSessionConfigIntegrationTest.java

@Test
public void shouldSetSessionTrackingModeToCookieOnly() throws Exception {
    Server server = new Server(1234);
    WebAppContext webAppContext = new WebAppContext();
    webAppContext.setWar(webapp.getAbsolutePath());
    webAppContext.setContextPath("/");
    server.setHandler(webAppContext);/*  w  ww .ja v a 2s .c  o m*/
    try {
        server.start();
        Set<SessionTrackingMode> effectiveSessionTrackingModes = ((WebAppContext) server.getHandlers()[0])
                .getServletContext().getEffectiveSessionTrackingModes();
        assertThat(effectiveSessionTrackingModes.size(), is(1));
        assertThat(effectiveSessionTrackingModes.contains(SessionTrackingMode.COOKIE), is(true));
    } finally {
        server.stop();
    }
}

From source file:io.lavagna.config.DispatcherServletInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    super.onStartup(servletContext);

    // initialize cookie
    if (StringUtils.isNotEmpty(System.getProperty(CookieNames.PROPERTY_NAME))) {
        CookieNames.updatePrefix(System.getProperty(CookieNames.PROPERTY_NAME));
    }/*www . jav a  2 s  . c om*/
    //

    //definition order = execution order, the first executed filter is HSTSFilter
    addFilter(servletContext, "HSTSFilter", HSTSFilter.class, "/*");

    addFilter(servletContext, "CSFRFilter", CSFRFilter.class, "/*");

    addFilter(servletContext, "RememberMeFilter", RememberMeFilter.class, "/*");

    addFilter(servletContext, "AnonymousUserFilter", AnonymousUserFilter.class, "/*");

    addFilter(servletContext, "SecurityFilter", SecurityFilter.class, "/*");

    addFilter(servletContext, "ETagFilter", ShallowEtagHeaderFilter.class, "*.js", "*.css", //
            "/", "/project/*", "/admin/*", "/me/", //
            "*.html", "*.woff", "*.eot", "*.svg", "*.ttf");

    addFilter(servletContext, "GzipFilter", GzipFilter.class, "*.js", "*.css", //
            "/", "/project/*", "/admin/*", "/me/", //
            "/api/self", "/api/board/*", "/api/project/*");

    servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
    servletContext.getSessionCookieConfig().setHttpOnly(true);
    servletContext.getSessionCookieConfig().setName(CookieNames.getSessionCookieName());
}

From source file:fr.univlorraine.mondossierweb.Initializer.java

/**
 * @see org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet.ServletContext)
 *///  ww  w  . j  a v  a  2  s.co m
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    addContextParametersToSystemProperties(servletContext);

    /* Configure les sessions */
    Set<SessionTrackingMode> sessionTrackingModes = new HashSet<SessionTrackingMode>();
    sessionTrackingModes.add(SessionTrackingMode.COOKIE);
    servletContext.setSessionTrackingModes(sessionTrackingModes);
    servletContext.addListener(new HttpSessionListener() {
        @Override
        public void sessionCreated(HttpSessionEvent httpSessionEvent) {
            // sans nouvelle requte, on garde la session active 4 minutes
            httpSessionEvent.getSession().setMaxInactiveInterval(240);
        }

        @Override
        public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        }
    });
    /* Gestion des sessions dans Atmosphere (Push Vaadin) */
    servletContext.addListener(SessionSupport.class);

    /* Configure Spring */
    AnnotationConfigWebApplicationContext springContext = new AnnotationConfigWebApplicationContext();
    if (!Boolean.valueOf(servletContext.getInitParameter(Constants.SERVLET_PARAMETER_PRODUCTION_MODE))) {
        springContext.getEnvironment().setActiveProfiles(DEBUG_PROFILE);
    }
    springContext.register(SpringConfig.class);
    servletContext.addListener(new ContextLoaderListener(springContext));
    servletContext.addListener(new RequestContextListener());

    /* Filtre Spring Security */
    FilterRegistration.Dynamic springSecurityFilterChain = servletContext.addFilter("springSecurityFilterChain",
            DelegatingFilterProxy.class);
    springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*");

    /* Filtre passant l'utilisateur courant  Logback */
    FilterRegistration.Dynamic userMdcServletFilter = servletContext.addFilter("userMdcServletFilter",
            UserMdcServletFilter.class);
    userMdcServletFilter.addMappingForUrlPatterns(null, false, "/*");

    /* Filtre Spring Mobile permettant de dtecter le device */
    FilterRegistration.Dynamic springMobileServletFilter = servletContext
            .addFilter("deviceResolverRequestFilter", DeviceResolverRequestFilter.class);
    springMobileServletFilter.addMappingForUrlPatterns(null, false, "/*");

    /* Servlet Spring-Vaadin */
    //ServletRegistration.Dynamic springVaadinServlet = servletContext.addServlet("springVaadin", JMeterServlet.class);
    //ServletRegistration.Dynamic springVaadinServlet = servletContext.addServlet("springVaadin", SpringVaadinServlet.class);
    ServletRegistration.Dynamic springVaadinServlet = servletContext.addServlet("springVaadin",
            fr.univlorraine.mondossierweb.utils.MdwSpringVaadinServlet.class);
    springVaadinServlet.setLoadOnStartup(1);
    springVaadinServlet.addMapping("/*");
    /* Dfini le bean UI */
    //springVaadinServlet.setInitParameter(Constants.SERVLET_PARAMETER_UI_PROVIDER, "fr.univlorraine.mondossierweb.MdwUIProvider");
    /* Utilise les messages Spring pour les messages d'erreur Vaadin (cf. http://vaadin.xpoft.ru/#system_messages) */
    springVaadinServlet.setInitParameter("systemMessagesBeanName", "DEFAULT");
    /* Dfini la frquence du heartbeat en secondes (cf. https://vaadin.com/book/vaadin7/-/page/application.lifecycle.html#application.lifecycle.ui-expiration) */
    springVaadinServlet.setInitParameter(Constants.SERVLET_PARAMETER_HEARTBEAT_INTERVAL, String.valueOf(30));

    /* Configure le Push */
    springVaadinServlet.setInitParameter(Constants.SERVLET_PARAMETER_PUSH_MODE,
            Boolean.valueOf(servletContext.getInitParameter("enablePush")) ? PushMode.AUTOMATIC.name()
                    : PushMode.DISABLED.name());

    /* Active le support des servlet 3 et des requtes asynchrones (cf. https://vaadin.com/wiki/-/wiki/Main/Working+around+push+issues) */
    springVaadinServlet.setInitParameter(ApplicationConfig.WEBSOCKET_SUPPORT_SERVLET3, String.valueOf(true));
    /* Active le support des requtes asynchrones */
    springVaadinServlet.setAsyncSupported(true);
    /* Ajoute l'interceptor Atmosphere permettant de restaurer le SecurityContext dans le SecurityContextHolder (cf. https://groups.google.com/forum/#!msg/atmosphere-framework/8yyOQALZEP8/ZCf4BHRgh_EJ) */
    springVaadinServlet.setInitParameter(ApplicationConfig.ATMOSPHERE_INTERCEPTORS,
            RecoverSecurityContextAtmosphereInterceptor.class.getName());

    /* Spring-Vaadin Touchkit Servlet  */
    ServletRegistration.Dynamic springTouchkitVaadinServlet = servletContext.addServlet("springTouchkitVaadin",
            MDWTouchkitServlet.class);
    //springTouchkitVaadinServlet.setLoadOnStartup(1);
    springTouchkitVaadinServlet.addMapping("/m/*");
    /* Dfini le bean UI */
    //springTouchkitVaadinServlet.setInitParameter(Constants.SERVLET_PARAMETER_UI_PROVIDER, "fr.univlorraine.mondossierweb.MdwTouchkitUIProvider");
    /* Utilise les messages Spring pour les messages d'erreur Vaadin (cf. http://vaadin.xpoft.ru/#system_messages) */
    springTouchkitVaadinServlet.setInitParameter("systemMessagesBeanName", "DEFAULT");
    springTouchkitVaadinServlet.setInitParameter(Constants.PARAMETER_WIDGETSET,
            "fr.univlorraine.mondossierweb.AppWidgetset");

    /* Configure le Push */
    springTouchkitVaadinServlet.setInitParameter(Constants.SERVLET_PARAMETER_PUSH_MODE,
            PushMode.DISABLED.name());
    /* Active le support des servlet 3 et des requtes asynchrones (cf. https://vaadin.com/wiki/-/wiki/Main/Working+around+push+issues) */
    springTouchkitVaadinServlet.setInitParameter(ApplicationConfig.WEBSOCKET_SUPPORT_SERVLET3,
            String.valueOf(true));
    /* Active le support des requtes asynchrones */
    springTouchkitVaadinServlet.setAsyncSupported(true);
    /* Ajoute l'interceptor Atmosphere permettant de restaurer le SecurityContext dans le SecurityContextHolder (cf. https://groups.google.com/forum/#!msg/atmosphere-framework/8yyOQALZEP8/ZCf4BHRgh_EJ) */
    springTouchkitVaadinServlet.setInitParameter(ApplicationConfig.ATMOSPHERE_INTERCEPTORS,
            RecoverSecurityContextAtmosphereInterceptor.class.getName());

}

From source file:org.wso2.carbon.webapp.mgt.TomcatUtil.java

private static void parseSessionCookiesId(Request request) {

    // If session tracking via cookies has been disabled for the current context, don't go looking for a session ID
    // in a cookie as a cookie from a parent context with a session ID may be present which would overwrite
    // the valid session ID encoded in the URL
    Context context = (Context) request.getMappingData().context;
    if (context != null && !context.getServletContext().getEffectiveSessionTrackingModes()
            .contains(SessionTrackingMode.COOKIE)) {
        return;/* w w  w  .  jav a2  s  .com*/
    }

    // Parse session id from cookies
    Cookie[] serverCookies = request.getCookies();
    int count = 0;
    if (serverCookies != null && (count = serverCookies.length) == 0) {
        return;
    }

    String sessionCookieName = SessionConfig.getSessionCookieName(context);
    for (int i = 0; i < count; i++) {
        Cookie cookie = serverCookies[i];
        if (cookie.getName().equals(sessionCookieName)) {
            // Override anything requested in the URL
            if (!request.isRequestedSessionIdFromCookie()) {
                // Accept only the first session id cookie
                request.setRequestedSessionId(cookie.getValue());
                request.setRequestedSessionCookie(true);
                request.setRequestedSessionURL(false);
                if (log.isDebugEnabled()) {
                    log.debug("Requested cookie session id is " + request.getRequestedSessionId());
                }
            } else {
                if (!request.isRequestedSessionIdValid()) {
                    // Replace the session id until one is valid
                    request.setRequestedSessionId(cookie.getValue());
                }
            }
        }
    }
}