usefactory.java

来自「JAVA 工作指南 可以说是程序员必备的东西哦」· Java 代码 · 共 45 行

JAVA
45
字号
import javax.naming.*;import javax.naming.directory.*;import java.util.Hashtable;/** * Demonstrates how to create an initial context to an LDAP server * using a custom socket factory. Requires use of the CustomSocketFactory class. * * usage: java UseFactory */class UseFactory {    public static void main(String[] args) {	// Set up environment for creating initial context        Hashtable<String, Object> env = new Hashtable<String, Object>(11);	env.put(Context.INITIAL_CONTEXT_FACTORY, 	    "com.sun.jndi.ldap.LdapCtxFactory");	env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");	// Specify the socket factory	env.put("java.naming.ldap.factory.socket", "CustomSocketFactory");	// Authenticate as S. User and password "mysecret"	env.put(Context.SECURITY_AUTHENTICATION, "simple");	env.put(Context.SECURITY_PRINCIPAL, 	    "cn=S. User, ou=NewHires, o=JNDITutorial");	env.put(Context.SECURITY_CREDENTIALS, "mysecret");	try {	    // Create initial context	    DirContext ctx = new InitialDirContext(env);	    System.out.println(ctx.lookup("ou=NewHires"));	    // ... do something useful with ctx	    // Close the context when we're done	    ctx.close();	} catch (NamingException e) {	    e.printStackTrace();	}    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?