これには、PrimeFacesアイドル モニターを使用できます。セッションを無効にするタイムアウト後に、ユーザーはログアウト アクションにリダイレクトされます。ユーザーに警告するカウントダウン ダイアログが表示される 2 分前。マウスを再度移動すると、セッションが延長されます。
PrimeFaces アイドル モニターとダイアログは、関連するすべてのページに追加できるテンプレートに配置されます。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:composition>
<h:form prependId="false">
<p:idleMonitor
timeout="#{session.maxInactiveInterval * 1000 - 125000}"
onidle="startIdleMonitor()"
onactive="timeoutDialog.hide()" />
<p:dialog id="timeoutSession"
header="#{msg['session.expire']}"
widgetVar="timeoutDialog"
showEffect="fade" hideEffect="fade"
modal="true"
width="400"
height="110"
closable="false"
draggable="false"
resizable="false"
appendToBody="true"
onHide="stopCount()"
onShow="doTimer()">
<br />
<p>
<span class="ui-icon ui-icon-alert" style="float: left; margin: 8px 8px 0;"/>
<p:panel>
#{msg['logoff.soon.1']}
<span id="dialog-countdown" style="font-weight: bold"></span>
#{msg['logoff.soon.2']}
</p:panel>
</p>
<br />
<p style="font-weight: bold;">#{msg['move.cursor']}</p>
</p:dialog>
<p:remoteCommand name="keepAlive" actionListener="#{auth.keepSessionAlive}" />
</h:form>
<script type="text/javascript">
var TIME = 120; // in seconds
var countTimer = TIME;
var processTimer;
var timer_is_on = 0;
var redirectPage = "#{request.contextPath}/auth/j_verinice_timeout";
var countDownDiv = "dialog-countdown";
var txtCountDown = null;
if (!txtCountDown)
txtCountDown = document.getElementById(countDownDiv);
function startIdleMonitor() {
countTimer = TIME;
txtCountDown.innerHTML = countTimer;
timeoutDialog.show();
}
function timedCount() {
txtCountDown.innerHTML = countTimer;
if (countTimer == 0) {
stopCount();
window.location.href = redirectPage;
return;
}
countTimer = countTimer - 1;
processTimer = setTimeout("timedCount()", 1000);
}
function doTimer() {
if (!timer_is_on) {
timer_is_on = 1;
timedCount();
}
}
function stopCount() {
clearTimeout(processTimer);
timer_is_on = 0;
keepAlive();
}
</script>
</ui:composition>
</html>
- 行 11: アイドル モニターのタイムアウトは、システム var session.maxInactiveIntervalによって設定されます。web.xmlまたはサーバー構成で設定した値。
- 行 12/13: Javascript メソッドの startIdleMonitor() がタイムアウト後にユーザーの操作なしで呼び出されます。このメソッドは、ダイアログを開きます。timeoutDialog.hide()は、ユーザーが再びビジー状態になると呼び出されます: ダイアログが閉じられます
- 行 26/27: ダイアログが表示または非表示になると、さらに 2 つの Javascript メソッドが呼び出されます: doTimer()が開始し、stopCount()がカウントダウンを停止します。
- 40 行目: セッションを維持するための PrimeFaces リモート コマンド。サーバーで任意のメソッドを呼び出すことにより、セッションが延長されます。コマンドは、78 行目の Javascript メソッドkeepAlive()によって呼び出されます。
- 行 59 ~ 68: Javascript メソッドtimedCount()が毎秒呼び出され、カウントダウンが実行されます。タイムアウト後のリダイレクトは 63 行目で行われます。
複数のページでタイムアウト処理を有効にするには、レイアウト テンプレートにタイムアウト テンプレートを含めます。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xml:lang="de-DE">
<h:head>
...
</h:head>
<body>
<ui:include src="/template/sessionTimeOut.xhtml" />
<ui:include src="/nav.xhtml"/>>
<ui:insert name="content">Default content</ui:insert>
<ui:include src="/footer.xhtml"/>>
</body>
</html>
web.xml で設定できる Web アプリケーションの特定のタイムアウト:
<!--?xml version="1.0" encoding="UTF-8"?-->
<web-app>
...
<session-config>
<!-- Session idle timeout in min. -->
<session-timeout>30</session-timeout>
</session-config>
</web-app>
このソリューションの詳細については、次のブログ記事を参照してください: JSF と PrimeFaces: セッション タイムアウト処理