<%
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) {}
}
}
%> |