`

jndi study

    博客分类:
  • jndi
阅读更多
jndi  jave naming and directory interface java命名和目录接口。主要用于远程访问对象(标准)。

naming service: the means by which names are associated with objects and objects are found based their name, 命名服务,通过名字与对象的联系,我们可以根据名字找到相应的对象
directory service: a directory service associated names with the objects and also allow such objects to have attributes.目录服务,可以说是命名服务的扩展,它不仅联系了对象,并且允许对象拥有属性

java应用程序使用jndi api访问不通的命名和目录服务,jndi提供了访问不同jndi服务标准统一的接口。

几个命名/目录服务的提供者
1 LDAP   lightweight directory access protocol     com.sun.jndi.ldap.LdapCtxFactory
2 CORBA COS  Common Object request broker architecture Common object services
3 RMI    java remote method invocation
4 DNS    Domain name system
com.sun.jndi.dns.DnsContextFactory
5 FSSP   file system service provider              com.sun.jndi.fscontext.RefFSContextFactory

图片中更加可以明白jndi的作用

java spi   server provider interface


Naming operations:
read operations and operations for updating the namespace
Look up an object
List the content of a context
Addind,overwriting,and removing a binding
Renaming an object
Creating and destorying subContexts

Directory operations
Reading a object's attributes
Modifying an object's attributes
Searching a directory
Performing hybrid naming and directory operations

代码,Lookup,通过名字找到找到对象,正如new File("H:/summary.txt");提供名字可以获取File对象。
package ldaptest;

import java.io.File;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author liuquan
 */
public class LdapLookup {
    
    public void lookupObject(){
        Hashtable ht = new Hashtable();
        ht.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
        ht.put(Context.PROVIDER_URL, "file:/H:");
        try {
            Context context = new InitialContext(ht);
            File file = (File)context.lookup("summary.txt");
            System.out.println(file.getAbsolutePath());
        } catch (NamingException ex) {
            Logger.getLogger(LdapLookup.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        LdapLookup lookup = new LdapLookup();
        lookup.lookupObject();
    }
}

使用的是file system service。Hashtable存放了二个参数,一个是service的提供者,另一个是访问路径。

代码list the content of the context,得到context内所有内容。如你访问文件夹,那么可以得到这个文件夹里面所有的内容。有二个方法,一个只得到object名字和class名字,另一种还会得到object对象。
package ldaptest;

import java.io.File;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Binding;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

/**
 *
 * @author liuquan
 */
public class LdapList {

    public void listObject() {
        try {
            Hashtable ht = new Hashtable();
            ht.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
            ht.put(Context.PROVIDER_URL, "file:/H:");
            Context context = new InitialContext(ht);
            NamingEnumeration list = context.list("summary");
            while (list.hasMore()) {
                NameClassPair ncp = (NameClassPair) list.next();
                System.out.println(ncp);
                System.out.println(ncp.getName());
                System.out.println(ncp.getClassName());

            }
        } catch (NamingException ex) {
            Logger.getLogger(LdapList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void listBindingObject() {
        try {
            Hashtable ht = new Hashtable();
            ht.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
            ht.put(Context.PROVIDER_URL, "file:/H:");
            Context context = new InitialContext(ht);
            NamingEnumeration list = context.listBindings("summary");
            while (list.hasMore()) {
                Binding b = (Binding) list.next();
                System.out.println(b);
                System.out.println(b.getObject());
                File file = (File) b.getObject();
                System.out.println(file.getAbsolutePath());
            }
        } catch (NamingException ex) {
            Logger.getLogger(LdapList.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String args[]) {
        LdapList ll = new LdapList();
        ll.listObject();
        ll.listBindingObject();
    }
}
  • 大小: 34.6 KB
分享到:
评论

相关推荐

    Tomcat中JNDI原理

    简单我tomcat5.0中的JNDI应用

    jndi-tool JNDI服务利用工具

    JNDI服务利用工具 RMI/LDAP,支持部分场景回显、内存shell,高版本JDK场景下利用等,fastjson rce命令执行,log4j rce命令执行 漏洞检测辅助工具

    关于JNDI测试项目

    JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试项目JNDI测试...

    hibernate 3.1+tomcat 5.5.x(配置jndi)

    hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+tomcat 5.5.x(配置jndi)hibernate 3.1+...

    jboss配置MySql的JNDI

    jboss配置MySql的JNDI

    jndi-1_2_1.zip_jndi_jndi-1.2.1.jar

    在JAVA编程中对JNDI的支持.是一个开放的源码.

    jndi配置

    jndi配置,jndi配置jndi配置jndi配置jndi配置jndi配置jndi配置jndi配置

    JNDI基础教程课件

    jndi入门学习资料,介绍jndi基本原理,安装和使用,基本配置

    jndi-JNDI-Injection-Exploit

    java asm jndi_JNDI-Injection-Exploit,用于log4j2漏洞验证 可执行程序为jar包,在命令行中运行以下命令: $ java -jar JNDI-Injection-Exploit-1.0-SNAPSHOT-all.jar [-C] [command] [-A] [address] 其中: -C ...

    tomcat jndi数据源解密factory

    自定义jndi数据源factory类,用于解密jndi中的加密数据,解密方式为DES,具体可根据实际需求修改。

    Windows_7_下搭建LDAP服务器并使用JNDI

    Windows_7_下搭建LDAP服务器并使用JNDI Windows_7_下搭建LDAP服务器并使用JNDI Windows_7_下搭建LDAP服务器并使用JNDI

    jndi jndi jndi

    jndi.jndi.jndi.jndi.jndi.jndi.jndi.jndi.jndi.

    JNDI Java操作示例

    JNDI java操作示例,JNDI java操作示例

    jndi所依赖的jar包

    jndi所依赖的jar包,fscontext.jar和providerutil.jar,jndi.jar 将jndi.jar复制到%JAVA_HOME%\jre\lib\ext目录下就可得到持久的扩展

    JNDI配置详细介绍

    tomcat的JNDI配置详细介绍 介绍详细,思路清晰

    hibernate中jndi的配置使用

    配置了tomcat之后发现jndi好简单啊,可是碰到了hibernate该怎么做呢,本例详细解析

    JNDI配置原理详解.doc

    JNDI配置原理详解 JNDI配置原理详解.doc

    tomcat8 JNDI数据源加密

    TOMCAT8 JNDI对用户名和密码加密

    JNDI配置方法详解

    JNDI(Java Naming and Directory Interface)是SUN公司提供的一种标准的Java命名系统接口,JNDI提供统一的客户端API,通过不同的访问提供者接口JNDI SPI的实现,由管理者将JNDI API映射为特定的命名服务和目录系统,...

Global site tag (gtag.js) - Google Analytics