将公共对象请求代理体系结构程序化登录迁移到 Java 认证和授权服务 (CORBA 和 JAAS)

使用本主题作为如何使用基于 CORBA 的程序化登录 API 执行程序化登录的示例。

准备工作

[AIX Solaris HP-UX Linux Windows][IBM i]本文档概述了不推荐使用的公共对象请求代理体系结构 (CORBA) 程序化登录 API 以及 JAAS提供的替代方法。 WebSphere® Application Server 完全支持 Java™ 认证和授权服务 (JAAS) 作为程序化登录应用程序编程接口 (API)。 有关 JAAS 支持的更多详细信息,请参阅 为 Java 认证和授权服务配置程序化登录使用 Java 认证和授权服务开发程序化登录

下面列出了已废弃的 CORBA 编程登录 API。
  • [AIX Solaris HP-UX Linux Windows]${user.install.root}/installedApps/sampleApp.ear/default_app.war/WEB-INF/classes/LoginHelper.java.
  • [AIX Solaris HP-UX Linux Windows]${user.install.root}/installedApps/sampleApp.ear/default_app.war/WEB-INF/classes/ServerSideAuthenticator.java.
  • [IBM i]profile_root/installedApps/sampleApp.ear/default_app.war/WEB-INF/classes/ServerSideAuthenticator.java.
  • [AIX Solaris HP-UX Linux Windows][IBM i]org.omg.SecurityLevel2.Credentials. 产品中包含该应用程序接口,但不建议您使用该应用程序接口。

WebSphere Application Server 中提供的 API 是标准 JAAS API 与标准 JAAS 接口的产品实现的组合。

以下信息只是摘要; 请参阅位于以下位置的平台的 JAAS 文档: http://www.ibm.com/developerworks/java/jdk/security/

  • 程序化登录 API:
    • javax.security.auth.login.LoginContext
    • javax.security.auth.callback.CallbackHandler 接口: WebSphere Application Server 产品提供 javax.security.auth.callback.CallbackHandler 接口的以下实现:
      com.ibm.websphere.security.auth.callback.WSCallbackHandlerImpl
      当应用程序将基本认证数据(用户标识、密码和安全领域)或令牌数据推送到产品登录模块时,提供非提示 CallbackHandler 处理程序。 建议在服务器端登录时使用此 API。
      [AIX Solaris HP-UX Linux Windows][IBM i]com.ibm.websphere.security.auth.callback.WSGUICallbackHandlerImpl
      [AIX Solaris HP-UX Linux Windows][IBM i]提供登录提示 CallbackHandler 处理程序以收集基本认证数据 (用户标识,密码和安全域)。 建议在客户端登录时使用此 API。

      如果在服务器端使用此 API,则会阻止服务器输入。

    • javax.security.auth.callback.Callback 接口:
      javax.security.auth.callback.NameCallback
      由 JAAS 提供,以将用户名传递到 LoginModules 接口。
      javax.security.auth.callback.PasswordCallback
      由 JAAS 提供,以将密码传递到 LoginModules 接口。
      com.ibm.websphere.security.auth.callback.WSCredTokenCallbackImpl
      由产品提供,以执行基于令牌的登录。 通过该应用程序接口,应用程序可以向 LoginModules 接口传递一个以字节为单位的令牌数组。
    • javax.security.auth.spi.LoginModule 接口

      WebSphere Application Server 为客户机和服务器端登录提供了 LoginModules 实现。 有关详细信息,请参阅 配置 Java 认证和授权服务的程序化登录

  • javax.security.Subject:
    [AIX Solaris HP-UX Linux Windows][IBM i]com.ibm.websphere.security.auth.WSSubject
    [AIX Solaris HP-UX Linux Windows][IBM i]产品提供的扩展,用于使用 javax.security.Subject 中的凭证来调用远程 J2EE 资源
    com.ibm.websphere.security.cred.WSCredential
    在使用 WebSphere Application Server LoginModules 接口成功登录 JAAS 之后,将创建 com.ibm.websphere.security.cred.WSCredential 凭证并将其存储在主体集中。
    com.ibm.websphere.security.auth.WSPrincipal
    由 WebSphere Application Server LoginModules 接口认证的主体集中创建和存储的已认证主体集。

过程

  1. [AIX Solaris HP-UX Linux Windows][IBM i]使用以下示例说明如何使用基于 CORBA 的程序化登录 API 执行程序化登录:
    基于 CORBA 的程序化登录 API 由 JAAS 登录替换。
    注: 以下示例中使用的 LoginHelper 应用程序编程接口 (API) 在 WebSphere Application Server V 9.0 中已不推荐使用,并且将在将来的发行版中除去。 建议使用下一个步骤中显示的 JAAS 程序化登录 API。
    public class TestClient {
    ...
    private void performLogin() {
    // Get the ID and password of the user.
    String userid = customGetUserid();
    String password = customGetPassword();
    
    // Create a new security context to hold authentication data.
    LoginHelper loginHelper = new LoginHelper();
    try {
    // Provide the ID and password of the user for authentication.
    org.omg.SecurityLevel2.Credentials credentials = 
    loginHelper.login(userid, password);
    
    // Use the new credentials for all future invocations.
    loginHelper.setInvocationCredentials(credentials);
    // Retrieve the name of the user from the credentials
    // so we can tell the user that login succeeded.
    
    String username = loginHelper.getUserName(credentials);
    System.out.println("Security context set for user: "+username);
    } catch (org.omg.SecurityLevel2.LoginFailed e) {
    // Handle the LoginFailed exception.
    }
    }
    ...
    }
  2. 使用以下示例将基于 CORBA 的程序化登录 API 迁移到 JAAS 程序化登录 API。

    以下示例假定已为应用程序代码授予必需的 Java 2 安全许可权。 有关更多信息,请参阅 配置 Java 认证和授权服务的程序化登录保护用于开发应用程序的系统资源和 API (Java 2 安全性)以及位于 http://www.ibm.com/developerworks/java/jdk/security/的 JAAS 文档。

    public class TestClient {
    ...
    private void performLogin() {
    // Create a new JAAS LoginContext.
    javax.security.auth.login.LoginContext lc = null;
    
    try {
    // Use GUI prompt to gather the BasicAuth data.
    lc = new javax.security.auth.login.LoginContext("WSLogin",
    new com.ibm.websphere.security.auth.callback.WSGUICallbackHandlerImpl());
    
    // create a LoginContext and specify a CallbackHandler implementation
    // CallbackHandler implementation determine how authentication data is collected
    // in this case, the authentication date is collected by  login prompt
    //   and pass to the authentication mechanism implemented by the LoginModule.
    } catch (javax.security.auth.login.LoginException e) {
    System.err.println("ERROR: failed to instantiate a LoginContext and the exception: " 
    + e.getMessage());
    e.printStackTrace();
    
    // may be javax.security.auth.AuthPermission "createLoginContext" is not granted
    //   to the application, or the JAAS Login Configuration is not defined.
    }
    
    if (lc != null)
    try {
    lc.login();  // perform login
    javax.security.auth.Subject s = lc.getSubject();
    // get the authenticated subject
    
    // Invoke a J2EE resources using the authenticated subject
    com.ibm.websphere.security.auth.WSSubject.doAs(s,
    new java.security.PrivilegedAction() {
    public Object run() {
    try {
    bankAccount.deposit(100.00);  // where bankAccount is an protected EJB
    } catch (Exception e) {
    System.out.println("ERROR: error while accessing EJB resource, exception: " 
    + e.getMessage());
    e.printStackTrace();
    }
    return null;
    }
    }
    );
    
    // Retrieve the name of the principal from the Subject
    // so we can tell the user that login succeeded,
    // should only be one WSPrincipal.
    java.util.Set ps = 
    s.getPrincipals(com.ibm.websphere.security.auth.WSPrincipal.class);
    java.util.Iterator it = ps.iterator();
    while (it.hasNext()) {
    com.ibm.websphere.security.auth.WSPrincipal p =
    (com.ibm.websphere.security.auth.WSPrincipal) it.next();
    System.out.println("Principal: " + p.getName());
    }
    } catch (javax.security.auth.login.LoginException e) {
    System.err.println("ERROR: login failed with exception: " + e.getMessage());
    e.printStackTrace();
    
    // login failed, might want to provide relogin logic
    }
    }
    ...
    }