下面列出了javax.naming.directory.InitialDirContext#getAttributes ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Set<String> collectRecords(String dns) {
if (ConfigUtil.getDebugOrFalse()) {
logger.info("Collecting A records for " + dns);
}
Set<String> retVal = new HashSet<>();
try {
InitialDirContext context = new InitialDirContext();
Attributes attributes = context.getAttributes("dns:/" + dns, new String[] { "A" });
NamingEnumeration<?> attributeEnum = attributes.get("A").getAll();
while (attributeEnum.hasMore()) {
retVal.add(attributeEnum.next().toString());
}
} catch (NamingException ex) {
logger.error(ex.getMessage(), ex);
}
if (ConfigUtil.getDebugOrFalse()) {
logger.info("Got " + retVal.size() + " record(s) for " + dns);
}
return retVal;
}
private static Set<String> collectRecords(String dns) {
if (ConfigUtil.getDebugOrFalse()) {
logger.info("Collecting A records for " + dns);
}
Set<String> retVal = new HashSet<>();
try {
InitialDirContext context = new InitialDirContext();
Attributes attributes = context.getAttributes("dns:/" + dns, new String[] { "A" });
NamingEnumeration<?> attributeEnum = attributes.get("A").getAll();
while (attributeEnum.hasMore()) {
retVal.add(attributeEnum.next().toString());
}
} catch (NamingException ex) {
logger.error(ex.getMessage(), ex);
}
if (ConfigUtil.getDebugOrFalse()) {
logger.info("Got " + retVal.size() + " record(s) for " + dns);
}
return retVal;
}
private static Set<String> collectRecords(String dns) {
if (ConfigUtil.getDebugOrFalse()) {
logger.info("Collecting A records for " + dns);
}
Set<String> retVal = new HashSet<>();
try {
InitialDirContext context = new InitialDirContext();
Attributes attributes = context.getAttributes("dns:/" + dns, new String[] { "A" });
NamingEnumeration<?> attributeEnum = attributes.get("A").getAll();
while (attributeEnum.hasMore()) {
retVal.add(attributeEnum.next().toString());
}
} catch (NamingException ex) {
logger.error(ex.getMessage(), ex);
}
if (ConfigUtil.getDebugOrFalse()) {
logger.info("Got " + retVal.size() + " record(s) for " + dns);
}
return retVal;
}
public static String getRevName(String oipAddr) throws NamingException {
String ipAddr = oipAddr;
try {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
InitialDirContext idc = new InitialDirContext(env);
String revName = null;
String[] quads = ipAddr.split("\\.");
//StringBuilder would be better, I know.
ipAddr = "";
for (int i = quads.length - 1; i >= 0; i--) {
ipAddr += quads[i] + ".";
}
ipAddr += "in-addr.arpa.";
Attributes attrs = idc.getAttributes(ipAddr, new String[]{"PTR"});
Attribute attr = attrs.get("PTR");
if (attr != null) {
revName = (String) attr.get(0);
}
return revName;
} catch (Exception e) {
return oipAddr;
}
}
private static List<MXRecord> getMXForDomain(String aDomain) throws NamingException {
final InitialDirContext ctx = new InitialDirContext();
final Attributes attributes = ctx.getAttributes("dns:/" + aDomain,
new String[] { "MX" });
final Attribute attr = attributes.get("MX");
final List<MXRecord> list = new ArrayList<MXRecord>();
// RFC 974
if (attr == null) {
list.add(new MXRecord(0, aDomain));
} else {
// split pref from hostname
for (int i = 0; i < attr.size(); i++) {
Object object = attr.get(i);
if (object != null && object instanceof String) {
String[] split = ((String) object).split("\\s+");
if (split != null && split.length == 2) {
Integer weight = Integer.parseInt(split[0]);
list.add(new MXRecord(weight, split[1]));
}
}
}
}
// sort (according to weight of mxrecord)
Collections.sort(list);
return list;
}
/**
* Perform actual authentication the user
*/
public boolean doAuthenticate(HashMap userProps) throws Exception {
if (ApplicationProperties
.getProperty("tmtbl.authenticate.ldap.provider") == null)
throw new Exception("Ldap provider is not set.");
String principal = ApplicationProperties
.getProperty("tmtbl.authenticate.ldap.principal");
if (principal == null)
throw new Exception("Ldap principal is not set.");
String query = ApplicationProperties
.getProperty("tmtbl.authenticate.ldap.query");
if (query == null)
throw new Exception("Ldap query is not set.");
String n = (String) userProps.get("username");
String p = (String) userProps.get("password");
Hashtable<String, String> env = getEnv();
env.put(Context.SECURITY_PRINCIPAL, principal.replaceAll("%", n));
env.put(Context.SECURITY_CREDENTIALS, p);
InitialDirContext cx = new InitialDirContext(env);
String idAttributeName = ApplicationProperties.getProperty(
"tmtbl.authenticate.ldap.externalId", "uid");
Attributes attributes = cx.getAttributes(query.replaceAll("%", n),
new String[] { idAttributeName });
Attribute idAttribute = attributes.get(idAttributeName);
if (idAttribute != null) {
sLog.debug("Ldap authentication passed ... ");
setAuthSucceeded(true);
iExternalUid = (String) idAttribute.get();
try {
if (iExternalUid != null
&& ApplicationProperties
.getProperty("tmtbl.authenticate.ldap.externalId.format") != null)
iExternalUid = new DecimalFormat(
ApplicationProperties
.getProperty("tmtbl.authenticate.ldap.externalId.format"))
.format(Long.parseLong(iExternalUid));
} catch (NumberFormatException e) {
}
setUser(n);
return true;
}
return false;
}