Cosminexus V9 アプリケーションサーバ 機能解説 セキュリティ管理機能編

[目次][用語][索引][前へ][次へ]

5.10.5 ログアウトの実装(APIを使用する場合)

ログアウトは「5.10.4 認証に成功したSubjectをHttpSessionに登録する実装」でHttpSessionに登録されているSubjectを使用してLoginContextを再生成し,ログアウト処理を実行します。HttpSessionに登録されているSubjectは削除します。さらに,ユーザ属性をHttpSessionに登録している場合はその削除処理も実行します。ユーザ属性がある場合のログアウトの実装例を次に示します。

<%
  try {
    Subject subject = (Subject)session.getAttribute("ExampleSubject");
    LoginContext lc = new LoginContext("Example", subject);
    session.removeAttribute("ExampleCredential");
    session.removeAttribute("ExampleSubject");
    lc.logout();
  } catch (LoginException e) { ... }
%>
...

セッションタイムアウト時にログアウトするためには,HttpSessionBindingListenerインタフェースを実装したオブジェクトを,HttpSessionオブジェクトに設定します。セッションタイムアウト時のログアウトの実装例を次に示します。

<%
  LoginContext lc = new LoginContext("Portal",
    new WebPasswordHandler(request, response, null, "login.html", true));
  try {
    lc.login();
     session.setAttribute("logoutObject",
        new MyListener("Portal", "ExampleSubject", "ExampleCredential"));
    session.setAttribute("ExampleSubject", lc.getSubject());
    session.setAttribute("ExampleCredential",
    lc.getSubject().getPublicCredentials().iterator().next());
 
  } catch (LoginException e) { ... }
%>
<%!
  class MyListener implements
          HttpSessionBindingListener, java.io.Serializable {
      String name;
      String subjectName;
      String attrsName;
      public MyListener(String name, String subjectName, String attrsName) {
          this.name = name;
          this.subjectName = subjectName;
          this.attrsName = attrsName;
      }
      public void valueBound(HttpSessionBindingEvent ev) {}
      public void valueUnbound(HttpSessionBindingEvent ev) {
          Subject subject =
           (Subject)ev.getSession().getAttribute(subjectName);
          ev.getSession().removeAttribute(attrsName);
          ev.getSession().removeAttribute(subjectName);
          try {
              LoginContext ctx = new LoginContext(name, subject);
              ctx.logout();
          } catch (LoginException e) {}
      }
  }
%>