目录

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;
    }
}

效果

本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
爬台阶的蚂蚁4 小时前
搭建docker registry私服,并且支持https推送
docker·容器·https
Lorin 洛林8 小时前
修复SSL证书链不完整问题certificate verify failed unable to get local issuer certificate
数据库·网络协议·ssl
Aa美少女战士11 小时前
企业如何选择通配符 SSL 证书?
网络协议·https·ssl
pingxiaozhao1 天前
在内网环境中为 Gogs 配置 HTTPS 访问
网络协议·http·https
所待.3831 天前
网络原理 - HTTP/HTTPS
网络·http·https
iOS技术狂热者2 天前
wireshak抓手机包 wifi手机抓包工具
websocket·网络协议·tcp/ip·http·网络安全·https·udp
java搬砖工-苤-初心不变2 天前
解决 Git 通过 SSH 克隆仓库时自动转换为 HTTPS 的问题
git·https·ssh
测试界吖吖2 天前
HTTPS 之fiddler抓包--jmeter请求
自动化测试·软件测试·功能测试·jmeter·程序人生·https·fiddler
信徒_2 天前
一个完整的 HTTP/HTTPS 请求流程
网络协议·http·https
陌路物是人非3 天前
SpringBoot + Netty + Vue + WebSocket实现在线聊天
vue.js·spring boot·websocket·netty