如何在JNDI查找中存储凭据? [英] How can you store credentials in a JNDI lookup?

查看:99
本文介绍了如何在JNDI查找中存储凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个属性文件的应用程序,我很好奇这些用户名 / 密码 / server-url-to-login-to 组合,而不是明文文件在.jar中。



有几个问题:


  1. 您使用JNDI存储不是数据库的证书?

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

  3. 如何检索这些属性来自资源?

我读过的大部分文档是如何为数据库连接设置JNDI以及如何使用,我没有连接到任何数据库,而是使用不同的认证方案(请求消息中的SOAP用户/密码,HTTP基本,标头,SSL等)的不同种类的Web服务。

解决方案

您的问题的答案:



  1. 您可以使用JNDI存储不是数据库的凭证吗?


是。



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


如果您使用Glassfish 2,您必须创建自定义 PropertiesObjectFactory 类来处理 JNDI 属性 java.util.Porperties



例如 PropertiesObjectFactory 类可能如下所示:

  public class PropertiesObjectFactory实现Serializable, ObjectFactory {

public static final String FILE_PROPERTY_NAME =org.glassfish.resources.custom.factory.PropertiesFactory.fileName;
$ b $ public Object getObjectInstance(Object obj,Name name,Context nameCtx,Hashtable< ?,?> environment)
throws Exception {
Reference ref =(Reference)obj;
枚举< RefAddr> refAddrs = ref.getAll();

String fileName = null;
属性fileProperties = new Properties();
属性properties = new Properties(); $(b)b
while(refAddrs.hasMoreElements()){
RefAddr addr = refAddrs.nextElement();
String type = addr.getType();
字符串值=(String)addr.getContent();

if(type.equalsIgnoreCase(FILE_PROPERTY_NAME)){
fileName = value;
} else {
properties.put(type,value); (fileName!= null){
File file = new File(fileName);
}
}

if
if(!file.isAbsolute()){
file = new File(System.getProperty(com.sun.aas.installRoot)+ File.separator + fileName); (file.exists()){
try {
FileInputStream fis = new FileInputStream(file);
}
try {
if
if(fileName.toUpperCase()。endsWith(XML)){
fileProperties.loadFromXML(fis);
} else {
fileProperties.load(fis);
}
} catch(IOException ioe){
throw new IOException(IO Exception in 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);
返回fileProperties;




$ b $ p
$ b

创建一个该类的jar,将其添加到glassfish中全局类路径。这将是: / glassfish / domains / domain1 / lib 然后你可以在你的 JNDI 属性配置。



Glassfish 3已经拥有属性工厂类。它被设置为: org.glassfish.resources.custom.factory.PropertiesFactory



打开glassfish管理控制台和导航到:Resources - > JNDI - > Custom Resources,点击New,提供一个JNDI名称,例如: jndi / credentials ,选择一个资源类型 java.util.Properties ,指定工厂类: org.glassfish.resources.custom.factory.PropertiesFactory ,然后单击添加属性 ,为例如: testUsernameName 和值列 testUsernameValue 指定名称。点击OK,就完成了,你已经配置了 JNDI 资源。您可以添加尽可能多的属性,如下所示: jndi / credentials 资源。



不要忘记重新启动应用服务器。



  1. 如何从资源中检索这些属性?




  public属性getProperties(String jndiName){
属性properties = null;
尝试{
InitialContext context = new InitialContext();
properties =(Properties)context.lookup(jndiName);
context.close();
} catch(NamingException e){
LOGGER.error(从JNDI初始化属性时发生了命名错误。,e);
返回null;
}
返回属性;
}

示例如何获取属性:

 字符串username = someInstance.getProperties(jndi / credentials)。getProperty(testUsernameName); 

您的用户名应该是: testUsernameValue



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



如果你想用 Spring 来做同样的事情:

 < jee:jndi-lookup id =credentials
jndi-name =jndi / credentials/>

好吧,我希望这就是它。希望这有助于。


I have an app with several properties files, and I was curious if it'd be easier to store these username/password/server-url-to-login-to combos in a JNDI lookup instead of this clear text file in a .jar.

There are a couple questions:

  1. Can you store credentials that aren't databases using JNDI?
  2. How do you configure a new JNDI resource to store these properties?
  3. How would you retrieve these properties from the resource?

Most of the documentation I read was how to set up JNDI for database connections, and for my use, I'm not connecting to any databases, but instead different kinds of web services using different authentication schemes (SOAP user/password in request message, HTTP Basic, headers, SSL, etc.).

解决方案

The answers to your questions:

  1. Can you store credentials that aren't databases using JNDI?

Yes.

  1. How do you configure a new JNDI resource to store these properties?

If you are using Glassfish 2 you have to create your custom PropertiesObjectFactory class to handle JNDI properties of java.util.Porperties.

For e.g. PropertiesObjectFactory class could look like this:

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;
    }
}

Make a jar of that class, add it into glassfish global classpath. It would be: /glassfish/domains/domain1/lib and then you can specify it as a factory class in your JNDI properties configuration.

Glassfish 3 already have properties factory class. It is set into: org.glassfish.resources.custom.factory.PropertiesFactory.

Open glassfish admin console and navigate to: Resources -> JNDI -> Custom Resources, click "New", provide a JNDI name, for e.g: jndi/credentials, choose a resource type java.util.Properties, specify Factory class: org.glassfish.resources.custom.factory.PropertiesFactory, then click "Add property", specify name for e.g: testUsernameName and in value column testUsernameValue. Click OK and that is all, you have configured JNDI resource. You can add as many properties as you like to this: jndi/credentials resource.

Don't forget to restart app server when you done creating resources.

  1. How would you retrieve these properties from the resource?

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;
}

Example how to get the property:

String username = someInstance.getProperties("jndi/credentials").getProperty("testUsernameName"); 

Your username would be: testUsernameValue.

When you call this method in your application provide a JNDI name you configured in your application server: jndi/credentials. If you have mapped resources in deployment descriptor you have to use: java:comp/env/jndi/credentials.

If you want to do the same using Spring:

<jee:jndi-lookup id="credentials"
                 jndi-name="jndi/credentials"/>

Well I hope thats it. Hope this helps.

这篇关于如何在JNDI查找中存储凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆