1. 程式人生 > >不用keytool,tomcat開啟https

不用keytool,tomcat開啟https

 

要用到https,也就是TLS或者SSL,我們需要有證書,要麼是法定證書機構(VeriSign,中國估計也有代理)給你簽發的可信證書,要麼自己給tomcat 配置https自己頒發一個根證書。自己給自己頒發的證書,瀏覽器是不信任的,會彈出一個提示框。

SSL認證分為雙向認證和單向認證(客戶端認證伺服器),一般做網站單向tomcat6 https認證就可,客戶端要認證伺服器端的證書,認證通過,通過非對稱加密演算法交換祕密金鑰,以後的通訊資料通過祕密金鑰加密。

所以說要想用https,就得現有證書。有證書就得現有公私鑰。

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());

}

有了證書,我們要將證書儲存起來,根證書是自簽名的證書,凡是通過根證書籤名頒發的證書都是可信任的。根證書需要新增到信任證書鏈中。而根證書我們自己給自己簽名的證書是給SSL協議用的。

KeyStore是用來儲存key,證書的。

Tomcat的keystore有兩個

Server keystore: 存放的是伺服器用的公私鑰key

Trust keystore:存放的是所有確定信任的證書。自己給自己頒發的證書當然是值得我們自己信任的。以後可以用來認證通訊的另外一方,不過單向認證應該用不到,

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檔案配置在tomcat的server.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”/>

啟動tomcat即可

開啟URL看看效果吧。

點是,就可以開啟網頁了。

Related posts: