Cosminexus 機能解説
HttpSessionに設定するオブジェクトは,java.io.Serializableインタフェースを継承したオブジェクトであることが推奨されます。また,HttpSessionには,java.io.Serializableインタフェースを継承したSubjectを格納して,ログアウトする方法が推奨されます。
なお,ログイン後にSubjectに関連づけられるユーザ属性(UserAttributes)をセッションフェイルオーバで引き継がせるには,HttpSessionにSubjectを格納する必要があります。ここでは,HttpSessionにjava.io.Serializableインタフェースを継承したSubjectを格納する場合のログインとログアウトの実装例を示します。
ログインでは,ログインが成功したSubjectをHttpSessionに登録します。次に,HttpSessionにSubjectを格納する場合のログインの実装例を示します。
<%
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) { ... }%
>
...
|
ログアウトは,HttpSessionに登録されているSubjectを使用して行います。次に,ユーザ属性がある場合のログアウトの実装例を示します。
<%
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) {}
}
}
%>
|
All Rights Reserved. Copyright (C) 2006, 2007, Hitachi, Ltd.