少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
          以下文章都是整合了好多網上的好多朋友的優秀資源,才寫出來的。具體參考過誰的,我也記不清楚了。關于怎么生成https雙向的證書,地址在這里:
          http://www.aygfsteel.com/stevenjohn/archive/2012/08/22/385989.html 
          應該正常來說,按照這個教程做的話是沒有任何問題的,但是也有些朋友出過問題,主要問題是在,把證書導入到瀏覽器里面的時候出的,注意這里。

          我這里面的我都做過三四次了,基本沒啥問題。但也不排除不會不出問題。

          由于網上關于httpCilent來測試調用HTTPS的例子較少,經過在度娘和谷爹的查找,總算是也找到了一篇文章,參考以后,做出來一個測試類,在我機器上面是能夠跑通的。具體地址:http://www.aygfsteel.com/stevenjohn/archive/2012/09/27/388646.html 



          //首先說一下,這個是我隨便寫的一個發布到tomcat的httpsUrlConnection的Servlet服務,主要是用來測試一下https雙向驗證的,現在網上好多的文章都是https單向驗證的Java代碼,我在網上看過好多,但是好多都是半成品,然后總結了一下,在自己的機器上面是完全能夠跑通的,在這里做個筆記,以后用得著的時候來拿:
          package com.abin.lee.https;
          import java.io.BufferedReader;
          import java.io.IOException;
          import java.io.InputStreamReader;
          import java.io.PrintWriter;
          import java.util.Enumeration;
          import java.util.Map;
          import javax.servlet.ServletException;
          import javax.servlet.ServletOutputStream;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;
          @SuppressWarnings("serial")
          public class ReceiveHttpsUrlConnectionRequest extends HttpServlet {
          public void service(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
          System.out.println("receive https request");
                          /**這個主要是接收,由對方給以POST形式發過來的內容,這種內容不是以key-value的形式發的,而是直接通過Java的
                          *string content="test https double auth";
                          *BufferedWriter writer  = new BufferedWriter();
                          *writer.writer(content.getBytes());
                          *通過這種形式發過來的內容的接收,由于直接放到request里面發送過來的,所以的從request里面來接收。
                          *之前做銀聯的手機支付的時候也是這么傳遞參數的。
                          */
          BufferedReader reader=new BufferedReader(new InputStreamReader(request.getInputStream()));
          String line=null;
          StringBuffer stb=new StringBuffer();
                          //循環的一行一行的讀取內容
               while((line=reader.readLine())!=null){
          stb.append(line);
          }
                          //打印讀取到的內容。
          System.out.println("stb="+stb.toString());
                          //給調用者返回內容
          PrintWriter write=response.getWriter();
          write.write("receive HttpsUrlConnection success");
          write.flush();
          write.close();
          }
          }




          //這個是在web工程里面的web.xml里面配置的發布的servlet服務
          //web.xml
          <servlet>
          <servlet-name>httpsUrlConnectionRequest</servlet-name>
          <servlet-class>com.abin.lee.https.ReceiveHttpsUrlConnectionRequest</servlet-class>
          </servlet>
          <servlet-mapping>
          <servlet-name>httpsUrlConnectionRequest</servlet-name>
          <url-pattern>/httpsUrlConnectionRequest</url-pattern>
          </servlet-mapping>

          //HttpsUrlConnection測試類
          package com.abin.lee.test;
          import java.io.BufferedReader;
          import java.io.IOException;
          import java.io.InputStreamReader;
          import java.io.OutputStream;
          import java.net.URL;
          import java.util.Date;
          import javax.net.ssl.HostnameVerifier;
          import javax.net.ssl.HttpsURLConnection;
          import javax.net.ssl.SSLSession;
          import junit.framework.TestCase;
          import org.junit.Before;
          import org.junit.Test;
          public class HttpsUrlConnectionClient extends TestCase {
          // 客戶端密鑰庫
          private String sslKeyStorePath;
          private String sslKeyStorePassword;
          private String sslKeyStoreType;
          // 客戶端信任的證書
          private String sslTrustStore;
          private String sslTrustStorePassword;
                  //上面發布的servlet請求地址
          private String httpsUrlConnectionUrl = "https://localhost:8443/global/httpsUrlConnectionRequest";
          @Before
          public void setUp() {
                          //這是密鑰庫
               sslKeyStorePath = "D:\\home\\tomcat.keystore";
          sslKeyStorePassword = "stevenjohn";
          sslKeyStoreType = "JKS"; // 密鑰庫類型,有JKS PKCS12等
                          //信任庫,這里需要服務端來新人客戶端才能調用,因為這個我是配置的https雙向驗證,不但是要客戶端信任服務端,服務端也要信任客戶端。
          sslTrustStore = "D:\\home\\tomcat.keystore";
          sslTrustStorePassword = "stevenjohn";
          System.setProperty("javax.net.ssl.keyStore", sslKeyStorePath);
          System.setProperty("javax.net.ssl.keyStorePassword",
          sslKeyStorePassword);
          System.setProperty("javax.net.ssl.keyStoreType", sslKeyStoreType);
          // 設置系統參數
          System.setProperty("javax.net.ssl.trustStore", sslTrustStore);
          System.setProperty("javax.net.ssl.trustStorePassword",
          sslTrustStorePassword);
          System.setProperty("java.protocol.handler.pkgs", "sun.net.www.protocol");
          }
          @Test
          public void testHttpsUrlConnectionClient() {
          try {
          URL url = new URL(httpsUrlConnectionUrl);
                                  //對于主機名的驗證,因為配置服務器端的tomcat.keystore的證書的時候,是需要填寫用戶名的,一般用戶名來說是本地ip地址,或者本地配置的域名
               HostnameVerifier hv = new HostnameVerifier() {
          public boolean verify(String urlHostName, SSLSession session) {
          return true;
          }
          };
          HttpsURLConnection.setDefaultHostnameVerifier(hv);
                                  //編寫HttpsURLConnection 的請求對象,這里需要注意HttpsURLConnection 比我們平時用的HttpURLConnection對了一個s,因為https是也是遵循http協議的,并且是采用ssl這個安全套接字來傳輸信息的,但是也有可能遭到黑客的攻擊  
          HttpsURLConnection connection = (HttpsURLConnection) url
          .openConnection();
          connection.setRequestProperty("Content-Type", "text/xml");
          connection.setDoOutput(true);
          connection.setDoInput(true);
                                  //設置請求方式為post,這里面當然也可以用get,但是我這里必須用post
               connection.setRequestMethod("POST");
          connection.setUseCaches(false);
          connection.setReadTimeout(30000);
          String user="abin";
          String pwd="abing";
          String request="user="+user+"&pwd="+pwd;
          OutputStream out = connection.getOutputStream();
                                  //下面的這句話是給servlet發送請求內容
          out.write(request.getBytes());
          out.flush();
          out.close();
          //接收請求的返回值
          BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          StringBuffer stb = new StringBuffer();
          String line;
          while ((line = reader.readLine()) != null) {
          stb.append(line);
          }
          Integer statusCode = connection.getResponseCode();
          System.out.println("返回狀態碼:" + statusCode);
          reader.close();
          connection.disconnect();
          } catch (IOException e) {
          e.printStackTrace();
          }
          }
          }




          //發布好了服務,你需要在tomcat里面配置好了https服務的端口才能使用。
          //tomcat配置文件:
           <Connector port="6060" protocol="HTTP/1.1" 
                         connectionTimeout="20000" 
                         redirectPort="8443" />
                  
                  /**關于https端口的說明,銀聯一般用的都是0--9  443,這種類型的端口,第一位是0--9中的任意一位,然后后面三位是443,而通過我的測試,發覺隨便一個端口號都可           *   以的,只要不和你機器的其他端口沖突就行,911,95553這些端口都是可以滴。
                    *clientAuth="true"   這里設置為false是https單向認證,設置為true則是https雙向認證
                  */
          <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
            SSLEnabled="true" maxThreads="150" scheme="https"
            secure="true" clientAuth="true" sslProtocol="TLS"
            keystoreFile="D:\\home\\tomcat.keystore" keystorePass="stevenjohn"  //密鑰庫
            truststoreFile="D:\\home\\tomcat.keystore" truststorePass="stevenjohn" />//信任庫
          posted on 2012-09-27 00:16 abin 閱讀(11247) 評論(1)  編輯  收藏 所屬分類: HTTPS

          Feedback

          # re: HttpsUrlConnection https雙向驗證 2016-07-12 19:37 12
          12121  回復  更多評論
            

          主站蜘蛛池模板: 新乡市| 石狮市| 阳信县| 锦州市| 鄢陵县| 九龙城区| 广南县| 平湖市| 洛浦县| 南平市| 维西| 芜湖县| 永寿县| 原平市| 韶关市| 宿松县| 鹤峰县| 旬邑县| 平罗县| 饶河县| 临颍县| 平乐县| 盐亭县| 宜丰县| 铅山县| 滨海县| 沁源县| 韩城市| 都江堰市| 盐边县| 洛宁县| 桐乡市| 屯留县| 象州县| 太和县| 峨眉山市| 界首市| 仁化县| 庆阳市| 万荣县| 利津县|