java active directory 单点登录_在Linux上使用Java对Active Directory进行身份验证

这是我根据此博客中的示例LINK和此来源LINK整理的代码。

import com.sun.jndi.ldap.LdapCtxFactory;

import java.util.ArrayList;

import java.util.Hashtable;

import java.util.List;

import java.util.Iterator;

import javax.naming.Context;

import javax.naming.AuthenticationException;

import javax.naming.NamingEnumeration;

import javax.naming.NamingException;

import javax.naming.directory.Attribute;

import javax.naming.directory.Attributes;

import javax.naming.directory.DirContext;

import javax.naming.directory.SearchControls;

import javax.naming.directory.SearchResult;

import static javax.naming.directory.SearchControls.SUBTREE_SCOPE;

class App2 {

public static void main(String[] args) {

if (args.length != 4 && args.length != 2) {

System.out.println("Purpose: authenticate user against Active Directory and list group membership.");

System.out.println("Usage: App2 ");

System.out.println("Short usage: App2 ");

System.out.println("(short usage assumes 'xyz.tld' as domain and 'abc' as server)");

System.exit(1);

}

String domainName;

String serverName;

if (args.length == 4) {

domainName = args[2];

serverName = args[3];

} else {

domainName = "xyz.tld";

serverName = "abc";

}

String username = args[0];

String password = args[1];

System.out

.println("Authenticating " + username + "@" + domainName + " through " + serverName + "." + domainName);

// bind by using the specified username/password

Hashtable props = new Hashtable();

String principalName = username + "@" + domainName;

props.put(Context.SECURITY_PRINCIPAL, principalName);

props.put(Context.SECURITY_CREDENTIALS, password);

DirContext context;

try {

context = LdapCtxFactory.getLdapCtxInstance("ldap://" + serverName + "." + domainName + '/', props);

System.out.println("Authentication succeeded!");

// locate this user's record

SearchControls controls = new SearchControls();

controls.setSearchScope(SUBTREE_SCOPE);

NamingEnumeration renum = context.search(toDC(domainName),

"(& (userPrincipalName=" + principalName + ")(objectClass=user))", controls);

if (!renum.hasMore()) {

System.out.println("Cannot locate user information for " + username);

System.exit(1);

}

SearchResult result = renum.next();

List groups = new ArrayList();

Attribute memberOf = result.getAttributes().get("memberOf");

if (memberOf != null) {// null if this user belongs to no group at all

for (int i = 0; i < memberOf.size(); i++) {

Attributes atts = context.getAttributes(memberOf.get(i).toString(), new String[] { "CN" });

Attribute att = atts.get("CN");

groups.add(att.get().toString());

}

}

context.close();

System.out.println();

System.out.println("User belongs to: ");

Iterator ig = groups.iterator();

while (ig.hasNext()) {

System.out.println("   " + ig.next());

}

} catch (AuthenticationException a) {

System.out.println("Authentication failed: " + a);

System.exit(1);

} catch (NamingException e) {

System.out.println("Failed to bind to LDAP / get account information: " + e);

System.exit(1);

}

}

private static String toDC(String domainName) {

StringBuilder buf = new StringBuilder();

for (String token : domainName.split("\\.")) {

if (token.length() == 0)

continue; // defensive check

if (buf.length() > 0)

buf.append(",");

buf.append("DC=").append(token);

}

return buf.toString();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值