2011-12-18 32 views
5

セキュリティ層としてSpring Securityを使用するwebappを開発しています。ログイン時間とセッションの継続時間を記録する - Java - Spring Security

重要な機能の1つは、どのユーザーがアプリケーションにアクセスしているか、またアプリケーションに費やしている時間を知ることです。

私はそれに対処する方法がわかりません。この種の使用統計を扱う他のフレームワークはありますか?

Spring Security自体を処理する方法はありますか?

// Spring Securityについて詳しくは、フィルタが役立つようです。進捗状況はここで共有されます。

答えて

5

ユーザー

ログインの固有名/ユーザー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()); 
    } 


} 

} 

実装する新しいクラスを追加します。

+0

春のセキュリティフィルタは、ユーザーがログインしたときにいつでも登録できるようです。ソリューションは、セキュリティフィルターを使用してデータベース(あなたが言ったように)に永続的になるでしょう、ユーザーID、ログイン時間とログアウト時間! –

0

たぶん、あなたはこのような何かを探しています:あなたのサイト上での現在のユーザーを追跡するために

ユーティリティ、そして、彼らは詳細にしてきたところ。これにより、サイト全体の「クリックストリーム」や「トラフィックパス」をトラッキングできます。私はHttpSessionListenerのを使用していると考えることができます解決策の一つは、あなたがセッションリスナーを実装する場合のように、時間をキャプチャ可能性があり、新しいユーザーセッションが作成され、破棄されたとき、あなたが活用できると思い

http://www.quartzscheduler.org/clickstream/

+0

Hm、良い選択であるかどうかはわかりません。 OpenSymphonyが閉鎖しており、このプロジェクトは中止されたようです。 –

関連する問題