2016-11-30 4 views
0

クライアント証明書を使用してサーバーとの相互認証を実装する必要がある過去2日間、この問題が発生しています。 .pfxファイルはresディレクトリのrawフォルダにあります。Httpsアンドロイドからサーバーへの通信は常に403の返答を返します

は、私はそれがRES /生/ certificate.pfx

に私はまた、アンドロイドでのHttpURLConnectionを実装していると私は私の証明書から生成されたものにのSSLSocketFactoryを設定しています。しかし問題はサーバーが常に403を返すことです。私は "User Agent"を含むすべてのリクエストプロパティを追加しようとしました。しかし、何も働いていないようです。

以下のコードを添付しました。私はこのような証明書を信頼しています。

URL url = null; 
     HttpURLConnection conn; 
     try { 
      url = new URL("https://example.com"); 
      conn = (HttpURLConnection) url.openConnection(); 
      if (conn instanceof HttpsURLConnection) { 
       ((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory); 
      } 
      conn.setRequestMethod("GET"); 
      conn.setRequestProperty("Accept", "application/json"); 
      conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"); 

      int responseCode = conn.getResponseCode(); 
      System.out.println("Server response code" + responseCode); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

を次のように

public SSLSocketFactory getSSLSocketFactory_Certificate(String keyStoreType, int keystoreResId) 
     throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException, NoSuchProviderException { 

    InputStream caInput = context.getResources().openRawResource(keystoreResId); 

    // creating a KeyStore containing trusted CAs 

    if (keyStoreType == null || keyStoreType.length() == 0) { 
     keyStoreType = KeyStore.getDefaultType(); 
    } 
    KeyStore keyStore = KeyStore.getInstance(keyStoreType); 

    keyStore.load(caInput, "".toCharArray()); 

    // creating a TrustManager that trusts the CAs in the KeyStore 

    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); 
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); 
    tmf.init(keyStore); 

    TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf.getTrustManagers()); 

    SSLContext sslContext = SSLContext.getInstance("TLS"); 
    sslContext.init(null, wrappedTrustManagers, null); 

    return sslContext.getSocketFactory(); 
} 

とは最終的に私の活動では、私はhttpsを呼び出し作るしかし、結果は常に禁止さ403です。私はどこが間違っているのか分からない。誰か助けてください。

答えて

2

ああ。最後に私は自分自身で問題を発見しました。私が欠けていたのは、証明書をロードした後、キーストアを使ってキーストアファクトリを初期化できなかったことです。

public SSLSocketFactory getSSLSocketFactory_Certificate(String keyStoreType, int keystoreResId) 
     throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException, NoSuchProviderException, UnrecoverableKeyException { 

    InputStream caInput = context.getResources().openRawResource(keystoreResId); 

    // creating a KeyStore containing trusted CAs 

    if (keyStoreType == null || keyStoreType.length() == 0) { 
     keyStoreType = KeyStore.getDefaultType(); 
    } 
    KeyStore keyStore = KeyStore.getInstance(keyStoreType); 
    keyStore.load(caInput, "".toCharArray()); 

    // creating a TrustManager that trusts the CAs in the KeyStore 

    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); 
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); 
    tmf.init(keyStore); 

    TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf.getTrustManagers()); 

    SSLContext sslContext = SSLContext.getInstance("TLS"); 
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 
    kmf.init(keyStore, "".toCharArray()); 

    sslContext.init(kmf.getKeyManagers(), wrappedTrustManagers, null); 
    return sslContext.getSocketFactory(); 
} 

これらの行が私を魅了しました。

SSLContext sslContext = SSLContext.getInstance("TLS"); 
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 
    kmf.init(keyStore, "".toCharArray()); 

    sslContext.init(kmf.getKeyManagers(), wrappedTrustManagers, null); 

誰かを助けてくれることを願っています。 :)

関連する問題