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

效果

相关推荐
2501_916008896 小时前
iOS 抓包工具有哪些?全面盘点主流工具与功能对比分析
android·ios·小程序·https·uni-app·iphone·webview
2501_915921436 小时前
iOS混淆工具实战 视频流媒体类 App 的版权与播放安全保护
android·ios·小程序·https·uni-app·iphone·webview
XMYX-07 小时前
解决 Apache/WAF SSL 证书链不完整导致的 PKIX path building failed 问题
网络协议·apache·ssl
2501_9160088910 小时前
uni-app iOS 日志与崩溃分析全流程 多工具协作的实战指南
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063215 小时前
Charles抓包工具在接口性能优化与压力测试中的实用方法
ios·性能优化·小程序·https·uni-app·压力测试·webview
智能化咨询20 小时前
基于网络原理——HTTP/HTTPS的Web服务搭建与核心技术实践
网络·http·https
web前端神器1 天前
记录一下node后端写下载https的文件报错,而浏览器却可以下载。
网络协议·http·https
少陽君1 天前
什么是CA根证书
服务器·https·ssl
2501_915909061 天前
uni-app iOS 上架常见问题与解决方案,实战经验全解析
android·ios·小程序·https·uni-app·iphone·webview
云霄IT1 天前
CentOS7安装Nginx服务——为你的网站配置https协议和自定义服务端口
服务器·nginx·https