java發https請求,認證配置

來源:互聯網
上載者:User

配認證:

Getting   Tomcat   SSL   (https)   Working
1.Create   a   certificate   keystore   containing   a   single   self-signed   certificate   by   executing   the   following   command.   Specify   a   password   value   of   "changeit ".   Note   that   this   command   creates   both   the   certificate   and   the   keystore 

 Windows: 進到%JAVA_HOME%/bin/目錄下輸入: 

             keytool   -genkey   -alias   tomcat   -keyalg   RSA   -keystore   tomcat.keystore

        產生一個tomcat.keystore 檔案
Unix:   $JAVA_HOME/bin/ 輸入:      keytool   -genkey   -alias   tomcat   -keyalg   RSA   -keystore   tomcat.keystore

到D:/Tomcat   5.0/conf目錄下輸入: keytool   -genkey   -alias   tomcat   -keyalg   RSA   -keystore   tomcat.keystore
輸入keystore密碼:     changeit
您的名字與姓氏是什麼。
    [Unknown]:     localhost
您的組織單位名稱是什麼。
    [Unknown]:     huawei

您的組織名稱是什麼。
    [Unknown]:     hell
您所在的城市或地區名稱是什麼。
    [Unknown]:     hangzhou
您所在的州或省份名稱是什麼。
    [Unknown]:     zhejiang

該單位的兩字母國家代碼是什麼
    [Unknown]:     ch
CN=localhost,   OU=wict,   O=hell,   L=wuhan,   ST=hubei,   C=ch   正確嗎。
    [否]:     y

輸入 <tomcat> 的主密碼
                (如果和   keystore   密碼相同,按斷行符號): 這裡我按了斷行符號
2.     Copy   the   keystore   file   to   CATALINA_HOME/conf   拷貝到tomcat的conf下
3.     Uncomment   the   "SSL   HTTP/1.1   Connector "   entry   in   $CATALINA_HOME/conf/server.xml.   Your   entry   should   look   like:
<!--   Define   a   SSL   Coyote   HTTP/1.1   Connector   on   port   8443   -->
<!---->

<Connector port="8443" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" disableUploadTimeout="true"
               acceptCount="100" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS"
               keystoreFile= "/conf/tomcat.keystore "
            truststoreFile= "/conf/tomcat.keystore "/>

        添加後儲存,瀏覽其中輸入以下地址訪問tomcat首頁     https://localhost:8443
4.     Copy   the   keystore   file   to   the   default   file   location   of   the   Java   Applications,   then   run   the   Java   Application。 拷貝到工程目錄下,注意,直接再工程這級目錄下面。

 

5、java代碼:

public static void main(String[] args) throws Exception {
  //serverkeys是通過keytool產生的自己的認證
  System.setProperty( "javax.net.ssl.trustStore",   "tomcat.keystore");
  System.setProperty( "javax.net.ssl.trustStorePassword",   "changeit");
  //connect   to   https https://www.sun.com
  URL   url   =   new   URL( "https://localhost:8443");
  HttpURLConnection   connection   =   (HttpURLConnection)   url.openConnection();
  connection.setRequestMethod( "POST");
  connection.setDoOutput(true);
  connection.setDoInput(true);
  System.out.println( "1--Conected to "+   connection.toString()); 
    
//  DataOutputStream   dos   =   new   DataOutputStream(connection.getOutputStream());
//  byte[]  rr   =   new   byte[dos.size()];
//  dos.write(rr,0,rr.length);
//  dos.flush();
//  String   ss   =   new   String(rr);
//  System.out.println("2-----"+ss); 
     
   StringBuffer   outbuff   =   new   StringBuffer();
  BufferedReader   in   =   new   BufferedReader(new   InputStreamReader(connection.getInputStream()));
  String   line;
  while   ((line   =   in.readLine())   !=   null)   {
            outbuff.append(line);
  }
  in.close();
  System.out.println( "3---Test   : "   +   outbuff.toString()); 
   }

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.