netty配置SSL、netty配置https(开发)

netty配置SSL、netty配置https(开发)

我们在开发下使用ssl,所用的证书将不被客户端信任。

转自:https://lingkang.top/archives/netty-pei-zhi-ssl

方案一

快速。使用netty提供的临时签发证书

java 复制代码
    private static SslContext sslContext = null;
    public ServerChannelHandler(RouterConfig config) {
        this.config = config;
        try {
            if (sslContext != null)
                return;
            // 使用临时签发的一个证书
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslContext = SslContextBuilder.forServer(ssc.certificate(),
                    ssc.privateKey()).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

配置

java 复制代码
@Slf4j
public class ServerChannelHandler extends ChannelInitializer<SocketChannel> {
    private final RouterConfig config;
    private static SslContext sslContext = null;

    public ServerChannelHandler(RouterConfig config) {
        this.config = config;
        try {
            if (sslContext != null)
                return;
            // 使用临时签发的一个证书
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslContext = SslContextBuilder.forServer(ssc.certificate(),
                    ssc.privateKey()).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        // 使用netty临时签发的证书
        pipeline.addLast(sslContext.newHandler(ch.alloc()));

        pipeline.addLast(new HttpServerCodec());// http编解码
        pipeline.addLast(new HttpObjectAggregator(config.getMaxContentLength()));
        pipeline.addLast(new DispatcherHandler(config));
    }
}

访问时需要你提前加上https,不会为你自动重定向到https
https://localhost:9595/

但是此时你发现控制台:

这是因为证书不被客户端信任,如果你是使用 logback日志,可以配置忽略它:
logback.xmlconfiguration中添加

xml 复制代码
<logger name="io.netty.channel.DefaultChannelPipeline" level="OFF"/>

上面的警告日志将不再打印,nice~

方案二

使用jdk自签一个jks证书:

shell 复制代码
keytool -genkeypair -alias lk -keyalg RSA -keypass 123456 -storepass 123456 -keyalg RSA -keysize 2048 -validity 3650 -keystore lk.jks

配置ssl

java 复制代码
@Slf4j
public class ServerChannelHandler extends ChannelInitializer<SocketChannel> {
    private final RouterConfig config;

    public ServerChannelHandler(RouterConfig config) {
        this.config = config;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        // ssl, 每次访问都要加载新的 SSLEngine 对象
        SSLEngine sslEngine = getSslContext().createSSLEngine();
        // 声明服务端
        sslEngine.setUseClientMode(false);
        pipeline.addLast(new SslHandler(sslEngine));

        pipeline.addLast(new HttpServerCodec());// http编解码
        pipeline.addLast(new HttpObjectAggregator(config.getMaxContentLength()));
        pipeline.addLast(new DispatcherHandler(config));
    }

    private static SSLContext getSslContext() throws Exception {
        // 密码
        char[] passArray = "123456".toCharArray();
        SSLContext sslContext = SSLContext.getInstance("TLSv1");
        KeyStore ks = KeyStore.getInstance("JKS");
        //加载keytool 生成的文件
        FileInputStream inputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\temp\\ssl\\lk.jks");
        ks.load(inputStream, passArray);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmf.init(ks, passArray);
        sslContext.init(kmf.getKeyManagers(), null, null);
        inputStream.close();
        return sslContext;
    }
}

效果

相关推荐
诗句藏于尽头1 小时前
完成ssl不安全警告
网络协议·安全·ssl
idolyXyz4 小时前
[netty5: WebSocketFrameEncoder & WebSocketFrameDecoder]-源码解析
netty
拾光拾趣录16 小时前
无状态协议下的用户状态管理:Web应用如何保持用户登录态
前端·http·https
游戏开发爱好者81 天前
iOS重构期调试实战:架构升级中的性能与数据保障策略
websocket·网络协议·tcp/ip·http·网络安全·https·udp
tmacfrank1 天前
Android 网络全栈攻略(四)—— TCPIP 协议族与 HTTPS 协议
android·网络·https
2501_916013741 天前
iOS 多线程导致接口乱序?抓包还原 + 请求调度优化实战
websocket·网络协议·tcp/ip·http·网络安全·https·udp
idolyXyz1 天前
[netty5: HttpObjectEncoder & HttpObjectDecoder]-源码解析
netty
charlee442 天前
nginx部署发布Vite项目
nginx·性能优化·https·部署·vite
2501_915921432 天前
Fiddler 中文版怎么配合 Postman 与 Wireshark 做多环境接口调试?
websocket·网络协议·tcp/ip·http·网络安全·https·udp
游戏开发爱好者82 天前
iOS App首次启动请求异常调试:一次冷启动链路抓包与初始化流程修复
websocket·网络协议·tcp/ip·http·网络安全·https·udp