首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SSLSocketFactory后连接到twitter

使用SSLSocketFactory后连接到twitter
EN

Stack Overflow用户
提问于 2013-02-20 21:05:25
回答 1查看 909关注 0票数 1

我正在尝试使用Twitter4J从中检索tweet。该项目使用SSLSocket连接到远程服务器以检索某些数据,然后调用Twitter4J。问题是,如果我建立了这个连接,Twitter4J就会出现这个异常:

星期三2月20日11:32:02 CET 2013sun.security.validator.ValidatorException: PKIX路径构建失败: sun.security.provider.certpath.SunCertPathBuilderException:无法找到被请求目标的有效认证路径

如果我不建立连接,并且清除用下面的行定义的密钥存储库,则不会发生这种情况:

代码语言:javascript
复制
System.clearProperty("javax.net.ssl.trustStore");
System.clearProperty("javax.net.ssl.trustStorePassword");

要连接的代码是:

代码语言:javascript
复制
private String publishFetcher() throws UnknownHostException, IOException {
    // Set trustStore.
    System.setProperty("javax.net.ssl.trustStore", properties.getProperty("trustedStore"));
    System.setProperty("javax.net.ssl.trustStorePassword", properties.getProperty("trustedStorePassword"));

    String host = properties.getProperty("host");
    int hostPort = Integer.parseInt(properties.getProperty("host_port"));
    SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket socket = (SSLSocket) factory.createSocket(host, hostPort);

    // Get input and output streams from socket connection.
    BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    PrintWriter output = new PrintWriter(socket.getOutputStream());

    // Request connection token to controller
    String connectionToken = getConnectionToken(input, output);

    // Publish fetcher
    final int rmiPort = Integer.parseInt(properties.getProperty("rmi_port"));
    TwitterFetcher fetcher = new TwitterFetcher(connector);
    Fetcher stub = (Fetcher) UnicastRemoteObject.exportObject(fetcher, 0);
    Registry registry = LocateRegistry.createRegistry(rmiPort);
    registry.rebind(connectionToken, stub);

    // Send RMI port
    output.write(String.valueOf(rmiPort) + "\n");
    output.flush();

    input.close();
    output.close();

    // Clean trusteStore properties.
    System.clearProperty("javax.net.ssl.trustStore");
    System.clearProperty("javax.net.ssl.trustStorePassword");

    return connectionToken;
}

我认为这个问题与SSLSocketFactory有关,因为我在一个不同的项目中测试了一些东西。例如,这段代码的工作原理就像一种魅力:

代码语言:javascript
复制
SSLSocketFactory deffactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
System.setProperty("javax.net.ssl.trustStore", "sttv_keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "Smart.Table");

SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();

factory = deffactory;
// Twitter4J code...

但是这个代码不起作用:

代码语言:javascript
复制
System.setProperty("javax.net.ssl.trustStore", "sttv_keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "Smart.Table");

SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
// Twitter4J code...

我不能在我的真正的项目中做同样的事因为休息..。几乎所有的^^

有什么问题吗?解决办法呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-21 10:02:21

代码的问题在于,我正在替换信任商店,而不是为我的应用程序创建一个新的存储库。解决这些问题的代码是下一个:

代码语言:javascript
复制
private SSLSocket getSSLSocket() throws TrackerSSLConnectionException {
    try {
        // Load properties.
        String keystore = properties.getProperty("keystore");
        String passphrase = properties.getProperty("keystorePassphrase");
        String host = properties.getProperty("host");
        int hostPort = Integer.parseInt(properties.getProperty("host_port"));

        // Create keystore.
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(new FileInputStream(keystore), passphrase.toCharArray());

        // Get factory for the given keystore.
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(keyStore);
        SSLContext ctx = SSLContext.getInstance("SSL");
        ctx.init(null, tmf.getTrustManagers(), null);
        SSLSocketFactory factory = ctx.getSocketFactory();

        return (SSLSocket) factory.createSocket(host, hostPort);
    } catch (Exception e) {
        throw new TrackerSSLConnectionException(e.getMessage(), e.getCause());
    }
}

private String publishFetcher() throws TrackerSSLConnectionException, IOException {
    // Get socket connection.
    SSLSocket socket = getSSLSocket();

    // Get input and output streams from socket.
    BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    PrintWriter output = new PrintWriter(socket.getOutputStream());

    // Request connection token to controller.
    String connectionToken = getConnectionToken(input, output);

    // Publish fetcher.
    final int rmiPort = Integer.parseInt(properties.getProperty("rmi_port"));
    TwitterFetcher fetcher = new TwitterFetcher(connector);
    Fetcher stub = (Fetcher) UnicastRemoteObject.exportObject(fetcher, 0);
    Registry registry = LocateRegistry.createRegistry(rmiPort);
    registry.rebind(connectionToken, stub);

    // Send RMI port.
    output.write(String.valueOf(rmiPort) + "\n");
    output.flush();

    input.close();
    output.close();

    return connectionToken;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14989895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档