-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6380 from effective-webwork/automatic-logout
Logout idle user automatically when HTTP session expires
- Loading branch information
Showing
10 changed files
with
194 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Kitodo/src/main/java/org/kitodo/production/helper/ActivityMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* (c) Kitodo. Key to digital objects e. V. <[email protected]> | ||
* | ||
* This file is part of the Kitodo project. | ||
* | ||
* It is licensed under GNU General Public License version 3 or later. | ||
* | ||
* For the full copyright and license information, please read the | ||
* GPL3-License.txt file that was distributed with this source code. | ||
*/ | ||
|
||
package org.kitodo.production.helper; | ||
|
||
import java.util.Iterator; | ||
|
||
import javax.enterprise.context.RequestScoped; | ||
import javax.faces.application.FacesMessage; | ||
import javax.faces.context.FacesContext; | ||
import javax.inject.Named; | ||
|
||
import org.primefaces.PrimeFaces; | ||
|
||
@Named | ||
@RequestScoped | ||
public class ActivityMonitor { | ||
|
||
/** | ||
* Event handler for 'idle' event. Triggered when user becomes idle and is about to be logged out automatically. | ||
* Displays a warning message to inform the user he is about to get logged out soon. | ||
*/ | ||
public void onIdle() { | ||
String warningTitle = Helper.getTranslation("automaticLogoutWarningTitle"); | ||
String warningDescription = Helper.getTranslation("automaticLogoutWarningDescription"); | ||
PrimeFaces.current().executeScript("PF('sticky-notifications').renderMessage(" | ||
+ "{'summary':'" + warningTitle + "','detail':'" + warningDescription + "','severity':'error'});"); | ||
} | ||
|
||
/** | ||
* Event handler for 'active' event. Triggered when user becomes active again after being idle. | ||
* Removes the warning message about pending automatic logout. | ||
*/ | ||
public void onActive() { | ||
Iterator<FacesMessage> messageIterator = FacesContext.getCurrentInstance().getMessages(); | ||
while (messageIterator.hasNext()) { | ||
messageIterator.next(); | ||
messageIterator.remove(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Kitodo/src/main/java/org/kitodo/production/session/CustomHttpSessionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* (c) Kitodo. Key to digital objects e. V. <[email protected]> | ||
* | ||
* This file is part of the Kitodo project. | ||
* | ||
* It is licensed under GNU General Public License version 3 or later. | ||
* | ||
* For the full copyright and license information, please read the | ||
* GPL3-License.txt file that was distributed with this source code. | ||
*/ | ||
|
||
|
||
package org.kitodo.production.session; | ||
|
||
import java.util.Objects; | ||
|
||
import javax.servlet.annotation.WebListener; | ||
import javax.servlet.http.HttpSessionEvent; | ||
import javax.servlet.http.HttpSessionListener; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.kitodo.production.helper.Helper; | ||
import org.kitodo.production.security.SecurityUserDetails; | ||
import org.kitodo.production.services.ServiceManager; | ||
import org.springframework.security.core.context.SecurityContextImpl; | ||
|
||
|
||
@WebListener | ||
public class CustomHttpSessionListener implements HttpSessionListener { | ||
|
||
private static final Logger logger = LogManager.getLogger(CustomHttpSessionListener.class); | ||
|
||
/** | ||
* Event handler that is triggere when an HTTP session is created. | ||
* | ||
* @param sessionEvent the notification event | ||
*/ | ||
@Override | ||
public void sessionCreated(HttpSessionEvent sessionEvent) { | ||
logger.debug("Session created: {}", sessionEvent.getSession().getId()); | ||
} | ||
|
||
/** | ||
* Event handler that is triggered when an HTTP session expires. | ||
* | ||
* @param sessionEvent the notification event | ||
*/ | ||
@Override | ||
public void sessionDestroyed(HttpSessionEvent sessionEvent) { | ||
Object securityContextObject = sessionEvent.getSession().getAttribute("SPRING_SECURITY_CONTEXT"); | ||
if (Objects.nonNull(securityContextObject) && securityContextObject instanceof SecurityContextImpl) { | ||
SecurityContextImpl securityContext = (SecurityContextImpl) securityContextObject; | ||
Object principal = securityContext.getAuthentication().getPrincipal(); | ||
if (principal instanceof SecurityUserDetails) { | ||
logger.debug("Session expired: {}", sessionEvent.getSession().getId()); | ||
ServiceManager.getSessionService().expireSessionsOfUser((SecurityUserDetails) principal); | ||
} else { | ||
logger.debug("Cannot expire session: {} is not an instance of SecurityUserDetails", | ||
Helper.getObjectDescription(principal)); | ||
} | ||
} else { | ||
logger.debug("Cannot expire session: {} is not an instance of SecurityContextImpl", | ||
Helper.getObjectDescription(securityContextObject)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters