HTTPS communication bidirectional authentication using the JDK's own SSL package

Source: Internet
Author: User

Package com.iraid.test;import java.io.bufferedreader;import java.io.fileinputstream;import  java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import  java.io.printwriter;import java.net.url;import java.security.keystore;import  java.security.keystoreexception;import java.security.nosuchalgorithmexception;import  java.security.unrecoverablekeyexception;import java.security.cert.certificateexception;import  javax.net.ssl.httpsurlconnection;import javax.net.ssl.keymanager;import  javax.net.ssl.keymanagerfactory;import javax.net.ssl.sslcontext;import  javax.net.ssl.sslsocketfactory;import javax.net.ssl.trustmanager;import  javax.net.ssl.trustmanagerfactory;/** *  uses  JDK SSL package    HTTPS communication bidirectional authentication.  *  @author  wangfeihu * */public class httpstest {public static  void main (String[] args)  throwS exception {testhttpswithcert ();} /** * post  request, with dual certificate authentication  */public static void testhttpswithcert ()  {//   Credit Certificate Library string truststore =  "D:\\workspaces\\test\\https-native\\src\\cacerts.jks"; string truststorepass =  "Changeit";//  private key certificate string keystore =  "D:\\workspaces \\TEST\\HTTPS-NATIVE\\SRC\\WWW.DEMO.COM.P12 "; string keystorepass =  "052537159932766"; printwriter out = null; bufferedreader in = null; string result =  ""; Try {trustmanager[] tms = gettrustmanagers (TrustStore,  truststorepass); Keymanager[] kms = getkeymanagers (Keystore, keystorepass); Sslcontext sslcontext = sslcontext.getinstance ("SSL"); Sslcontext.init (kms, tms, new  java.security.securerandom ()); Sslsocketfactory ssf = sslcontext.getsocketfactory ();//  Service link URL URL = NEW&NBsp URL ("Https://www.demo.com/rest/UidApiService/authCardWithoutOTP");//  request parameter string params =  " {\ "merchantcode\":  \ "www.demo.com\", "+ " \ "sessionid\":  \ "10000011\", "+ " \ "username\":  \ "Jack\", "+ " \ "idnumber\":  \ "432652515\", "+ " \ "cardno\":  \ "561231321\", " +   "\" phoneno\ ":  \" \ "}"; httpsurlconnection conn =  (httpsurlconnection)  url.openconnection (); Conn.setsslsocketfactory (SSF);//  sets the generic request attribute Conn.setrequestproperty ("accept",  "*/*"); Conn.setrequestproperty ("Connection",  "keep-alive"); Conn.setrequestproperty ("User-agent",  "Mozilla/ 4.0 ");// content-type  set Conn.setrequestproperty (" Content-type ", " Application/json ") according to the specific needs;// The   send POST request must have the following two lines set Conn.setdooutput (true), Conn.setdoinput (true);//  get the output stream corresponding to the URLConnection object out =  new printwriter (Conn.getoutputstream ());//  send request parameter Out.print (params);//  The buffer out.flush ();//  definition of the flush output stream BufferedReaderInput stream to read the URL response in = new bufferedreader (New inputstreamreader (Conn.getinputstream ())); string line;while  ((Line = in.readline ())  != null)  {result +=  Line;} SYSTEM.OUT.PRINTLN (result);}  catch  (exception e)  {e.printstacktrace ();}  finally {try {in.close (); Out.close ();}  catch  (ioexception e)  {e.printstacktrace ();}} /** *  Load Trust Certificate library  *  *  @param  trustStore *  @param  truststorepass  *  @return  *  @throws  IOException */private static TrustManager[]  Gettrustmanagers (String truststore,string truststorepass)  throws ioexception {try  {string alg = trustmanagerfactory.getdefaultalgorithm (); Trustmanagerfactory factory = trustmanagerfactory.getinstance (ALG); InputStream fp =  new fileinputstream (Truststore); Keystore ks&nbSp;= keystore.getinstance ("JKS"); Ks.load (Fp, truststorepass.tochararray ()); Fp.close (); Factory.init ( KS); Trustmanager[] tms = factory.gettrustmanagers (); System.out.println (TMS); Return tms;}  catch  (nosuchalgorithmexception e)  {e.printstacktrace ();}  catch  (keystoreexception e)  {e.printstacktrace ();}  catch  (certificateexception e)  {e.printstacktrace ();} Return null;} /** *  loading the private key certificate  *  *  @param  keyStore *  @param  keystorepass  *  @return  *  @throws  IOException */private static KeyManager[]  Getkeymanagers (String keystore,string keystorepass)  throws ioexception {try { String alg = keymanagerfactory.getdefaultalgorithm (); Keymanagerfactory factory = keymanagerfactory.getinstance (ALG);inputstream fp =  New fileinputstream (KeyStore); keystore&Nbsp;ks = keystore.getinstance ("PKCS12"); Ks.load (Fp, keystorepass.tochararray ()); Fp.close (); Factory.init (Ks, keystorepass.tochararray ()); Keymanager[] keyms = factory.getkeymanagers (); System.out.println (keyms); return keyms;}  catch  (nosuchalgorithmexception e)  {e.printstacktrace ();}  catch  (keystoreexception e)  {e.printstacktrace ();}  catch  (certificateexception e)  {e.printstacktrace ();}  catch  (unrecoverablekeyexception e)  {e.printstacktrace ();} Return null;}}


This article is from the "Wandering Footsteps" blog, please make sure to keep this source http://now51jq.blog.51cto.com/3474143/1789539

HTTPS communication bidirectional authentication using the JDK's own SSL package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.