简体   繁体   English

使用自签名客户端证书的Java oAuth

[英]Java oAuth using self-signed client certificate

I had a perfectly working oauth with a self-signed client certificate, until suddenly it stopped working. 我有一个自签名客户端证书的可正常运行的oauth,直到突然停止工作。 I get SocketException: Connection Reset . 我得到SocketException: Connection Reset According to Xero, the API that I'm integrating with, everything is ok on their side now, but they did have SSL problem one week ago. 据我正在与之集成的API Xero称,现在他们一切都很好,但是一周前他们确实遇到了SSL问题。

Since the last time it worked we moved to Java 8, which I rolledback for this test. 自从上次运行以来,我们移至Java 8,在该测试中我回滚了Java 8。 Initially I had it working with this oauth project , because it was the only one that would, kind of, support self-signed client certificates. 最初,我让它与此oauth项目一起工作,因为它是唯一一种支持自签名客户端证书的项目。 Today I hacked Scribe a bit, in order to add the certificate to the request. 今天,为了将证书添加到请求中,我对Scribe做了一些修改。 When I finally got it working, I got the same exception again. 当我终于使它工作时,我又遇到了同样的异常。

The certificate that I have is in a KeyStore (.p12), which I exported into my java cacerts. 我拥有的证书位于KeyStore(.p12)中,该证书已导出到Java cacerts中。 This step should not be needed though, since it was working without it. 但是,由于没有此步骤,因此不需要此步骤。

So, this is how I create the SSLContext that is injected in the HttpClient (in the oauth project) and in the HttpsUrlConnection (in Scribe). 因此,这就是我创建注入到HttpClient(在oauth项目中)和HttpsUrlConnection(在Scribe中)中的SSLContext的方式。

Set<KeyManager> keymanagers = new HashSet<KeyManager>();
final KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(
    KeyManagerFactory.getDefaultAlgorithm());
kmfactory.init(entrustStore, password.toCharArray());
final KeyManager[] kms = kmfactory.getKeyManagers();
if (kms != null) {
    for (final KeyManager km : kms) {
        keymanagers.add(km);
    }
}

// TrustManagerFactory tmf =
//     TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
// tmf.init(keyStore);

SSLContext sslContext = SSLContext.getInstance(SSLSocketFactory.TLS);
sslContext.init(
    keymanagers.toArray(new KeyManager[keymanagers.size()]),
    null, // tmf.getTrustManagers()
    null);


// in the oauth project
SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext);
Scheme scheme = new Scheme("https", 443, socketFactory);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(scheme);
BasicClientConnectionManager cm = 
    new BasicClientConnectionManager(schemeRegistry);
httpClient = new DefaultHttpClient(cm);

// ---------------------------------

// in Scribe
HttpsURLConnection connection = 
    (HttpsURLConnection) new URL(completeUrl).openConnection();
connection.setSSLSocketFactory(sslContext.getSocketFactory());

I suspect this is the code that might be causing the exception, since this is the only common part between both implementations. 我怀疑这是可能导致异常的代码,因为这是两个实现之间唯一的共同部分。

The issue was related to TLS version after all. 毕竟,该问题与TLS版本有关。 Using TLSv1.1 did the trick. 使用TLSv1.1可以达到目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM