第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有童鞋遇到過同樣的問題: netty怎么才能 同時支持HTTP與HTTPS

有沒有童鞋遇到過同樣的問題: netty怎么才能 同時支持HTTP與HTTPS

德瑪西亞99 2019-06-13 09:56:24
在netty中添加自帶的SslHandler就能支持HTTPS,但是添加之后使用HTTP訪問是存在問題的。請問如何能支持使用用一個端口兩種協(xié)議并行,比如在某個事件中判斷出使用HTTPS協(xié)議然后在把SslHandler添加到pipeline中。SelfSignedCertificatessc=newSelfSignedCertificate();SslContextsslCtx=SslContextBuilder.forServer(ssc.certificate(),ssc.privateKey()).build();SSLEnginesslEngine=sslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);ch.pipeline().addFirst(newSslHandler(sslEngine));
查看完整描述

2 回答

?
智慧大石

TA貢獻1946條經(jīng)驗 獲得超3個贊

根據(jù)node如何讓一個端口同時支持https與http文中描述的https數(shù)據(jù)流的第一位是十六進制“16”,轉(zhuǎn)換成十進制就是22,在讀取的第一位數(shù)據(jù)進行判斷實現(xiàn)動態(tài)添加ChannelHandler。
.childHandler(newChannelInitializer(){
protectedvoidinitChannel(finalNioSocketChannelch)throwsException{
ch.pipeline().addFirst(newChannelInboundHandlerAdapter(){
@Override
publicvoidchannelRead(ChannelHandlerContextctx,Objectmsg)throwsException{
if(((ByteBuf)msg).getByte(0)==22){
SelfSignedCertificatessc=newSelfSignedCertificate();
SslContextsslCtx=SslContextBuilder.forServer(ssc.certificate(),ssc.privateKey()).build();
SSLEnginesslEngine=sslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);
//將處理https的處理程序添加至HttpServerCodec之前
ctx.pipeline().addBefore("HttpServerCodec","sslHandler",newSslHandler(sslEngine));
}
ctx.pipeline().remove(this);
super.channelRead(ctx,msg);
}
});
ch.pipeline().addLast("HttpServerCodec",newHttpServerCodec());
ch.pipeline().addLast("aggregator",newHttpObjectAggregator(10*1024*1024));
ch.pipeline().addLast(newHttpServerHandler());
}
});
反之先添加sslHandler在移除也行
.childHandler(newChannelInitializer(){
protectedvoidinitChannel(finalNioSocketChannelch)throwsException{
ch.pipeline().addFirst(newChannelInboundHandlerAdapter(){
@Override
publicvoidchannelRead(ChannelHandlerContextctx,Objectmsg)throwsException{
if(((ByteBuf)msg).getByte(0)!=22){
//刪除sslHandler
ctx.pipeline().remove("sslHandler");
}
ctx.pipeline().remove(this);
super.channelRead(ctx,msg);
}
});
SelfSignedCertificatessc=newSelfSignedCertificate();
SslContextsslCtx=SslContextBuilder.forServer(ssc.certificate(),ssc.privateKey()).build();
SSLEnginesslEngine=sslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);
ch.pipeline().addLast("sslHandler",newSslHandler(sslEngine));
ch.pipeline().addLast("HttpServerCodec",newHttpServerCodec());
ch.pipeline().addLast("aggregator",newHttpObjectAggregator(10*1024*1024));
ch.pipeline().addLast(newHttpServerHandler());
}
});
                            
查看完整回答
反對 回復(fù) 2019-06-13
  • 2 回答
  • 0 關(guān)注
  • 469 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號