2016-06-27 76 views
1

我有嘗試打開ssh連接下面的示例代碼不能使用JSch與JDK開放SSH會話8

package jsch; 

import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.JSchException; 
import com.jcraft.jsch.Session; 

public class Main { 

    public static void main(String... args) { 
    JSch jsch=new JSch(); 
    try { 
     Session session=jsch.getSession("some_user", "some_host", 22); 
     session.setPassword("some_password"); 
     session.setConfig("StrictHostKeyChecking", "no"); 
     session.connect(); 
     Thread.sleep(1000); 
     session.disconnect(); 
    } catch (JSchException e) { 
     e.printStackTrace(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
    } 
} 

如果我使用JDK 7中一切正常,但如果我使用JDK 8我m如果該堆棧跟蹤:

Exception in thread "main" java.lang.IllegalArgumentException: Empty nameStrings not allowed 
    at sun.security.krb5.PrincipalName.validateNameStrings(PrincipalName.java:167) 
    at sun.security.krb5.PrincipalName.<init>(PrincipalName.java:277) 
    at sun.security.krb5.PrincipalName.parse(PrincipalName.java:315) 
    at sun.security.krb5.internal.KRBError.init(KRBError.java:355) 
    at sun.security.krb5.internal.KRBError.<init>(KRBError.java:186) 
    at sun.security.krb5.KrbTgsRep.<init>(KrbTgsRep.java:58) 
    at sun.security.krb5.KrbTgsReq.getReply(KrbTgsReq.java:259) 
    at sun.security.krb5.KrbTgsReq.sendAndGetCreds(KrbTgsReq.java:270) 
    at sun.security.krb5.internal.CredentialsUtil.serviceCreds(CredentialsUtil.java:302) 
    at sun.security.krb5.internal.CredentialsUtil.acquireServiceCreds(CredentialsUtil.java:120) 
    at sun.security.krb5.Credentials.acquireServiceCreds(Credentials.java:458) 
    at sun.security.jgss.krb5.Krb5Context.initSecContext(Krb5Context.java:693) 
    at sun.security.jgss.GSSContextImpl.initSecContext(GSSContextImpl.java:248) 
    at sun.security.jgss.GSSContextImpl.initSecContext(GSSContextImpl.java:179) 
    at com.jcraft.jsch.jgss.GSSContextKrb5.init(GSSContextKrb5.java:129) 
    at com.jcraft.jsch.UserAuthGSSAPIWithMIC.start(UserAuthGSSAPIWithMIC.java:135) 
    at com.jcraft.jsch.Session.connect(Session.java:463) 
    at com.jcraft.jsch.Session.connect(Session.java:183) 
    at jsch.Main.main(Main.java:15) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

是否有機會解決這一問題,而不黑客和使用JSch與JDK8或者我應該切換到另一個SSH庫?

回答

0

您的JDK8顯然認爲Kerberos已安裝/可用,但無論是錯誤的還是它在某個地方都無法正常工作。儘管我自己並不使用Kerberos,但如果您的意思是啓用Kerberos,這表明您的JDK 8中某處出現了錯誤(可能配置錯誤),則默認情況下它們都不會默認啓用Kerberos。

代替(或補充)查找和修復的是,它應該工作,告訴JSCH到無法使用Kerberos,或至少先嚐試其他方法:

# Q&D -- assumes default as of 0.1.53 
session.setConfig ("PreferredAuthentications", 
    "publickey,keyboard-interactive,password"); 
# or 
session.setConfig ("PreferredAuthentications", 
    "publickey,keyboard-interactive,password,gssapi-with-mic"); 

# more robust for future 
ArrayList<String> auths = new ArrayList<String>( 
    Arrays.asList(session.getConfig ("PreferredAuthentications") .split(","))); 
auths.remove ("gssapi-with-mic"); 
# optional add back to end 
auths.add ("gssapi-with-mic"); 
session.setConfig ("PreferredAuthentications", String.join (",", auths));