2016-08-16 12 views
0

証明書付きのSSL接続を使用してサーバーに接続できるコードを作成しようとしています。 私は証明書、その.crtファイルを持っています。 私は、コードを組み立てましたが、機能しません:証明書を使用したSSL接続

public class SSLClient { 
public static void main(String[] args) throws Exception{ 
    String strServerName = "Some Server"; // SSL Server Name 
    int intSSLport = 443; // Port where the SSL Server is listening 
    PrintWriter out = null; 
    BufferedReader in = null; 

    { 
     // Registering the JSSE provider 
     Security.addProvider(new Provider()); 
    } 

    try { 
     // Creating Client Sockets 
     SSLSocketFactory sslfac = (SSLSocketFactory)SSLSocketFactory.getDefault(); 
     SSLSocket socket = (SSLSocket)sslfac.createSocket(strServerName,intSSLport); 

     System.setProperty("javax.net.ssl.keyStore", "D:/projects/TemporaryTesting/certificate.crt"); 

     // Initializing the streams for Communication with the Server 
     out = new PrintWriter(socket.getOutputStream(), true); 
     in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

     BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); 
     String userInput = "Hello Testing "; 
     out.println(userInput); 
     System.out.println("echo: " + in.readLine()); 

     // Closing the Streams and the Socket 
     out.close(); 
     in.close(); 
     stdIn.close(); 
     socket.close(); 
    } 

    catch(Exception exp) 
    { 
     System.out.println(" Exception occurred .... " +exp); 
     exp.printStackTrace(); 
    } 

}} 

これは私が取得していますエラーです:私は間違っ

javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targetjavax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 

何をしているのですか? 私はそれを使用する前に証明書をJavaで登録する必要がありますか? どうすれば修正できますか? これは私のSSL接続との最初の遭遇であり、どんな助けにも感謝します。

私は日食

に証明書をインポートすると、また最後に、このパラメータを追加することのlib /セキュリティの下にJDKフォルダにcacertsファイルにcmdを手動 http://magicmonster.com/kb/prg/java/ssl/pkix_path_building_failed.html

をインポートし、日食でkeytoolをインストールするようないくつかのことをやって覚えて

+0

あなたのトラスト内にサーバの証明書( 'strServerName')を追加するのを忘れたと思います。 –

+3

[これをチェック](http:// stackoverf low.com/questions/6908948/java-sun-security-provider-certpath-suncertpathbuilderexception-unable-to-find) –

+0

@ oliv37トラストアとは何ですか?証明書をどのように追加するのですか? –

答えて

0

デバッグのためのEclipseの-Djavax.net.debug=all

+0

eclipseでkeytoolをインストールして証明書をインポートした後も、コマンドをcmdに入力する必要がありますか? keytoolはあなたのためにすべての処理をすることを想定していませんか?どこで '-Djavax.net.debug = all'をeclipseに入力しますか? –

+0

あなたは幸運なことに、1分前にjdk8、keytool -import -alias someUrl.com -keystore "C:\ Program Files \ Java \ jdk1.8.0_102 \ jre \ lib \ security \ cacerts "-file" C:\ Users \ yourUsername \ Desktop \ certificate.crt "私のための修正を行いましたが、すでにkeytoolなどがあったので、私たちはチャットで話しますか? – kaya

+0

ここにチャットはありますか?私たちはそれをどのように有効にしますか? –

関連する問題