javax.naming.AuthenticationException [英] javax.naming.AuthenticationException

查看:267
本文介绍了javax.naming.AuthenticationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个上下文的ActiveDirectory(客户端和服务器两个窗口),使用我的Windows凭据,使用NTLM。

I'm trying to create a context for ActiveDirectory (client and server are both windows), using my Windows credentials with NTLM.

这是我的code:

public void func() {
    try {
      URL configURL = getClass().getResource("jaas_ntlm_configuration.txt");
        System.setProperty("java.security.auth.login.config", configURL.toString());

        // If the application is run on NT rather than Unix, use this name
        String loginAppName = "MyConfig";

        // Create login context
        LoginContext lc = new LoginContext(loginAppName, new SampleCallbackHandler());

        // Retrieve the information on the logged-in user
        lc.login();

        // Get the authenticated subject
        Subject subject = lc.getSubject();

        System.out.println(subject.toString());

        Subject.doAs(subject, new JndiAction(new String[] { "" }));
    }
    catch (LoginException e) {
      e.printStackTrace();
    }
}

class JndiAction implements java.security.PrivilegedAction {
    private String[] args;

    public JndiAction(String[] origArgs) {
        this.args = (String[])origArgs.clone();
    }

    public Object run() {
        performJndiOperation(args);
        return null;
    }

    private static void performJndiOperation(String[] args) {

        // Set up environment for creating initial context
        Hashtable env = new Hashtable(11);

        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

        // Must use fully qualified hostname
        env.put(Context.PROVIDER_URL, "ldap://server:389");

        // Request the use of the "GSSAPI" SASL mechanism
        // Authenticate by using already established Kerberos credentials
        env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI");

        try {
            /* Create initial context */
//          DirContext ctx = new InitialDirContext(env);

              // Create the initial context
            DirContext ctx = new InitialLdapContext(env, null);


            // Close the context when we're done
            ctx.close();
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}

和我jaas_ntlm_configuration.txt文件包括:

And my jaas_ntlm_configuration.txt file contains:

MyConfig { com.sun.security.auth.module.Krb5LoginModule required
useTicketCache=true
doNotPrompt=false;
};

当我试图启动的背景下,我发现了以下异常:

When I'm trying to initiate the context, i'm getting the following exception:

javax.naming.AuthenticationException: GSSAPI [Root exception is javax.security.sasl.SaslException: Final handshake failed [Caused by GSSException: Token had invalid integrity check (Mechanism level: Corrupt checksum in Wrap token)]]
        at com.sun.jndi.ldap.sasl.LdapSasl.saslBind(Unknown Source)
        at com.sun.jndi.ldap.LdapClient.authenticate(Unknown Source)
        at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
        at com.sun.jndi.ldap.LdapCtx.<init>(Unknown Source)
        at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(Unknown Source)
        at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(Unknown Source)
        at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(Unknown Source)
        at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(Unknown Source)
        at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
        at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
        at javax.naming.InitialContext.init(Unknown Source)
        at javax.naming.ldap.InitialLdapContext.<init>(Unknown Source)
        at JndiAction.performJndiOperation(JndiAction.java:204)
        at JndiAction.run(JndiAction.java:181)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Unknown Source)
        at MyTest.Do(MyTest.java:59)
        at MyTest.main(MyTest.java:68)
Caused by: javax.security.sasl.SaslException: Final handshake failed [Caused by GSSException: Token had invalid integrity check (Mechanism level: Corrupt checksum in Wrap token)]
        at com.sun.security.sasl.gsskerb.GssKrb5Client.doFinalHandshake(Unknown Source)
        at com.sun.security.sasl.gsskerb.GssKrb5Client.evaluateChallenge(Unknown Source)
        ... 18 more
Caused by: GSSException: Token had invalid integrity check (Mechanism level: Corrupt checksum in Wrap token)
        at sun.security.jgss.krb5.WrapToken_v2.getData(Unknown Source)
        at sun.security.jgss.krb5.WrapToken_v2.getData(Unknown Source)
        at sun.security.jgss.krb5.Krb5Context.unwrap(Unknown Source)
        at sun.security.jgss.GSSContextImpl.unwrap(Unknown Source)
        ... 20 more

一个人可以帮我解决这个问题?

Can someone help me with this issue?

推荐答案

您实际上是使用Kerberos身份验证.. 如果这是你的意思做,我可以告诉你我是如何设法得到它的工作:

You are actually using a Kerberos authentication.. If this is what you mean to do, I can tell you how I managed to get it working :

- add somewhere a file called krb5.conf with inside :

[libdefaults]
default_realm = YOUR_REALM
default_tkt_enctypes = arcfour-hmac-md5
default_tgs_enctypes = arcfour-hmac-md5
permitted_enctypes = arcfour-hmac-md5

dns_lookup_kdc = true
dns_lookup_realm = false

[realms]
YOUR_REALM = {
kdc = KERBEROS_SERVER
default_domain = YOUR_REALM
}

  • 添加此行到code:

    • add this lines to your code :

      System.setProperty(java.security.krb5.conf,PATH_TO_KRB5CONF_FILE); System.setProperty(sun.security.krb5.principal,PRINCIPAL_NAME_WITHOUT_DOMAIN);

      System.setProperty("java.security.krb5.conf",PATH_TO_KRB5CONF_FILE); System.setProperty("sun.security.krb5.principal", PRINCIPAL_NAME_WITHOUT_DOMAIN);

      如果你不知道你的Kerberos服务器,你不能从命令行启动klist输出,并采取谁使用LDAP协议的服务(LDAP /服务器@域 - >服务器)。

      If you don't know your Kerberos Server you can't launch klist from commandline, and take the service who uses the LDAP protocol (LDAP/server@domain -> server).

      如果仍然无法正常工作,尝试添加

      If it still does not work try to add

      System.setProperty("sun.security.krb5.debug", "true"); 
      

      和后期的输出。

      这篇关于javax.naming.AuthenticationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆