5.10.4 認証に成功したSubjectをHttpSessionに登録する実装

HttpSessionに設定するオブジェクトは,java.io.Serializableインタフェースを継承したオブジェクトになります。そのため,HttpSessionにはログイン時に生成したLoginContextインスタンスではなく,java.io.Serializableインタフェースを継承しているSubjectを格納してください。格納したSubjectは,ログアウトの実装で必要になります。SubjectをHttpSessionに格納する実装例(太字部分)を次に示します。

<%
 LoginContext lc = new LoginContext("Portal",
   new WebPasswordHandler(request, response, null, "login.html", true));
 try {
   lc.login();
   session.setAttribute("ExampleSubject", lc.getSubject());
 } catch (LoginException e) { ... }%
>
...

なお,ログイン後にSubjectに関連づけられるユーザ属性(UserAttributes)をセッションフェイルオーバで引き継がせる場合は,Subjectとユーザ属性をHttpSessionに格納する必要があります。Subjectとユーザ属性をHttpSessionに格納する実装例(太字部分)を次に示します。

<%
 LoginContext lc = new LoginContext("Portal",
   new WebPasswordHandler(request, response, null, "login.html", true));
 try {
   lc.login();
    session.setAttribute("ExampleSubject", lc.getSubject());
    session.setAttribute("ExampleCredential", lc.getSubject().getPublicCredentials().iterator().next());
 } catch (LoginException e) { ... }%
>
...