Jetty9 配置使用HTTPS證書

公司目前服務(wù)器用的是Jetty?9.2.5.v20141112版本膛薛,有一天跟我說需要加上HTTPS补鼻,查找很多文檔后才找到一個方法哄啄,將完整方法分享給大家风范。

Jetty 需要使用的Key文件為keystore,而各大服務(wù)商申請的Key文件一般為pem等文件硼婿。


一、申請Key證書

? ? ?這個部分就省略不講了拳喻,一般阿里云猪腕、騰訊云等等服務(wù)商都有免費的證書申請。


二陋葡、轉(zhuǎn)換證書格式

? ? 1.將pfx格式證書轉(zhuǎn)換為jks格式證書

? ? ? ? windows打開CMD命令行窗口

? ? ? keytool -importkeystore -srckeystore 你的證書.pfx -destkeystore 你的證書.jks -srcstoretype PKCS12 -deststoretype JKS

? ?2.將jks格式證書轉(zhuǎn)換為p12格式證書

? ? ? ? 通過JAVA代碼進行轉(zhuǎn)換

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.security.Key;

import java.security.KeyStore;

import java.security.cert.Certificate;

import java.util.Enumeration;

public class KeyZ {

????// 證書格式

????public static final String JKS = "JKS";

????public static final String PKCS12 = "PKCS12";

????// 證書和路徑

????public static final String INPUT_KEYSTORE_FILE = "e:/你的證書/your-name.jks";

????public static final String KEYSTORE_PASSWORD = "你的證書密碼";

????public static final String OUTPUT_KEYSTORE_FILE = "e:/你的證書/你的證書.p12";

????// 證書別名

????public static final String CERT_ALIAS = "client"; /

????** * @param args */

????public static void main(String[] args) throws Exception{

????????KeyStore inputKeyStore = KeyStore.getInstance(JKS);

????????FileInputStream fis = new FileInputStream(INPUT_KEYSTORE_FILE);

????????char[] nPassword = KEYSTORE_PASSWORD.toCharArray();

????????inputKeyStore.load (fis, nPassword);

????????fis.close();

????????System.out.println("keystore type=" + inputKeyStore.getType());

????????KeyStore outputKeyStore = KeyStore.getInstance(PKCS12);

????????outputKeyStore.load(null, KEYSTORE_PASSWORD.toCharArray());

????????Enumeration enumStrs = inputKeyStore.aliases();

????????while (enumStrs.hasMoreElements()){

????????????String keyAlias = enumStrs.nextElement();

????????????System.out.println("alias=[" + keyAlias + "]");

????????????if (inputKeyStore.isKeyEntry(keyAlias)) {

????????????????Key key = inputKeyStore.getKey(keyAlias, nPassword);

????????????????Certificate[] certChain = inputKeyStore.getCertificateChain(keyAlias);

????????????????outputKeyStore.setKeyEntry(CERT_ALIAS, key, KEYSTORE_PASSWORD.toCharArray(), certChain);

????????????}

????????????}

????????FileOutputStream out = new FileOutputStream(OUTPUT_KEYSTORE_FILE);

????????outputKeyStore.store(out, nPassword); out.close();

????????}

????}

? ? 3.將p12證書格式轉(zhuǎn)換為 keystore文件格式

? ? ? ? 打開CMD窗口

? ??????keytool -importkeystore -v -srckeystore 你的證書.p12 -srcstoretype pkcs12 -srcstorepass 你的證書密碼 -destkeystore 你的證書.keystore -deststoretype jks -deststorepass 你的證書密碼


三捌归、Jetty配置

? ? 1.運行java -jar ..\jetty-distribution-9.2.5.v20141112\start.jar --add-to-start=https

????????????java -jar ..\jetty-distribution-9.2.5.v20141112\start.jar --add-to-start=ssl

? ? 2.將證書放置在jetty的etc/cert中

? ? 3.打開start.ini

? ? ? ? 發(fā)現(xiàn)已經(jīng)有https和SSL兩個模塊

# --------------------------------------- #Module: ssl

--module=ssl

### SSL Keystore Configuration

# define the port to use for secure redirection

jetty.secure.port=8999? #安全端口自己配置

## Setup a demonstration keystore and truststore

jetty.keystore=etc/cert/ 你的證書.keystore

jetty.truststore=etc/cert/你的證書.keystore

## Set the demonstration passwords.

## Note that OBF passwords are not secure, just protected from casual observation

## See http://www.eclipse.org/jetty/documentation/current/configuring-security-secure-passwords.html

jetty.keystore.password= 你的證書密碼

jetty.keymanager.password= 你的證書密碼

jetty.truststore.password= 你的證書密碼

### Set the client auth behavior

## Set to true if client certificate authentication is required

# jetty.ssl.needClientAuth=true

## Set to true if client certificate authentication is desired

# jetty.ssl.wantClientAuth=true

## Parameters to control the number and priority of acceptors and selectors

# ssl.selectors=1

# ssl.acceptors=1

# ssl.selectorPriorityDelta=0

# ssl.acceptorPriorityDelta=0

# --------------------------------------- #Module: https

--module=https

## HTTPS Configuration

# HTTP port to listen on

https.port=8999? #端口與上面保持一致

# HTTPS idle timeout in milliseconds

https.timeout=30000

# HTTPS Socket.soLingerTime in seconds. (-1 to disable)

# https.soLingerTime=-1


至此惜索,重啟服務(wù)器剃浇,HTTPS就配置成功了,可以試試看用https端口是否能夠成功訪問

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末虎囚,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子圃伶,更是在濱河造成了極大的恐慌,老刑警劉巖窒朋,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異魄揉,居然都是意外死亡拭宁,警方通過查閱死者的電腦和手機瓣俯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來彩匕,“玉大人,你說我怎么就攤上這事掸犬⌒靼郑” “怎么了?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵奠货,是天一觀的道長。 經(jīng)常有香客問我柔滔,道長萍虽,這世上最難降的妖魔是什么睛廊? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任超全,我火速辦了婚禮王财,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘绒净。我一直安慰自己,他們只是感情好挂疆,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著宝当,像睡著了一般。 火紅的嫁衣襯著肌膚如雪庆揩。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天虏辫,我揣著相機與錄音锈拨,去河邊找鬼。 笑死奕枢,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的萌焰。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼杆怕,長吁一口氣:“原來是場噩夢啊……” “哼壳贪!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起违施,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤磕蒲,失蹤者是張志新(化名)和其女友劉穎留潦,沒想到半個月后辣往,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡坊萝,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了菩鲜。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惦积。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖狮崩,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情厉亏,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站招刹,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏训柴。R本人自食惡果不足惜妇拯,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望越锈。 院中可真熱鬧,春花似錦甘凭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至坯苹,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背闸翅。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工菊霜, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鉴逞。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像液南,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子滑凉,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容