Netty websocket配置wss

时间:2024-04-25 13:37:34

录音配置https 导致ws连不上

@Slf4j
@Component
public class NettyServer {

    /**
     * 启动
     *
     * @throws InterruptedException
     */
    private void start() throws InterruptedException {
        bossGroup = new NioEventLoopGroup();
        workGroup = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        // bossGroup辅助客户端的tcp连接请求, workGroup负责与客户端之前的读写操作
        bootstrap.group(bossGroup, workGroup);
        // 设置NIO类型的channel
        bootstrap.channel(NioServerSocketChannel.class);
        // 设置监听端口
        bootstrap.localAddress(new InetSocketAddress(port));
        // 连接到达时会创建一个通道
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
               // 添加SSL处理器
                FileInputStream crt = new FileInputStream("/etc/app/ssl/test.xx-zn.com_bundle.crt");
                FileInputStream key = new FileInputStream("/etc/app/ssl/test.xx-zn.com.key");
                SslContext sslContext = SslContextBuilder.forServer(crt, key)
                        .sslProvider(SslProvider.JDK)
                        .build();
                ch.pipeline().addLast(sslContext.newHandler(ch.alloc()));
               
    }

https 和wss用的是同一个域名证书(企鹅),Java 配置WSS ssl处理器,启动程序发现报错

无法找ssl文件的key,发现格式不匹配。下载openssl转 ssl文件中.key 到pkcs8格式

openssl工具下载地址:

Win32/Win64 OpenSSL Installer for Windows - Shining Light Productionsp

 配置openssl环境变量,win path 后面加安装目录 D:\zhang\OpenSSL-Win64\bin

打开cmd 执行openssl pkcs8 -topk8 -nocrypt -in 你的.key文件 -out pkcs8.key

备注:ssl证书给的.key文件就是pkcs1格式文件