jsoup和密码套件SSL

时间:2014-01-16 21:29:52

标签: java ssl jsoup

进行SSL连接时,我可以选择jsoup使用的密码吗? 我有java 7和最新版本的jsoup,但是当jsoup进行SSL连接时,它永远不会连接到Web服务器支持的最佳密码。我可以强制选择吗?

修改

import java.util.ArrayList;
import java.util.Arrays;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.InetAddress;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.HttpsURLConnection;
import java.io.IOException;

public class PreferredCipherSuiteSSLSocketFactory extends SSLSocketFactory 
{
private final SSLSocketFactory delegate;

public PreferredCipherSuiteSSLSocketFactory(SSLSocketFactory delegate) 
{
    this.delegate = delegate;
}
public PreferredCipherSuiteSSLSocketFactory() 
{
    this.delegate = HttpsURLConnection.getDefaultSSLSocketFactory();
}

@Override
public String[] getDefaultCipherSuites() 
{
    return setupPreferredDefaultCipherSuites(this.delegate);
}

@Override
public String[] getSupportedCipherSuites() 
{
    return setupPreferredSupportedCipherSuites(this.delegate);
}

@Override
public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException 
{
    Socket socket = this.delegate.createSocket(arg0, arg1);
    String[] cipherSuites = setupPreferredDefaultCipherSuites(delegate);
    ((SSLSocket)socket).setEnabledCipherSuites(cipherSuites);

    return socket;
}

@Override
public Socket createSocket(InetAddress arg0, int arg1) throws IOException 
{
    Socket socket = this.delegate.createSocket(arg0, arg1);
    String[] cipherSuites = setupPreferredDefaultCipherSuites(delegate);
    ((SSLSocket)socket).setEnabledCipherSuites(cipherSuites);

    return socket;
}

@Override
public Socket createSocket(Socket arg0, String arg1, int arg2, boolean arg3) throws IOException
{
    Socket socket = this.delegate.createSocket(arg0, arg1, arg2, arg3);
    String[] cipherSuites = setupPreferredDefaultCipherSuites(delegate);
    ((SSLSocket)socket).setEnabledCipherSuites(cipherSuites);

    return socket;
}

@Override
public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException 
{
    Socket socket = this.delegate.createSocket(arg0, arg1, arg2, arg3);
    String[] cipherSuites = setupPreferredDefaultCipherSuites(delegate);
    ((SSLSocket)socket).setEnabledCipherSuites(cipherSuites);

    return socket;
}

@Override
public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException 
{
    Socket socket = this.delegate.createSocket(arg0, arg1, arg2, arg3);
    String[] cipherSuites = setupPreferredDefaultCipherSuites(delegate);
    ((SSLSocket)socket).setEnabledCipherSuites(cipherSuites);

    return socket;
}

private static String[] setupPreferredDefaultCipherSuites(SSLSocketFactory sslSocketFactory) 
{
    ArrayList<String> suitesList = new ArrayList<String>(Arrays.asList(sslSocketFactory.getSupportedCipherSuites()));
    ArrayList<String> newList = new ArrayList<String>();
    for(String s : suitesList)
    {
        if(s.indexOf("ECDHE")!=-1)
            newList.add(s);
    }
    return newList.toArray(new String[newList.size()]);
}

private static String[] setupPreferredSupportedCipherSuites(SSLSocketFactory sslSocketFactory) 
{
    ArrayList<String> suitesList = new ArrayList<String>(Arrays.asList(sslSocketFactory.getSupportedCipherSuites()));
    ArrayList<String> newList = new ArrayList<String>();
    for(String s : suitesList)
    {
        if(s.indexOf("ECDHE")!=-1)
            newList.add(s);
    }
    return newList.toArray(new String[newList.size()]);
}
}

在我班上

    PreferredCipherSuiteSSLSocketFactory objPrefCipher = new PreferredCipherSuiteSSLSocketFactory();
    HttpsURLConnection.setDefaultSSLSocketFactory(objPrefCipher);

现在jsoup连接到https时只使用密码与ECDHE。

1 个答案:

答案 0 :(得分:0)

  

我可以强制选择吗?

没有。 SSL服务器选择密码套件,而不是客户端。除非你已经做了一些改变启用的密码套件的事情,否则你应该得到服务器在Java支持的许多服务器中选择的任何内容。