ユーザー
ログインの固有名/ユーザーIDのホールドを得るためにあなたの春のセキュリティコンテキストホルダー私はトンまでいくつかの側面があるとされていることは、この
public class SesssionListenerImpl implements HttpSessionListener
{
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent)
{
String uniqueName = SecurityContextHolder.getContext().getAuthentication().getName();
String sessionId = httpSessionEvent.getSession().getId();
long beginTimeInSeconds = System.currentTimeMillis()/1000;
//create a record and persist to data base with sessionId, uniquename, sessionBeginTime
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent)
{
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
httpSessionEvent.getSession().getId();
long endTime = System.currentTimeMillis()/1000;
//load the record based on sessionId
//update the record with sessionEndTime
}
}
のようないくつかのことを考えています彼のアプローチは、HTTPセッションが無効になることがないなら、あなたは終了時間のないいくつかのセッションに終わるでしょう。
- あなたは優しくとして良い練習あなたが行うことができ
- (ない現実的な解決策が)が負荷に体で、ユーザーが自分のドメインを残しているかどうかをチェックするすべての時間をログアウトするユーザーベースををprodことができればまたは終了時間
UPDATE
をキャプチャするウィンドウを閉じ、セッションの無効化を発射するウィンドウを閉じるボタンを使用します私はあなたがあなたのweb.xmlにこれを追加することを春のアプリケーションイベントメカニズムを使用することができると思います。このリスナーは、HTTPセッションイベントを公開します。アプリケーションリスナーを実装しても、セッションイベントにアクセスできなくなります。
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
は今、あなたは自分自身でログアウトをキャプチャしたい場合は、あなたがイベントをlogutにアクセスすることができますLogoutHandlerを実装することができます別のイベントがありApplicationListener
@Service
public class ApplicationSecurityListener implements ApplicationListener<ApplicationEvent>
{
@Override
public void onApplicationEvent(ApplicationEvent event)
{
if (event instanceof AuthorizationFailureEvent)
{
AuthorizationFailureEvent authorizationFailureEvent = (AuthorizationFailureEvent) event;
System.out.println ("not authorized:" + authorizationFailureEvent);
}
else if (event instanceof AuthenticationFailureBadCredentialsEvent)
{
AuthenticationFailureBadCredentialsEvent badCredentialsEvent = (AuthenticationFailureBadCredentialsEvent) event;
System.out.println ("badCredentials:" + badCredentialsEvent);
}
//login success event
else if (event instanceof AuthenticationSuccessEvent)
{
AuthenticationSuccessEvent authenticationSuccessEvent = (AuthenticationSuccessEvent) event;
//this will provide user id and password but no session, get source has all the user information in security context
System.out.println ("AuthenticationSuccessEvent:" + authenticationSuccessEvent.getSource());
}
//this event will published if you add the HttpSessionEventPublisher to web.xml
else if (event instanceof SessionDestroyedEvent)
{
SessionDestroyedEvent sessinEvent = (SessionDestroyedEvent) event;
System.out.println ("SessionDestroyedEvent:" + sessinEvent.getId());
//load session if it is not empty
if(sessinEvent.getSecurityContext() != null)
{
System.out.println ("SessionDestroyedEvent:" + sessinEvent.getSecurityContext().getAuthentication().getName());
//update the session with endTime
}
}
else
{
//System.out.println ("undefined: " + event.getClass().getName());
}
}
}
実装する新しいクラスを追加します。
春のセキュリティフィルタは、ユーザーがログインしたときにいつでも登録できるようです。ソリューションは、セキュリティフィルターを使用してデータベース(あなたが言ったように)に永続的になるでしょう、ユーザーID、ログイン時間とログアウト時間! –