本文整理匯總了Java中javax.naming.directory.InitialDirContext類的典型用法代碼示例。如果您正苦於以下問題:Java InitialDirContext類的具體用法?Java InitialDirContext怎麽用?Java InitialDirContext使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
InitialDirContext類屬於javax.naming.directory包,在下文中一共展示了InitialDirContext類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: dnFromUser
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
private static String dnFromUser(String username) throws NamingException {
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
props.put(Context.PROVIDER_URL, "ldap://ldap.example.com");
props.put(Context.REFERRAL, "ignore");
InitialDirContext context = new InitialDirContext(props);
SearchControls ctrls = new SearchControls();
ctrls.setReturningAttributes(new String[]{"givenName", "sn"});
ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> answers = context.search("dc=People,dc=example,dc=com", "(uid=" + username + ")", ctrls);
SearchResult result = answers.next();
return result.getNameInNamespace();
}
示例2: getDnsAttributes
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
String getDnsAttributes(String ip) {
try {
Hashtable<String, String> env = new Hashtable<>();
env.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory");
// TODO don't specify ws1, instead use ns servers for s.maxmind.com
env.put("java.naming.provider.url", "dns://ws1.maxmind.com/");
DirContext ictx = new InitialDirContext(env);
Attributes attrs = ictx.getAttributes(licenseKey + "." + ip
+ ".s.maxmind.com", new String[] { "txt" });
// System.out.println(attrs.get("txt").get());
String str = attrs.get("txt").get().toString();
return str;
} catch (NamingException e) {
// TODO fix this to handle exceptions
System.out.println("DNS error");
return null;
}
}
示例3: LdapUtil
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
public LdapUtil(String csUserId, String csPassword, String csServer)
{
try
{
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://"+csServer+"/");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, csUserId);
env.put(Context.SECURITY_CREDENTIALS, csPassword);
m_ctx = new InitialDirContext(env);
}
catch (NamingException e)
{
e.printStackTrace();
m_ctx = null ;
}
}
示例4: activateNis
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
/**
* This seems to be required for objectClass posixGroup.
*/
private ApacheDS activateNis() throws Exception {
Preconditions.checkState(ldapServer.isStarted());
Attribute disabled = new BasicAttribute("m-disabled", "TRUE");
Attribute disabled2 = new BasicAttribute("m-disabled", "FALSE");
ModificationItem[] mods = new ModificationItem[] {
new ModificationItem(DirContext.REMOVE_ATTRIBUTE, disabled),
new ModificationItem(DirContext.ADD_ATTRIBUTE, disabled2)
};
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, getUrl());
DirContext ctx = new InitialDirContext(env);
ctx.modifyAttributes("cn=nis,ou=schema", mods);
return this;
}
示例5: getMockedLDAPSearchResult
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
private LDAPInitialDirContextFactoryImpl getMockedLDAPSearchResult(boolean withEmail) throws NamingException
{
@SuppressWarnings("unchecked")
NamingEnumeration<SearchResult> mockedNamingEnumeration = mock(NamingEnumeration.class);
when(mockedNamingEnumeration.hasMore()).thenReturn(true).thenReturn(false);
BasicAttributes attributes = new BasicAttributes();
attributes.put(new BasicAttribute("sAMAccountName", "U1"));
attributes.put(new BasicAttribute("givenName", "U1"));
if (withEmail)
{
attributes.put(new BasicAttribute("mail", "[email protected]"));
}
SearchResult mockedSearchResult = new SearchResult("CN:U1", null, attributes);
mockedSearchResult.setNameInNamespace("CN:U1");
when(mockedNamingEnumeration.next()).thenReturn(mockedSearchResult);
InitialDirContext mockedInitialDirContext = mock(InitialDirContext.class);
when(mockedInitialDirContext.search(any(String.class), any(String.class), any(SearchControls.class))).thenReturn(mockedNamingEnumeration);
LDAPInitialDirContextFactoryImpl mockedLdapInitialDirContextFactory = mock(LDAPInitialDirContextFactoryImpl.class);
when(mockedLdapInitialDirContextFactory.getDefaultIntialDirContext(0)).thenReturn(mockedInitialDirContext);
return mockedLdapInitialDirContextFactory;
}
示例6: getUserForLoginName
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
public User getUserForLoginName(String login) {
try {
InitialDirContext ctx = createContext();
User user = new User();
SearchResult next = getLDAPInformation(ctx, login.toLowerCase()).nextElement();
user.setLogin(login.toLowerCase());
user.setSurname(next.getAttributes().get("sn").get().toString());
user.setForename(next.getAttributes().get("givenName").get().toString());
user.setEmail(next.getAttributes().get("mail").get().toString().toLowerCase());
ctx.close();
return user;
} catch (Exception e) {
log.info("Login " + login + " nicht gefunden!");
}
return null;
}
示例7: getServerAddress
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
/**
* Returns a server's address and port for the specified hostname, looking up the SRV record if possible
*/
private static String[] getServerAddress(String p_78863_0_)
{
try
{
String s = "com.sun.jndi.dns.DnsContextFactory";
Class.forName("com.sun.jndi.dns.DnsContextFactory");
Hashtable hashtable = new Hashtable();
hashtable.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
hashtable.put("java.naming.provider.url", "dns:");
hashtable.put("com.sun.jndi.dns.timeout.retries", "1");
DirContext dircontext = new InitialDirContext(hashtable);
Attributes attributes = dircontext.getAttributes("_minecraft._tcp." + p_78863_0_, new String[] {"SRV"});
String[] astring = attributes.get("srv").get().toString().split(" ", 4);
return new String[] {astring[3], astring[2]};
}
catch (Throwable var6)
{
return new String[] {p_78863_0_, Integer.toString(25565)};
}
}
示例8: getDirContext
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
public DirContext getDirContext() throws NamingException {
Hashtable<String,String> env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ctxFactory","com.sun.jndi.ldap.LdapCtxFactory"));
env.put(Context.PROVIDER_URL, ApplicationProperties.getProperty("tmtbl.authenticate.ldap.provider"));
env.put(Context.REFERRAL, ApplicationProperties.getProperty("tmtbl.authenticate.ldap.referral","ignore"));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.version")!=null)
env.put("java.naming.ldap.version", ApplicationProperties.getProperty("tmtbl.authenticate.ldap.version"));
env.put(Context.SECURITY_AUTHENTICATION, ApplicationProperties.getProperty("tmtbl.authenticate.ldap.security","simple"));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.socketFactory")!=null)
env.put("java.naming.ldap.factory.socket",ApplicationProperties.getProperty("tmtbl.authenticate.ldap.socketFactory"));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.keyStore")!=null)
System.setProperty("javax.net.ssl.keyStore", ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.keyStore").replaceAll("%WEB-INF%", ApplicationProperties.getBasePath()));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStore")!=null)
System.setProperty("javax.net.ssl.trustStore", ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStore").replaceAll("%WEB-INF%", ApplicationProperties.getBasePath()));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStorePassword")!=null)
System.setProperty("javax.net.ssl.keyStorePassword", ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.keyStorePassword"));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStorePassword")!=null)
System.setProperty("javax.net.ssl.trustStorePassword", ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStorePassword"));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStoreType")!=null)
System.setProperty("javax.net.ssl.trustStoreType", ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStoreType"));
return new InitialDirContext(env);
}
示例9: getDirContext
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
public DirContext getDirContext() throws NamingException {
Hashtable<String,String> env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ctxFactory","com.sun.jndi.ldap.LdapCtxFactory"));
env.put(Context.PROVIDER_URL, ApplicationProperties.getProperty("tmtbl.authenticate.ldap.provider"));
env.put(Context.REFERRAL, ApplicationProperties.getProperty("tmtbl.authenticate.ldap.referral","ignore"));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.version")!=null)
env.put("java.naming.ldap.version", ApplicationProperties.getProperty("tmtbl.authenticate.ldap.version"));
env.put(Context.SECURITY_AUTHENTICATION, ApplicationProperties.getProperty("tmtbl.authenticate.ldap.security","simple"));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.socketFactory")!=null)
env.put("java.naming.ldap.factory.socket",ApplicationProperties.getProperty("tmtbl.authenticate.ldap.socketFactory"));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.keyStore")!=null)
System.setProperty("javax.net.ssl.keyStore", ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.keyStore").replaceAll("%WEB-INF%", ApplicationProperties.getBasePath()));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStore")!=null)
System.setProperty("javax.net.ssl.trustStore", ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStore").replaceAll("%WEB-INF%", ApplicationProperties.getBasePath()));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStorePassword")!=null)
System.setProperty("javax.net.ssl.keyStorePassword", ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.keyStorePassword"));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStorePassword")!=null)
System.setProperty("javax.net.ssl.trustStorePassword", ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStorePassword"));
if (ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStoreType")!=null)
System.setProperty("javax.net.ssl.trustStoreType", ApplicationProperties.getProperty("tmtbl.authenticate.ldap.ssl.trustStoreType"));
return new InitialDirContext(env);
}
示例10: openContext
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
/**
* Open (if necessary) and return a connection to the configured
* directory server for this Realm.
*
* @throws NamingException if a directory server error occurs
*/
private DirContext openContext() throws NamingException {
if (log.isDebugEnabled()) {
log.debug("opening context...");
}
final Hashtable environment = makeDirectoryContextEnvironment();
if (log.isDebugEnabled()) {
log.debug("environment: " + environment);
}
//noinspection UnnecessaryLocalVariable
final InitialDirContext initialDirContext = new InitialDirContext(environment);
if (log.isDebugEnabled()) {
log.debug("initialDirContext: " + initialDirContext);
}
return initialDirContext;
}
示例11: authenticate
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
static boolean authenticate(String username, String password) {
try {
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
props.put(Context.PROVIDER_URL, "ldap://ldap.example.com");
props.put(Context.REFERRAL, "ignore");
props.put(Context.SECURITY_PRINCIPAL, dnFromUser(username));
props.put(Context.SECURITY_CREDENTIALS, password);
new InitialDirContext(props);
return true;
} catch (NamingException e) {
return false;
}
}
示例12: getServerAddress
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
/**
* Returns a server's address and port for the specified hostname, looking up the SRV record if possible
*/
private static String[] getServerAddress(String p_78863_0_)
{
try
{
String s = "com.sun.jndi.dns.DnsContextFactory";
Class.forName("com.sun.jndi.dns.DnsContextFactory");
Hashtable<String, String> hashtable = new Hashtable();
hashtable.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
hashtable.put("java.naming.provider.url", "dns:");
hashtable.put("com.sun.jndi.dns.timeout.retries", "1");
DirContext dircontext = new InitialDirContext(hashtable);
Attributes attributes = dircontext.getAttributes("_minecraft._tcp." + p_78863_0_, new String[] {"SRV"});
String[] astring = attributes.get("srv").get().toString().split(" ", 4);
return new String[] {astring[3], astring[2]};
}
catch (Throwable var6)
{
return new String[] {p_78863_0_, Integer.toString(25565)};
}
}
示例13: getURLContext
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
public static Context getURLContext(
String scheme, Hashtable<?,?> environment)
throws NamingException {
return new InitialDirContext() {
public Attributes getAttributes(String name, String[] attrIds)
throws NamingException {
return new BasicAttributes() {
public Attribute get(String attrID) {
BasicAttribute ba = new BasicAttribute(attrID);
ba.add("1 1 99 b.com.");
ba.add("0 0 88 a.com."); // 2nd has higher priority
return ba;
}
};
}
};
}
示例14: isAuthenticed
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
/**
* Checks if is authenticed.
*
* @param host
* the host
* @param port
* the port
* @param userName
* the user name
* @param password
* the password
* @return true, if is authenticed
* @throws NamingException
* the naming exception
*/
public static boolean isAuthenticed(String host, int port, String userName, String password) throws NamingException {
log.info("isAuthenticed");
// Set up the environment for creating the initial context
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://" + host + ":" + port);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, userName + "@" + host);
log.info(env.toString());
env.put(Context.SECURITY_CREDENTIALS, password);
// Create the initial context
DirContext ctx = new InitialDirContext(env);
log.info("DirContext Init Succ");
boolean result = ctx != null;
if (ctx != null) {
log.info("Closing DirContext");
ctx.close();
}
return result;
}
示例15: createLdapDataSource
import javax.naming.directory.InitialDirContext; //導入依賴的package包/類
@Override
public DataSource createLdapDataSource(Properties loginProperties, String ldapName) throws NamingException
{
DataSource ds;
// borisv: this code stores all parameters in static baseEnvironment. So if you have multiple connections, next connection will get parameters from previous (unless overwriten by loginParamters).
loginProperties.put(USE_JNDI_JDBC_CONNECTION_POOL_KEY, "false");
if (loginProperties.getProperty(JAVA_NAMING_FACTORY_INITIAL) == null)
{
loginProperties.put(JAVA_NAMING_FACTORY_INITIAL, "com.sun.jndi.ldap.LdapCtxFactory");
}
InitialDirContext context = new JdbcInitialDirContext(loginProperties);
Enumeration propKeys = loginProperties.keys();
while(propKeys.hasMoreElements())
{
Object key = propKeys.nextElement();
context.addToEnvironment((String)key, loginProperties.get(key));
}
ds = (DataSource) context.lookup(ldapName);
return ds;
}