当前位置: 首页 > 面试题库 >

如何在apache-commons net中使用生成的证书配置客户端身份验证

淳于祺
2023-03-14
问题内容

首先,我知道这里有一个类似的问题,但并没有回答我的疑问。我有一个配置有SSL的FTPS服务器(vsftpd)。

我用以下方法生成了适当的密钥:

openssl req -x509 -nodes -days 1825 -newkey rsa:2048 \
-keyout private/vsftpd.key \
-out certs/vsftpd.crt

并在服务器中配置了相应的conf文件。

现在,在具有Apache commons net的Java客户端代码中,我可以通过SSL与服务器通信,如下所示:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;

public class CommonsNetFTPSTest {
public static void main(String[] args) throws Exception {
    System.setProperty("javax.net.debug", "ssl");

    String server = "XXX.XXX.XXX.XXX";
    String username = "USER_TEST";
    String password = "ABCD1234";
    String remoteFile = "/Data/Input/PH240819";
    String localFile = "PH240819";
    String protocol = "SSL"; // TLS / null (SSL)
    int port = 990;
    int timeoutInMillis = 10000;
    boolean isImpicit = true;

    FTPSClient client = new FTPSClient(protocol, isImpicit);

    client.setDataTimeout(timeoutInMillis);
    client.addProtocolCommandListener(new PrintCommandListener(new  PrintWriter(System.out)));

    System.out.println("################ Connecting to Server ################################");

    try
    {
        int reply;
        System.out.println("################ Connect Call ################################");
        client.connect(server, port);

        client.login(username, password);

        System.out.println("################ Login Success ################################");

        //client.setFileType(FTP.BINARY_FILE_TYPE);
        client.setFileType(FTP.NON_PRINT_TEXT_FORMAT);
        client.execPBSZ(0);  // Set protection buffer size
        client.execPROT("P"); // Set data channel protection to private
        client.enterLocalPassiveMode();

        System.out.println("Connected to " + server + ".");
        reply = client.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            client.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }

        client.listFiles();
        boolean retrieved = client.retrieveFile(remoteFile, new FileOutputStream(localFile));
    }
    catch (Exception e)
    {
        if (client.isConnected())
        {
            try
            {
                client.disconnect();
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
        System.err.println("Could not connect to server.");
        e.printStackTrace();
        return;
    }
    finally
    {
        //client.disconnect();
        client.logout();
        System.out.println("# client disconnected");
    }
}
}

但是这样,我的客户就可以接受任何证书,而且我猜这很可能是中间人攻击。因此,我想告诉我的客户要接受哪些证书,并且仅在证书有效时才进行连接。

现在,我已经生成了两个文件:vsftpd.key和vsftpd.crt,并且已经将crt文件导入到本地密钥库中,如下所示:

keytool -import -alias alias -file vsftps.crt -keypass keypass -keystore mykeystore.jks -storepass Hello1

我的问题是,如何告诉我的客户使用mykeystore的证书?我还想念什么?谢谢


问题答案:

好的,现在我知道了。

我从一开始就做错了。首先,您需要将两个文件(vsftpd.crt和vsftpd.key)转换为单个PKCS12文件。

openssl pkcs12 -export -in vsftpd.crt -inkey vsftpd.key > vsftpd.p12

接下来,您需要将PKCS12文件导入密钥库:

keytool -importkeystore -srckeystore vsftpd.p12 -destkeystore keystore.jks -srcstoretype pkcs12

详细说明[此处]。2

最后,您只需要使用生成的密钥库实例化信任管理器,然后将其交给FTPSClient。就像是:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPSClient;
import org.apache.commons.net.io.Util;
import org.apache.commons.net.util.TrustManagerUtils;

public method() throws IOException, GeneralSecurityException{

    File storeFile = new File("path/to/keystore");

    KeyStore keyStore = loadStore("JKS", storeFile, "password");
    X509TrustManager defaultTrustManager = TrustManagerUtils.getDefaultTrustManager(keyStore);

    client = new FTPSClient(properties.getProtocol(), isImpicit);

    client.setTrustManager(defaultTrustManager);
    logOutput = new LogOutputStream(log, Level.INFO);
}

//Helper method from apache: http://commons.apache.org/proper/commons-net/apidocs/index.html?org/apache/commons/net/util/KeyManagerUtils.html
private KeyStore loadStore(String storeType, File storePath, String storePass)
        throws KeyStoreException,  IOException, GeneralSecurityException {
        KeyStore ks = KeyStore.getInstance(storeType);
        FileInputStream stream = null;
        try {
            stream = new FileInputStream(storePath);
            ks.load(stream, storePass.toCharArray());
        } finally {
            Util.closeQuietly(stream);
        }
        return ks;
    }


 类似资料:
  • 我不熟悉SSL和证书。我一直在做关于客户端证书认证的研究。我看过这个和wiki。 因此,如果我必须为我的B2B REST服务实现客户端证书身份验证解决方案,我应该执行以下操作 要求客户端生成自己的私钥,并为其公钥生成证书(CA 颁发?)。通过电子邮件或 USB 闪存盘发送该证书。 在服务器端将客户端的公共证书导入信任存储区并启用客户端身份验证 在握手期间,客户端会出示其证书并进行身份验证,因为服务

  • 我正在尝试调整Spring Security SAML示例应用程序,以使用测试IDP(由其他人提供给我)而不是SSOCELL。单击“SAML登录”可以正确地将我重定向到IDP的SSO登录页面,但在登录并重定向回示例应用程序后,我会得到一个异常(显然是在工件解析期间),其根源是: 挖了一会儿后,我意识到服务器需要相关端口的客户端身份验证。如果我这样连接到它,我会得到一个有效的响应: 此外,如果我通过

  • 问题内容: 我需要导入证书,以便向Spring Boot应用程序中的外部服务发出http请求。 我该如何设置Spring Boot来做到这一点? 那里有很多信息,但我发现所有这些都令人困惑。似乎我可能只需要创建类似“ truststore.jks”密钥库的内容并导入正确的证书,然后将一些条目添加到我的application.properties中即可。 问题答案: 打开您的终端或 回答所有问题。在

  • 问题内容: 我正在尝试连接到HTTPS URL,但是我需要使用客户端身份验证以及第三方软件在我的系统上放置的证书。 我丝毫不知道如何找到或使用它,我所要做的只是C#示例代码,这与我为此找到的所有Java答案都大不相同。(例如,KeyStore显然需要某种密码吗?) 这是我拥有的C#示例代码 然后,它仅遍历WS2_store_Certs CertificateCollection并一路检查它们。再进

  • 问题内容: 当使用node.js作为客户端时,是否可以使用Windows集成身份验证连接到服务器(例如,连接到IIS时)? 我对此的搜索仅显示将node.js用作服务器的结果。 问题答案: 2015更新: 现在有些模块实现了Windows集成的身份验证。 node- sspi 使用SSPI(Windows安全API)来处理服务器端的事务,但不执行client auth 。有几种客户端实现,例如ht

  • 授权服务器为进行客户端身份验证的目的,为Web应用客户端创建客户端凭据。授权服务器被鼓励考虑比客户端密码更强的客户端身份验证手段。Web应用程序客户端必须确保客户端密码和其他客户端凭据的机密性。 授权不得向本地应用程序或基于用户代理的应用客户端颁发客户端密码或其他客户端凭据用于客户端验证目的。授权服务器可以颁发客户端密码或其他凭据给专门的设备上特定安装的本地应用程序客户端。 当客户端身份验证不可用