繁体   English   中英

如何从在服务器外部运行的 Java 客户端访问 WebSphere 身份验证别名信息?

[英]How can I access WebSphere authentication alias info from a Java client running outside of the server?

我使用以下代码(请参阅此 SO 帖子)读取在我的 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 和密码的代码添加到称为批处理作业的代码中。 通常要调试作为批处理作业调用的代码,我们使用 Debug Configuration 并且服务器不必运行。 我可以设置断点并逐步执行代码,直到我到达callbackHandler行为止。

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

您不能像在服务器内运行一样,从在服务器外运行的客户端使用相同的 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;
            }
        }
    }
}

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

在最简单的情况下(没有安全性,这可能对入门有用),您可以简单地将管理瘦客户端添加到 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 )。

参考/信用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM