我使用以下代码(see this SO post)来读取在WAS 7服务器上存储为JC2 Alias的userId和密码。
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-&gt; Debug As。 (我使用Run-&gt; Debug As-&gt; Debug配置来配置进程)。我收到错误“java.lang.NullPointerException:WSMappingCallbackHandlerFactory not initialized”。我已经逐步完成了有效的代码,并且无法看到代码中的值没有任何区别。我怀疑我可能需要在调试配置中修改构建路径,但我不知道要改变什么。
编辑:
我认为我没有很好地解释过这种情况。代码在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
我想添加代码以将userId和Password读取到作为批处理作业调用的代码中。通常,要调试称为批处理作业的代码,我们使用调试配置,服务器不必运行。我可以设置断点并逐步执行代码,直到我到达callbackHandler
行。
答案 0 :(得分:2)
您无法在服务器外部运行的客户端使用相同的API。
要在Java中执行此操作(与wsadmin / Jython相反,这可能是另一种方法),您可以从以下代码开始:
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项目的属性 - &gt; Java构建路径。
在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)了解更多信息。