如何在JNDI查找中存储凭据?

时间:2012-10-17 18:19:25

标签: glassfish jndi

我有一个包含多个properties个文件的应用,我很好奇在JNDI中存储这些username / password / server-url-to-login-to个组合是否更容易在.jar中查找而不是此明文文件。

有几个问题:

  1. 您可以使用JNDI存储非数据库的凭据吗?
  2. 如何配置新的JNDI资源来存储这些属性?
  3. 您如何从资源中检索这些属性?
  4. 我读到的大多数文档都是如何为数据库连接设置JNDI,而对于我的使用,我没有连接到任何数据库,而是连接到使用不同身份验证的不同类型的Web服务方案(请求消息中的SOAP用户/密码,HTTP Basic,标头,SSL等)。

1 个答案:

答案 0 :(得分:3)

您的问题的答案:

  
      
  1. 您可以使用JNDI存储非数据库的凭据吗?
  2.   

  
      
  1. 如何配置新的JNDI资源来存储这些属性?
  2.   

如果您使用的是Glassfish 2,则必须创建自定义PropertiesObjectFactory类来处理JNDI的{​​{1}}属性。

例如java.util.Porperties类看起来像这样:

PropertiesObjectFactory

创建该类的jar,将其添加到glassfish全局类路径中。它将是: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; } } 然后您可以在/glassfish/domains/domain1/lib属性配置中将其指定为工厂类。

Glassfish 3已经拥有属性工厂类。它设置为:JNDI

打开glassfish管理控制台并导航至:Resources - &gt; JNDI - &gt;自定义资源,单击“新建”,提供JNDI名称,例如:org.glassfish.resources.custom.factory.PropertiesFactory,选择资源类型jndi/credentials,指定工厂类:java.util.Properties,然后单击“添加属性”,指定例如:org.glassfish.resources.custom.factory.PropertiesFactory和值列testUsernameName的名称。单击“确定”即可,您已配置testUsernameValue资源。您可以根据需要添加任意数量的属性:JNDI资源。

完成资源创建后,不要忘记重新启动应用服务器。

  
      
  1. 您如何从资源中检索这些属性?
  2.   
jndi/credentials

如何获取属性的示例:

public Properties getProperties(String jndiName) {
    Properties properties = null;
    try {
        InitialContext context = new InitialContext();
        properties = (Properties) context.lookup(jndiName);
        context.close();
    } catch (NamingException e) {
        LOGGER.error("Naming error occurred while initializing properties from JNDI.", e);
        return null;
    }
    return properties;
}

您的String username = someInstance.getProperties("jndi/credentials").getProperty("testUsernameName"); 将是:username

在应用程序中调用此方法时,请提供您在应用程序服务器中配置的testUsernameValue名称:JNDI。如果您在部署描述符中映射了资源,则必须使用:jndi/credentials

如果您想使用java:comp/env/jndi/credentials

执行相同操作
Spring

好吧,我希望如此。希望这会有所帮助。