将 Java 认证和授权服务编程模型用于 Web 认证
WebSphere® Application Server 支持 Java™ Platform, Enterprise Edition (Java EE) 声明式安全性模型。可以使用 Java EE 部署描述符来定义认证和访问控制策略。可以进一步堆叠定制登录模块来定制 WebSphere Application Server 认证机制。
开始之前
关于此任务
如果启用了轻量级第三方认证 (LTPA) 机制单点登录 (SSO) 选项,那么在成功登录后,Web 客户机登录会话由 LTPA SSO 令牌 Cookie 跟踪。注销时,此令牌被删除,以终止登录会话,但服务器端主体集未被删除。当您使用声明式安全性模型时,WebSphere Application Server Web 容器自动执行客户机认证和登录会话管理。通过在没有 Java EE 安全性约束的情况下设置登录页面,并且将客户机请求首先定向到您的登录页面,您可以使用应用程序代码执行认证。您的登录页面可以使用 Java 认证和授权服务 (JAAS) 编程模型来执行认证。要使 WebSphere Application Server Web 登录模块能够生成 SSO Cookie,请使用以下步骤。
过程
示例
使用以下代码样本来执行认证。

Suppose you wrote a LoginServlet.java:
Import com.ibm.wsspi.security.auth.callback.WSCallbackHandlerFactory;
Import com.ibm.websphere.security.auth.WSSubject;
public Object login(HttpServletRequest req, HttpServletResponse res)
throws ServletException {
PrintWriter out = null;
try {
out = res.getWriter();
res.setContentType("text/html");
} catch (java.io.IOException e){
// Error handling
}
Subject subject = null;
try {
LoginContext lc = new LoginContext("system.Your_login_configuration",
WSCallbackHandlerFactory.getInstance().getCallbackHandler(
userid, null, password, req, res, null));
lc.login();
subject = lc.getSubject();
WSSubject.setRunAsSubject(subject);
} catch(Exception e) {
// catch all possible exceptions if you want or handle them separately
out.println("Exception in LoginContext login + Exception = " +
e.getMessage());
throw new ServletException(e.getMessage());
}
The following is sample code to revoke the SSO cookies upon a programming logout:
The LogoutServlet.java:
public void logout(HttpServletRequest req, HttpServletResponse res,
Object retCreds) throws ServletException {
PrintWriter out =null;
try {
out = res.getWriter();
res.setContentType("text/html");
} catch (java.io.IOException e){
// Error Handling
}
try {
WSSecurityHelper.revokeSSOCookies(req, res);
} catch(Exception e) {
// catch all possible exceptions if you want or handle them separately
out.println("JAASLogoutServlet: logout Exception = " + e.getMessage());
throw new ServletException(e);
}
}