Glassfish 如何在JNDI查找中存储凭据?
我有一个应用程序,它有几个Glassfish 如何在JNDI查找中存储凭据?,glassfish,jndi,Glassfish,Jndi,我有一个应用程序,它有几个属性文件,我很好奇是否可以更容易地将这些用户名/密码/服务器url存储到JNDI查找中的组合中,而不是将此明文文件存储在.jar中 有几个问题: 您可以使用JNDI存储不是数据库的凭据吗 如何配置新的JNDI资源来存储这些属性 如何从资源中检索这些属性 我阅读的大多数文档都是关于如何为数据库连接设置JNDI的,在我的使用中,我并没有连接到任何数据库,而是使用不同的身份验证方案(请求消息中的SOAP用户/密码、HTTP Basic、标头、SSL等)连接不同类型的web服务
属性
文件,我很好奇是否可以更容易地将这些用户名
/密码
/服务器url存储到JNDI查找中的
组合中,而不是将此明文文件存储在.jar中
有几个问题:
我阅读的大多数文档都是关于如何为数据库连接设置JNDI的,在我的使用中,我并没有连接到任何数据库,而是使用不同的身份验证方案(请求消息中的SOAP用户/密码、HTTP Basic、标头、SSL等)连接不同类型的web服务.您的问题的答案:
PropertiesObjectFactory
类来处理JNDI
java.util.Porperties的属性
例如,属性对象工厂
类可以如下所示:
public class PropertiesObjectFactory implements Serializable, ObjectFactory {
public static final String FILE_PROPERTY_NAME = "org.glassfish.resources.custom.factory.PropertiesFactory.fileName";
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment)
throws Exception {
Reference ref = (Reference) obj;
Enumeration<RefAddr> refAddrs = ref.getAll();
String fileName = null;
Properties fileProperties = new Properties();
Properties properties = new Properties();
while (refAddrs.hasMoreElements()) {
RefAddr addr = refAddrs.nextElement();
String type = addr.getType();
String value = (String) addr.getContent();
if (type.equalsIgnoreCase(FILE_PROPERTY_NAME)) {
fileName = value;
} else {
properties.put(type, value);
}
}
if (fileName != null) {
File file = new File(fileName);
if (!file.isAbsolute()) {
file = new File(System.getProperty("com.sun.aas.installRoot") + File.separator + fileName);
}
try {
if (file.exists()) {
try {
FileInputStream fis = new FileInputStream(file);
if (fileName.toUpperCase().endsWith("XML")) {
fileProperties.loadFromXML(fis);
} else {
fileProperties.load(fis);
}
} catch (IOException ioe) {
throw new IOException("IO Exception during properties load : " + file.getAbsolutePath());
}
} else {
throw new FileNotFoundException("File not found : " + file.getAbsolutePath());
}
} catch (FileNotFoundException fnfe) {
throw new FileNotFoundException("File not found : " + file.getAbsolutePath());
}
}
fileProperties.putAll(properties);
return fileProperties;
}
}
获取属性的示例:
String username = someInstance.getProperties("jndi/credentials").getProperty("testUsernameName");
您的username
将是:testUsernameValue
在应用程序中调用此方法时,请提供在应用程序服务器中配置的JNDI
名称:JNDI/credentials
。如果在部署描述符中映射了资源,则必须使用:java:comp/env/jndi/credentials
如果要使用弹簧执行相同操作
:
<jee:jndi-lookup id="credentials"
jndi-name="jndi/credentials"/>
我希望是这样。希望这能有所帮助。检查此答案我知道此答案很旧,但我希望您仍然知道答案,因为我正在尝试在glassfish4.1上重新创建此答案。我应该使用哪些.jar作为上下文和属性?或者它们是否包含在预定义的包中?
<jee:jndi-lookup id="credentials"
jndi-name="jndi/credentials"/>