【问题标题】:How can I access WebSphere authentication alias info from a Java client running outside of the server?如何从在服务器外部运行的 Java 客户端访问 WebSphere 身份验证别名信息?
【发布时间】:2017-01-28 01:09:43
【问题描述】:

我使用以下代码 (see this SO post) 来读取在我的 WAS 7 服务器上存储为 JC2 别名的用户 ID 和密码。

    Map<String, String> map = new HashMap<String, String>();
    map.put(Constants.MAPPING_ALIAS, MDM_JC2_ALIAS);
    CallbackHandler callbackHandler = WSMappingCallbackHandlerFactory.getInstance().getCallbackHandler(map, null);
    LoginContext loginContext = new LoginContext(DEFAULT_PRINCIPAL_MAPPING, callbackHandler);
    loginContext.login();
    Subject subject = loginContext.getSubject();
    Set<Object> credentials = subject.getPrivateCredentials();
    PasswordCredential passwordCredential = (PasswordCredential) credentials.iterator().next();
    userId = passwordCredential.getUserName();
    password = new String(passwordCredential.getPassword());

代码运行良好。但现在我正在尝试在批处理中使用它。要测试批处理过程,我必须在 Rad 8.5 中使用 Run->Debug As。 (我使用 Run->Debug As->Debug 配置来配置进程)。我收到错误“java.lang.NullPointerException:WSMappingCallbackHandlerFactory 未初始化”。我已经逐步完成了有效的代码,并且看不到与无效代码的值有任何区别。我怀疑我可能需要在调试配置中修改构建路径,但我不知道要更改什么。

编辑:

我认为我没有很好地解释这种情况。该代码在 WAS 7 上运行的 Web 服务中运行。我们有完全不同的项目,其中包含一些称为批处理作业的代码,如下所示:

-classpath D:\WebSphere\AppServer\profiles\AppSrv01\Apps\Cell01\SSS.ear\PlanningEJB.jar;
D:\WebSphere\AppServer\profiles\AppSrv01\Apps\Cell01\SSS.ear\Planning.war\WEB-INF\classes;
D:\Progra~1\IBM\SQLLIB\java\db2jcc.jar -Dlog4j.configuration=file:/d:/apps/websphere/SSS/properties/log4J.properties
url.planning.batch.AppName D:\\apps\\websphere\\SSS\\properties\\sss.properties

我想将读取用户 ID 和密码的代码添加到称为批处理作业的代码中。通常要调试称为批处理作业的代码,我们使用调试配置并且服务器不必运行。我可以设置断点并单步执行代码,直到我到达callbackHandler 行。

【问题讨论】:

  • 当你说代码工作正常时。你的意思是你可以让它与 Run->Run As 一起工作,但当你尝试调试时失败?如果这不是您的意思,您能否澄清一下您在可以使其工作时使用的设置/环境?
  • 它在被称为 Web 服务的 Web 项目中运行良好。
  • 你需要在调试模式下启动服务器,然后调用该服务,如果你设置了一个,你应该切换到调试透视和断点。如果您尝试将其作为标准 java 应用程序进行调试,它将无法正常工作。它必须从服务器运行。
  • 要获取服务器外部的配置信息,您需要使用com.ibm.websphere.management.* API 之一,例如 (com.ibm.websphere.management.configservice.ConfigService)、MBeans ,或 wsadmin jacl/jython。正如@Gas 所说,您使用的 API 只能在服务器中运行。
  • 说你是在“批处理”模式下运行它,你的意思是你试图在服务器之外运行一个独立的 Java main()?如果是这样,您是否假设服务器环境中可用的任何 API 在服务器外部都可用,只需在类路径中放置适当的类?情况并非如此,如果 API 在服务器之外无法工作,则您无法对类路径或 Debug 配置进行任何更改。

标签: java eclipse websphere ibm-rad


【解决方案1】:

如何编写Java客户端(在服务器外运行)查看WebSphere认证别名信息

您不能在服务器外运行的客户端使用与在服务器内运行的 API 相同的 API。

要在 Java 中执行此操作(与作为替代方法的 wsadmin/Jython 不同),您可以从以下代码开始:

示例 Java 代码

package mypkg;

import java.util.Properties;

import javax.management.ObjectName;

import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.Session;
import com.ibm.websphere.management.configservice.ConfigServiceHelper;
import com.ibm.websphere.management.configservice.ConfigServiceProxy;

public class MyAdminClient {

    public static void main(String[] args) throws Exception {


        String aliasToFind = "blahAlias";  // Or "MyNode/blahAlias" if you use the node prefix.
        String SOAP_CONNECTOR_ADDRESS_PORT = "8879";
        String host = "localhost";

        // Initialize the AdminClient
        Properties adminProps = new Properties();
        adminProps.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
        adminProps.setProperty(AdminClient.CONNECTOR_HOST, host);
        adminProps.setProperty(AdminClient.CONNECTOR_PORT, SOAP_CONNECTOR_ADDRESS_PORT);
        AdminClient adminClient = AdminClientFactory.createAdminClient(adminProps);

        // Get the ConfigService implementation
        ConfigServiceProxy configService = new ConfigServiceProxy(adminClient);

        Session session = new Session();

        // Find the parent security object
        ObjectName security = ConfigServiceHelper.createObjectName(null, "Security", null);
        ObjectName[] securityName = configService.queryConfigObjects(session, null, security, null);
        security = securityName[0];

        ObjectName authData = ConfigServiceHelper.createObjectName(null, "JAASAuthData", null);
        ObjectName[] authDataEntries = configService.queryConfigObjects(session, null, authData, null);
        ObjectName auth;

        for (int i=0; i < authDataEntries.length; i++) {
            auth = authDataEntries[i];
            Object aliasAttr = configService.getAttribute(session, auth, "alias");
            if (aliasAttr.equals(aliasToFind)) {
                System.out.println("Alias located: alias = " + configService.getAttribute(session, auth, "alias")
                + "; userId = " + configService.getAttribute(session, auth, "userId")                                
                + "; password = " + configService.getAttribute(session, auth, "password"));
                break;
            }
        }
    }
}

使用“管理瘦客户端”编译/运行

在最简单的情况下(没有安全性,这可能对入门很有用),您只需将 Administration Thin Client 添加到 Eclipse 项目的 Properties->Java Build Path时间>。

在 WebSphere V9 中,这将是文件 WAS_INSTALL_ROOT/runtimes/com.ibm.ws.admin.client_9.0.jar,其他版本类似。

要在启用安全性的情况下运行,您可能需要根据 JDK 设置额外的系统属性和可能的​​额外设置。看这里]( https://www.ibm.com/support/knowledgecenter/SSAW57_9.0.0/com.ibm.websphere.nd.multiplatform.doc/ae/txml_adminclient.html) 了解更多信息。

参考/信用

  • 我从解决方案开始here
  • More 关于开发 Java 管理客户端

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-08
    • 2011-06-07
    • 2012-08-28
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    相关资源
    最近更新 更多