我从互联网上获得了以下示例,以使用 JNDI 测试 GSSAPI。
public class GssExample {
public static void main(String[] args) {
System.setProperty("java.security.auth.login.config", "C:\\gssapi_jaas.conf");
System.setProperty("java.security.krb5.conf", "C:\\krb5.conf");
LoginContext lc = null;
try {
lc = new LoginContext(GssExample.class.getName(),
new LoginCallBackHandler());
lc.login();
} catch (LoginException le) {
}
Subject.doAs(lc.getSubject(), new JndiAction(args));
}
}
class JndiAction implements java.security.PrivilegedAction {
public Object run() {
performJndiOperation(args);
return null;
}
private static void performJndiOperation(String[] args) {
Hashtable<String, String> env = new Hashtable<String, String>(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL,
"ldap://XXXX.domain.lab:389/OU=StackOverFlow,dc=domain,dc=lab");
env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI");
env.put("javax.security.sasl.server.authentication", "true");
try {
DirContext ctx = new InitialDirContext(env);
// Fails here with exception as shown below
NamingEnumeration<SearchResult> _result = ctx.search("DC=domain,DC=lab","(objectClass=User)", null);
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
我得到了例外
javax.naming.NameNotFoundException:[LDAP:错误代码 32 - 0000208D:NameErr:DSID-03100241,问题 2001(NO_OBJECT),数据 0,最佳匹配:'OU=StackOverFlow,DC=domain,DC=lab'
请注意: OU 存在于 AD 系统中。
代码LoginCallBackHandler
如下
class LoginCallBackHandler implements CallbackHandler {
@Override
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
if(callbacks != null && callbacks.length > 0) {
if(callbacks[0] instanceof NameCallback) {
((NameCallback)(callbacks[0])).setName("User@DOMAIN.LAB");
}
else if(callbacks[0] instanceof PasswordCallback) {
((PasswordCallback)(callbacks[0])).setPassword("Password".toCharArray());
}
}
}
}
我错过了什么还是我做错了什么?