Jsse Programming Basic code

Source: Internet
Author: User

 public static void main (String[] args)  throws Exception {                //NULL implementation, accept the certificate of any server. If the trusted Public key library is used to construct, the trustmanagerfactory of keymanagerfactory        //is used.        trustmanager tm = new x509trustmanager ()  {          @Override          Public void checkclienttrusted (X509certificate[] chain, string authtype)  throws  CertificateException {          //todo                        }         @Override          public void checksErvertrusted (X509certificate[] chain, string authtype)  throws certificateexception  {           //todo           }         @Override          public x509certificate[] getacceptedissuers ()  {             return null;         }       };               sslcontext sslcontext = sslcontext.getinstance ("SSL");               //'s own certificate requires a KeyStore, Can't construct        KeyStore ks =  like TrustManager. Keystore.getinstance ("JKS");      &nbsP; ks.load (New fileinputstream ("Keystore.jks"), "password". ToCharArray ());               KeyManagerFactory kmf =  Keymanagerfactory.getinstance ("SunX509");        kmf.init (ks,  "password ". ToCharArray ());               Keymanager km = new mykeymanager ((X509keymanager) kmf.getkeymanagers () [0]);                       sslcontext.init (New keymanager[]{km}, new trustmanager[]{tm}, new securerandom ( ));              sslsocket ss  =   (Sslsocket) sslcontext.getsocketfactory (). Createsocket ("localhost", 8080);                //Monitor handshake completion event.        ss.addhandshakecompletedlistener (New handshakecompletedlistener ()  {          @Override          public void handshakecompleted (handshakecompletedevent event)  {             system.out.println (Event);         }       });            }
Public class mykeymanager implements x509keymanager {    private  x509keymanager delegate;        public mykeymanager ( X509keymanager delegate) {        this.delegate =  delegate;    }     @Override     public string[ ] getclientaliases (string keytype, principal[] issuers)  {         return delegate.getclientaliases (keytype, issuers);     }      @Override     public string chooseclientalias (string[]  keytype, principal[] issuers, socket socket)  {         return delegate.chooseclientalias (Keytype, issuers, socket);     }   &nbSP; @Override     public string[] getserveraliases (string keytype,  Principal[] issuers)  {        return  Delegate.getserveraliases (keytype, issuers);    }     @Override     public string chooseserveralias (string keytype, principal[]  Issuers, socket socket)  {        return  Delegate.chooseserveralias (Keytype, issuers, socket);    }      @Override     public x509certificate[] getcertificatechain (String  Alias)  {        return delegate.getcertificatechain (alias);     }     @Override     public PrivateKey  Getprivatekey (String alias)  {         return delegate.getprivatekey (alias);     }} 


This article is from the "No Thieves" blog, please be sure to keep this source http://guojuanjun.blog.51cto.com/277646/1955239

Jsse Programming Basic code

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.