HttpSessionDestroyedEventListener.java

  1. /*
  2.  * Copyright 2010-2025 James Pether Sörling
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  *  $Id$
  17.  *  $HeadURL$
  18. */
  19. package com.hack23.cia.web.impl.ui.application.web.listener;

  20. import java.util.ArrayList;
  21. import java.util.Collection;

  22. import javax.servlet.http.HttpSession;

  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.context.ApplicationListener;
  27. import org.springframework.security.authentication.AnonymousAuthenticationToken;
  28. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  29. import org.springframework.security.core.context.SecurityContextHolder;
  30. import org.springframework.security.web.session.HttpSessionDestroyedEvent;
  31. import org.springframework.stereotype.Service;

  32. import com.hack23.cia.service.api.ApplicationManager;
  33. import com.hack23.cia.service.api.action.application.DestroyApplicationSessionRequest;

  34. /**
  35.  * The Class HttpSessionDestroyedEventListener.
  36.  *
  37.  * @see HttpSessionDestroyedEventEvent
  38.  */
  39. @Service
  40. public final class HttpSessionDestroyedEventListener implements ApplicationListener<HttpSessionDestroyedEvent> {

  41.     /** The Constant KEY. */
  42.     private static final String KEY = "HttpSessionDestroyedEventListener";

  43.     /** The Constant LOG_MSG_SESSION_DESTROYED_SESSION_ID. */
  44.     private static final String LOG_MSG_SESSION_DESTROYED_SESSION_ID = "Session destroyed SESSION_ID :{}";

  45.     /** The Constant LOGGER. */
  46.     private static final Logger LOGGER = LoggerFactory.getLogger(HttpSessionDestroyedEventListener.class);

  47.     /** The Constant PRINCIPAL. */
  48.     private static final String PRINCIPAL = "AnonymousUser";

  49.     /** The Constant ROLE_ANONYMOUS. */
  50.     private static final String ROLE_ANONYMOUS = "ROLE_ANONYMOUS";

  51.     /** The application manager. */
  52.     @Autowired
  53.     private ApplicationManager applicationManager;

  54.     /**
  55.      * Instantiates a new http session destroyed event listener.
  56.      */
  57.     public HttpSessionDestroyedEventListener() {
  58.         super();
  59.     }

  60.     @Override
  61.     public void onApplicationEvent(final HttpSessionDestroyedEvent event) {
  62.         final HttpSession httpSession = event.getSession();
  63.         final Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
  64.         authorities.add(new SimpleGrantedAuthority(ROLE_ANONYMOUS));
  65.         final DestroyApplicationSessionRequest destroyApplicationSessionRequest = new DestroyApplicationSessionRequest();
  66.         destroyApplicationSessionRequest.setSessionId(httpSession.getId());

  67.         SecurityContextHolder.getContext()
  68.                 .setAuthentication(new AnonymousAuthenticationToken(KEY, PRINCIPAL, authorities));
  69.         applicationManager.service(destroyApplicationSessionRequest);
  70.         SecurityContextHolder.getContext().setAuthentication(null);

  71.         LOGGER.info(LOG_MSG_SESSION_DESTROYED_SESSION_ID, httpSession.getId());
  72.     }

  73. }