从应用程序QA环境连接SFTP服务器时出现UnknownHostKey错误

时间:2019-07-16 11:48:02

标签: sftp

从Java应用程序连接外部SFTP服务器时,我收到Unknownhostkey错误。

我从运营商系统的SFTP服务器获取了主机密钥文件内容,并将knownhost设置为字节数组值。但是在连接服务器时出现了unknownhostkey错误。

/ **      * connect方法用于与SFTP服务器通信。      *      *类型为String的@param systemId      * @返回FTPProxyClient      * @throwsOfBusinessRuntimeException      * @请参阅ICommunication#connect(String)      * @modelguid {968FE68C-55D2-4AF6-990A-B9B90ABB20C0}      * /

public Session connectSFTP(final FtpCredentials config)
        throws OfBusinessRuntimeException{
    final String METHOD_NAME = "connect";
    log.entering(CLASS_NAME, METHOD_NAME, config);

    /*Local attributes declaration */
    String host = null;
    String user = null;
    String password = null;
    int elapsedTime = 0;
    int sftpPort = 0;
    String knownHostPublicKey = null;
    String sftpProxy = null;
    int sftpProxyPort=0;
    Session sftpSession = null;
    JSch jsch = new JSch();
    /* Beginning of try catch block */
    try {

        // Getting FTP connection details.
        if (config != null) {
            host = config.getHost();
            user = config.getUserId();
            sftpPort = config.getSftpPort();
            password = config.getPassword();
            elapsedTime = config.getElapsedTime();
            knownHostPublicKey=config.getKnownHostPublicKey();
            jsch.setKnownHosts(new ByteArrayInputStream(knownHostPublicKey.getBytes()));
            sftpProxy=config.getSftpProxy();
            sftpProxyPort=config.getSftpProxyPort();
            sftpSession = jsch.getSession(user, host, sftpPort);
        }
        log.info("Before Connect " + host + " User :- " + user);
        if (host == null || user == null || password == null) {
            throw new OfBusinessRuntimeException(
                    "SFTP Host Information not found.");
        }
        sftpSession.setHost(host);
        sftpSession.setPassword(password);
        sftpSession.setPort(sftpPort);

        //java.util.Properties config = new java.util.Properties();
        // // force aes256-ctr encryption
        //config.put("cipher.s2c", "aes256-ctr");
        //config.put("cipher.c2s", "aes256-ctr");
        //config.put("CheckCiphers", "aes256-ctr");
        //session.setConfig(config);

        sftpSession.setProxy(new ProxyHTTP(sftpProxy,sftpProxyPort));
        /*
         * Setting the timeout to 30 seconds to ensure connection is made
         * for testing setting the port to 22 as this should be the one
         * to accept the connection
         **/

        sftpSession.setTimeout(elapsedTime);
        sftpSession.connect();
        log.info("SFTP Session Connection is successful");

    } catch (final JSchException jschException) {
        log.info("JSchException @  TransCommunicationMgr:- "
                 + METHOD_NAME + " Result Code: "
                 + jschException.getMessage());
        throw new OfBusinessRuntimeException(jschException.getMessage(),
                jschException);
    }
    /* Log existing method. */
    log.exiting(CLASS_NAME, METHOD_NAME, sftpSession);
    return sftpSession;
}

我得到的错误如下

5026 [7/1/19 11:05:04:473 EDT] 00026cb2 TransCommunic I JSchException @ TransCommunicationMgr:-connect结果代码:UnknownHostKey:74.126.93.138。 RSA密钥指纹为e8:90:a9:f3:3d:8f:83:26:e3:24:2b:2f:a1:71:e3:7c

期望未知主机密钥错误应得到解决。

0 个答案:

没有答案