sslHandler创建
HAZYUHE opened this issue · comments
HazYuhe commented
渔民小镇 commented
https://www.yuque.com/iohao/game/ea6geg#u5rfY
netty SSL
public class Example {
static ExternalServer createExternalServer() {
int port = 10100;
DefaultExternalServerBuilder builder = DefaultExternalServer.newBuilder(port);
// your sslContext
SslContext sslContext = getSslContext();
// 对 netty ChannelHandler 编排;重写 MicroBootstrapFlow 的 pipelineCustom 方法
builder.setting().setMicroBootstrapFlow(new WebSocketMicroBootstrapFlow() {
@Override
public void pipelineCustom(PipelineContext context) {
// 使用已有的 handler
super.pipelineCustom(context);
if (context instanceof DefaultPipelineContext defaultPipelineContext) {
Channel channel = defaultPipelineContext.channel();
// 添加 SSL/TLS 处理器
SslHandler sslHandler = sslContext.newHandler(channel.alloc());
context.addFirst(sslHandler);
}
}
});
return builder.build();
}
private static SslContext getSslContext() {
try {
// 伪代码 - 创建 SslContext
SelfSignedCertificate ssc = new SelfSignedCertificate();
return SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
} catch (CertificateException | SSLException e) {
throw new RuntimeException(e);
}
}
}