<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    走自己的路

    路漫漫其修遠(yuǎn)兮,吾將上下而求索

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      50 隨筆 :: 4 文章 :: 118 評(píng)論 :: 0 Trackbacks
     

    網(wǎng)上大多數(shù)文章都是用keytool生成自簽名根證書,將根證書配置在tomcatserver.xml中。我不太喜歡用keytool,原因:
    1.
    我們可能換provider,不同的provider會(huì)有不同的算法實(shí)現(xiàn),算法的安全性和性能也可能不同,通過代碼生成比較方便一些,不同算法的實(shí)現(xiàn)要放在classpath上。
    2.
    通過代碼生成還有一個(gè)好處,會(huì)對(duì)整個(gè)流程理解的比較清楚,實(shí)現(xiàn)的原理到底是怎樣的。

     

    要用到https,也就是TLS或者SSL,我們需要有證書,要么是法定證書機(jī)構(gòu)(VeriSign,中國(guó)估計(jì)也有代理)給你簽發(fā)的可信證書,要么自己給自己頒發(fā)一個(gè)根證書。自己給自己頒發(fā)的證書,瀏覽器是不信任的,會(huì)彈出一個(gè)提示框。

     

    SSL認(rèn)證分為雙向認(rèn)證和單向認(rèn)證(客戶端認(rèn)證服務(wù)器),一般做網(wǎng)站單向認(rèn)證就可,客戶端要認(rèn)證服務(wù)器端的證書,認(rèn)證通過,通過非對(duì)稱加密算法交換秘密密鑰,以后的通信數(shù)據(jù)通過秘密密鑰加密。

     

    所以說要想用https,就得現(xiàn)有證書。有證書就得現(xiàn)有公私鑰。

        public static KeyPair generateKeyPair() throws NoSuchAlgorithmException,

               NoSuchProviderException {

           // create the keys

           KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");

           generator.initialize(1024, new SecureRandom());

           return generator.generateKeyPair();

        }

     

     

     

    有了公私鑰,接著就生成證書。

        public static X509Certificate generateX509V3RootCertificate(KeyPair pair)

               throws NoSuchAlgorithmException, NoSuchProviderException,

               CertificateEncodingException, InvalidKeyException,

               IllegalStateException, SignatureException {

           X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();

            certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));

           certGen.setIssuerDN(new X500Principal(

                  "CN=localhost, OU=Ldd600 Blog, O=SHA, C=cn"));

           certGen.setNotBefore(new Date(System.currentTimeMillis() - 5000L));

           certGen.setSubjectDN(new X500Principal(

                  "CN=localhost, OU=Ldd600 Blog, O=SHA, C=cn"));

           certGen.setPublicKey(pair.getPublic());

           certGen.setSignatureAlgorithm("SHA1WithRSA");

           certGen.setNotAfter(new Date(System.currentTimeMillis()

                  + Integer.MAX_VALUE));

           return certGen.generate(pair.getPrivate(), new SecureRandom());

        }

     

        public static X500PrivateCredential createRootCredential(KeyPair rootPair)

               throws Exception {

           X509Certificate rootCert =

                  generateX509V3RootCertificate(rootPair);

           return new X500PrivateCredential(rootCert, rootPair.getPrivate());

        }

     

    有了證書,我們要將證書存儲(chǔ)起來(lái),根證書是自簽名的證書,凡是通過根證書簽名頒發(fā)的證書都是可信任的。根證書需要添加到信任證書鏈中。而根證書我們自己給自己簽名的證書是給SSL協(xié)議用的。

    KeyStore是用來(lái)保存key,證書的。

    Tomcatkeystore有兩個(gè)

    Server keystore: 存放的是服務(wù)器用的公私鑰key

    Trust keystore:存放的是所有確定信任的證書。自己給自己頒發(fā)的證書當(dāng)然是值得我們自己信任的。以后可以用來(lái)認(rèn)證通信的另外一方,不過單向認(rèn)證應(yīng)該用不到,

     

    publicstaticvoid main(String[] args) throws Exception {

          

           //trustsotre, my root certificate

           KeyStore store = KeyStore.getInstance("JKS");

           // initialize

            store.load(null, null);

            KeyPair rootPair = generateKeyPair();

           X500PrivateCredential rootCredential = createRootCredential(rootPair);

           store.setCertificateEntry(TRUST_STORE_NAME, rootCredential.getCertificate());

           store.store(

                    new FileOutputStream(TRUST_STORE_NAME + ".jks"),

                    TRUST_STORE_PASSWORD);

           // server credentials

           store = KeyStore.getInstance("JKS");

           store.load(null, null);

            store.setKeyEntry(

              SERVER_NAME, rootCredential.getPrivateKey(), SERVER_PASSWORD,

                    new Certificate[] { rootCredential.getCertificate() });

            store.store(

              new FileOutputStream(SERVER_NAME + ".jks"), SERVER_PASSWORD);

        }

     

    KeyStore文件配置在tomcatserver.xml

      <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"

                   maxThreads="150" scheme="https" secure="true"

                   clientAuth="false" sslProtocol="TLS"

                     keystoreFile="conf/server.jks" keystorePass="serverPassword" truststoreFile ="conf/trustStore.jks" truststorePass="trustPassword"/>

     

    啟動(dòng)tomcat即可

     

    打開URL看看效果吧。



     

    點(diǎn)是,就可以打開網(wǎng)頁(yè)了。

     



    posted on 2010-08-14 01:06 叱咤紅人 閱讀(2746) 評(píng)論(1)  編輯  收藏 所屬分類: Other Java and J2EE frameworks

    評(píng)論

    # re: 不用keytool,tomcat打開https 2010-08-16 10:22 shaiehv
    曙海嵌入式學(xué)院提供以下課程的培訓(xùn)--中國(guó)最大的FPGA,DSP和3G手機(jī)通信培訓(xùn)機(jī)構(gòu):
    FPGA培訓(xùn),DSP培訓(xùn),MTK培訓(xùn),Android培訓(xùn),Symbian培訓(xùn),iPhone培訓(xùn),單片機(jī)培訓(xùn),Candence PCB培訓(xùn),Vxworks培訓(xùn)等。
    網(wǎng)址: http://www.51qianru.cn
    上海總部電話:021-51875830 深圳:0755-61280252 北京:010-51292078 南京:025-68662821  回復(fù)  更多評(píng)論
      

    主站蜘蛛池模板: 黄页网址大全免费观看12网站| 精品久久久久久亚洲| 5555在线播放免费播放| 久久精品成人免费看| 久久久久成人精品免费播放动漫| 日韩精品无码免费专区午夜| 中国内地毛片免费高清| 99精品免费视品| 久久成人免费大片| 114级毛片免费观看| 永久免费AV无码国产网站| 全免费一级毛片在线播放| 国产三级电影免费观看| 青青草原亚洲视频| 久久精品国产亚洲沈樵| 日木av无码专区亚洲av毛片| 亚洲福利一区二区| 亚洲天堂免费在线| 国产偷国产偷亚洲高清人| 男女作爱免费网站| 成全视频免费观看在线看| 7m凹凸精品分类大全免费| 美女网站免费福利视频| 成年女人毛片免费播放人| 全部免费毛片在线| 亚洲中文字幕不卡无码| 91久久亚洲国产成人精品性色| 亚洲一区二区三区亚瑟 | 国产最新凸凹视频免费| 亚洲国产精品日韩专区AV| 亚洲国产精品成人精品无码区 | 国产aa免费视频| 国产亚洲精久久久久久无码AV| 亚洲av日韩av无码| 亚洲制服丝袜一区二区三区| 亚洲AV永久无码天堂影院| 免费夜色污私人影院网站| 日本免费大黄在线观看| 好吊妞998视频免费观看在线| 亚洲精品成人区在线观看| 亚洲高清视频在线观看|