当前位置: 首页 > news >正文

Netty SSL双向验证

Netty SSL双向验证

  • 1. 环境说明
  • 2. 生成证书
    • 2.1. 创建根证书 密钥+证书
    • 2.2. 生成请求证书密钥
    • 2.3. 生成csr请求证书
    • 2.4. ca证书对server.csr、client.csr签发生成x509证书
    • 2.5. 请求证书PKCS#8编码
    • 2.6. 输出文件
  • 3. Java代码
    • 3.1. Server端
    • 3.2. Client端
    • 3.3. 证书存放
  • 4. 运行效果
    • 4.1. SSL客户端发送消息:
    • 4.2. 服务器收到SSL客户端消息:
    • 4.3. 非SSL客户端发送消息:
    • 4.4. 服务器收到非SSL客户端消息:
  • 5. References:

1. 环境说明

  • 本例使用windows10 + Win64OpenSSL-3_3_0(完整版,不是lite),netty版本4.1.77.Final,JDK-17
  • openssl官方推荐合作下载地址:https://slproweb.com/download/Win64OpenSSL-3_3_0.exe
  • ${openssl_home}是openssl的安装目录
  • 所有命令在${openssl_home}/bin目录下执行
  • windows下openssl的配置文件是${openssl_home}/bin/openssl.cfg,linux下是${openssl_home}/bin/openssl.conf,注意替换后缀名
  • 需要手动按照openssl.cfg的配置创建好各种目录、文件

2. 生成证书

2.1. 创建根证书 密钥+证书

openssl genrsa -des3 -out demoCA/private/ca.key 4096openssl req -new -x509 -days 3650 -key demoCA/private/ca.key -out demoCA/certs/ca.crt

2.2. 生成请求证书密钥

openssl genrsa -des3 -out demoCA/private/server.key 2048openssl genrsa -des3 -out demoCA/private/client.key 2048

2.3. 生成csr请求证书

openssl req -new -key demoCA/private/server.key -out demoCA/certs/server.csr -config openssl.cfgopenssl req -new -key demoCA/private/client.key -out demoCA/certs/client.csr -config openssl.cfg

2.4. ca证书对server.csr、client.csr签发生成x509证书

openssl x509 -req -days 3650 -in demoCA/certs/server.csr -CA demoCA/certs/ca.crt -CAkey demoCA/private/ca.key -CAcreateserial -out demoCA/certs/server.crtopenssl x509 -req -days 3650 -in demoCA/certs/client.csr -CA demoCA/certs/ca.crt -CAkey demoCA/private/ca.key -CAcreateserial -out demoCA/certs/client.crt

2.5. 请求证书PKCS#8编码

openssl pkcs8 -topk8 -in demoCA/private/server.key -out demoCA/private/pkcs8_server.key -nocryptopenssl pkcs8 -topk8 -in demoCA/private/client.key -out demoCA/private/pkcs8_client.key -nocrypt

2.6. 输出文件

server端:ca.crt、server.crt、pkcs8_server.key

client端:ca.crt、client.crt、pkcs8_client.key

3. Java代码

3.1. Server端

  • ServiceMain.java
public class ServiceMain implements CommandLineRunner {@Value("${netty.host}")private String host;@Value("${netty.port}")private int port;@Resourceprivate NettyServer nettyServer;public static void main(String[] args) {SpringApplication.run(ServiceMain.class, args);}@Overridepublic void run(String... args) throws Exception {InetSocketAddress address = new InetSocketAddress(host, port);ChannelFuture channelFuture = nettyServer.bind(address);Runtime.getRuntime().addShutdownHook(new Thread(() -> nettyServer.destroy()));channelFuture.channel().closeFuture().syncUninterruptibly();}
}
  • NettyServer.java
package cn.a.service.netty;import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.ssl.ClientAuth;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;import javax.annotation.Resource;
import java.io.File;
import java.net.InetSocketAddress;@Slf4j
@Component("nettyServer")
public class NettyServer {private final EventLoopGroup parentGroup = new NioEventLoopGroup();private final EventLoopGroup childGroup = new NioEventLoopGroup();private Channel channel;@ResourceApplicationContext applicationContext;/*** 绑定端口** @param address* @return*/public ChannelFuture bind(InetSocketAddress address) {ChannelFuture channelFuture = null;try {File certChainFile = ResourceUtils.getFile("classpath:server.crt");File keyFile = ResourceUtils.getFile("classpath:pkcs8_server.key");File rootFile = ResourceUtils.getFile("classpath:ca.crt");SslContext sslCtx = SslContextBuilder.forServer(certChainFile, keyFile).trustManager(rootFile).clientAuth(ClientAuth.REQUIRE).build();ServerBootstrap b = new ServerBootstrap();b.group(parentGroup, childGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024).childHandler(new NettyChannelInitializer(applicationContext, sslCtx));channelFuture = b.bind(address).syncUninterruptibly();channel = channelFuture.channel();} catch (Exception e) {log.error(e.getMessage());} finally {if (null != channelFuture && channelFuture.isSuccess()) {log.info("netty server start done.");} else {log.error("netty server start error.");}}return channelFuture;}/*** 销毁*/public void destroy() {if (null == channel) return;channel.close();parentGroup.shutdownGracefully();childGroup.shutdownGracefully();}/*** 获取通道** @return*/public Channel getChannel() {return channel;}
}
  • NettyChannelInitializer.java
package cn.a.service.netty;import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.ssl.SslContext;
import org.springframework.context.ApplicationContext;public class NettyChannelInitializer extends ChannelInitializer<SocketChannel> {private final ApplicationContext applicationContext;private final SslContext sslContext;public NettyChannelInitializer(ApplicationContext applicationContext, SslContext sslCtx) {this.applicationContext = applicationContext;this.sslContext = sslCtx;}@Overrideprotected void initChannel(SocketChannel channel) throws Exception {// 添加SSL安装验证channel.pipeline().addLast(sslContext.newHandler(channel.alloc()));//发送时编码channel.pipeline().addLast(new FrameEncoder());//接收时解码channel.pipeline().addLast(new FrameDecoder());//业务处理器channel.pipeline().addLast(new NettyMsgHandler(applicationContext));}
}

3.2. Client端

  • TestClientApp.java
package cn.a.service;import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.a.service.netty.FrameDecoder;
import cn.a.service.netty.FrameEncoder;
import cn.a.service.netty.NettyMsg;
import cn.a.service.netty.Session;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.SpringBootApplication;import java.io.File;
import java.util.Scanner;@Slf4j
@SpringBootApplication
public class TestClientApp {private static final Session session = new Session().setId(IdUtil.randomUUID());public static void main(String[] args) {new Thread(new TestThread("127.0.0.1", 7890)).start();}private static class TestThread implements Runnable {private final String serverHost;private final int serverPort;public TestThread(String serverHost, int serverPort) {this.serverHost = serverHost;this.serverPort = serverPort;}@Overridepublic void run() {EventLoopGroup group = new NioEventLoopGroup();try {final String certsDir = "D:\\GIT\\secim_service\\service\\src\\main\\resources\\";File certChainFile = new File(certsDir + "client.crt");File keyFile = new File(certsDir + "pkcs8_client.key");File rootFile = new File(certsDir + "ca.crt");SslContext sslCtx = SslContextBuilder.forClient().keyManager(certChainFile, keyFile).trustManager(rootFile).build();Bootstrap b = new Bootstrap();b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {protected void initChannel(SocketChannel ch) throws Exception {// 添加SSL安装验证ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()));ch.pipeline().addLast(new FrameEncoder());ch.pipeline().addLast(new FrameDecoder());ch.pipeline().addLast(new TestClientHandler(session));}});// 发起异步连接操作ChannelFuture f = b.connect(serverHost, serverPort);f.addListener(future -> {startConsoleThread(f.channel(), session);}).sync();// 等待客户端连接关闭f.channel().closeFuture().sync();} catch (Exception e) {e.printStackTrace();} finally {// 优雅退出,释放NIO线程组group.shutdownGracefully();}}}/*** 开启控制台线程** @param channel*/private static void startConsoleThread(Channel channel, Session session) {new Thread(() -> {while (!Thread.interrupted()) {log.info("输入指令:");Scanner scanner = new Scanner(System.in);String input;while (!"exit".equals((input = scanner.nextLine()))) {log.info("输入的命令是:{}", input);if (!NumberUtil.isInteger(input)) {log.error("输入的指令有误,请重新输入");continue;}NettyMsg nettyMsg;switch (Integer.parseInt(input)) {case 1:nettyMsg = TestMsgBuilder.buildIdentityMsg(session);break;default:log.error("无法识别的指令:{},请重新输入指令", input);nettyMsg = null;break;}if (null != nettyMsg) {channel.writeAndFlush(nettyMsg);}}}}).start();}
}

3.3. 证书存放

在这里插入图片描述

4. 运行效果

4.1. SSL客户端发送消息:

在这里插入图片描述

4.2. 服务器收到SSL客户端消息:

在这里插入图片描述

4.3. 非SSL客户端发送消息:

在这里插入图片描述

4.4. 服务器收到非SSL客户端消息:

在这里插入图片描述

5. References:

2020-07-14 15:01:55 小傅哥:netty案例,netty4.1中级拓展篇十三《Netty基于SSL实现信息传输过程中双向加密验证》

2017-07-04 11:44 骏马金龙:openssl ca(签署和自建CA)

相关文章:

Netty SSL双向验证

Netty SSL双向验证 1. 环境说明2. 生成证书2.1. 创建根证书 密钥证书2.2. 生成请求证书密钥2.3. 生成csr请求证书2.4. ca证书对server.csr、client.csr签发生成x509证书2.5. 请求证书PKCS#8编码2.6. 输出文件 3. Java代码3.1. Server端3.2. Client端3.3. 证书存放 4. 运行效果4…...

4.nginx反向代理、负载均衡

nginx反向代理、负载均衡 一、反向代理1、语法2、注意事项3、后端服务器记录客户端真实IP3.1 在nginx反向代理时添加x-real-ip字段3.2 后端httpd修改combined日志格式3.3 后端是nginx的情况 二、负载均衡 upstream模块1、负载均衡作用2、调度算法3、配置应用 一、反向代理 隐藏…...

浅谈申请小程序地理位置权限的正确打开方式

小程序地理位置接口有什么功能&#xff1f; 这篇内容会教大家如何快速申请“获取当前的地理位置&#xff08;onLocationChange&#xff09;”接口&#xff0c;以便帮助大家顺利开通接口。以下内容是本人经历了多次的申请经历得出来的经验&#xff0c;来之不易&#xff0c;望大家…...

make modules 和 make modules_install

1. make modules 如&#xff1a;make ARCHarm64 CROSS_COMPILEaarch64-linux-gnu- modules 直接make&#xff0c;不加任何参数&#xff0c;就是make all&#xff0c;包含make modules。 make modules是单独编译模块&#xff0c;驱动被配置成M的都是modules&#xff0c;modul…...

docker 安装mysql,redis,rabbitmq

文章目录 docker 安装ngnix&#xff0c;mysql,redis,rabbitmq安装docker1.安装下载docker-ce源命令2.安装docker3.查看版本4.查看docker状态5.启动docker6.测试安装ngnix 安装mysql8.0.361.拉取mysql镜像2.安装mysql8 安装redis1.拉取redis7.0.11镜像2.安装redis3.进入容器内部…...

Windows配置java环境JDK

配置jdk环境非常简单&#xff0c;大概有以下几步&#xff1a; 下载jdk安装&#xff0c;然后双击进行安装配置环境变量(也不是一定非要配置环境变量&#xff0c;配置环境变量的好处就是&#xff0c;在任何位置&#xff0c;系统都可以找到安装路径&#xff0c;非常实用且方便) …...

英伟达驱动重装教程

离线安装NVIDIA驱动程序通常涉及下载驱动程序安装包并手动执行安装步骤。以下是详细步骤: 1. 下载NVIDIA驱动程序 首先,你需要在有网络连接的计算机上下载适合你系统的NVIDIA驱动程序安装包。可以从NVIDIA官方驱动下载页面下载。 选择你的GPU型号和操作系统,然后下载相应…...

【蓝桥杯嵌入式】 第六届国赛

目录 题目 配置 注意事项 代码 - 默写大师 EEPROM读写函数 LED驱动函数 ADC采集 上电初始化 LCD 按键 PWM互补输出 全部代码 hardware.c hardware.h control.c control.h main.c 题目 配置 注意事项 复制LCD的工程&#xff0c;先配置资源 --- 勾选完选项一…...

图片裁剪与上传处理方案 —— 基于阿里云 OSS 处理用户资料

目录 01: 通用组件&#xff1a;input 构建方案分析 02: 通用组件&#xff1a;input 构建方案 03: 构建用户资料基础样式 04: 用户基本资料修改方案 05: 处理不保存时的同步问题 06: 头像修改方案流程分析 07: 通用组件&#xff1a;Dialog 构建方案分析 08: 通用组件&…...

迷你主机Esxi 6.7挂载新硬盘

背景 硬件&#xff1a;零刻SER Pro 6 系统&#xff1a;vmware Exsi 6.7.0 Update 3 现有的硬盘槽位占满了&#xff0c;但空间不够用&#xff0c;想要通过USB外接移动硬盘来进行扩容。使用了一块250G的硬盘做测试。 步骤 TL;DR # 停止usbarbitrator服务 /etc/init.d/usbarbi…...

解决VSCode右键没有Open In Default Browser问题

在VSCode进行Web小程序测试时&#xff0c;我们在新建的HTML文件中输入 !会自动生成页面代码骨架&#xff0c;写入内容后&#xff0c;我们想要右键在浏览器中预览。发现右键没有“Open In Default Browser”选项。原因是没有安装插件。 下面是解决方案&#xff1a;首先在VSCode找…...

httpsok-v1.12.0支持LB证书自动部署

&#x1f525;httpsok-v1.12.0支持LB证书自动部署 介绍 httpsok 是一个便捷的 HTTPS 证书自动续签工具&#xff0c;基于全新的设计理念&#xff0c;专为 Nginx 、OpenResty 服务器设计。已服务众多中小企业&#xff0c;稳定、安全、可靠。 一行命令&#xff0c;一分钟轻松搞…...

基于Pytorch框架的深度学习EfficientNetV2神经网络中草药识别分类系统源码

第一步&#xff1a;准备数据 5种中草药数据&#xff1a;self.class_indict ["百合", "党参", "山魈", "枸杞", "槐花", "金银花"] &#xff0c;总共有900张图片&#xff0c;每个文件夹单独放一种数据 第二步&a…...

网络协议。

一、流程案例 接下来揭秘我要说的大事情&#xff0c;“双十一”。这和我们要讲的网络协议有什么关系呢&#xff1f; 在经济学领域&#xff0c;有个伦纳德里德&#xff08;Leonard E. Read&#xff09;创作的《铅笔的故事》。这个故事通过一个铅笔的诞生过程&#xff0c;来讲述…...

Excel单元格格式无法修改的原因与解决方法

Excel单元格格式无法更改可能由多种原因造成。以下是一些可能的原因及相应的解决方法&#xff1a; 单元格或工作表被保护&#xff1a; 如果单元格或工作表被设置为只读或保护状态&#xff0c;您将无法更改其中的格式。解决方法&#xff1a;取消单元格或工作表的保护。在Excel中…...

CasaOS玩客云安装全平台高速下载器Gopeed并实现远程访问

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…...

JAVA学习-练习试用Java实现“最长回文子串”

问题&#xff1a; 给定一个字符串 s&#xff0c;找到 s 中最长的回文子串。 示例 1&#xff1a; 输入&#xff1a;s "babad" 输出&#xff1a;"bab" 解释&#xff1a;"aba" 同样是符合题意的答案。 示例 2&#xff1a; 输入&#xff1a;s …...

深入探索Qt框架系列之信号槽原理(三)

前面两篇分别介绍了QObject::connect和QMetaObject::Connection&#xff0c;那么信号槽机制的基础已经介绍完了&#xff0c;本文将介绍信号槽机制是如何从信号到槽的&#xff0c;以及多线程下是如何工作的。 信号槽机制源码解析 1. 信号的触发 以该系列的第一篇文章中的示例举…...

npm镜像源管理、nvm安装多版本node异常处理

查看当前使用的镜像源 npm config get registry --locationglobal 设置使用官方源 npm config set registry https://registry.npmjs.org/ --locationglobal 设置淘宝镜像源 npm config set registry https://registry.npm.taobao.org/ --locationglobal 需要更改淘宝镜像源地址…...

异步编程的魔力:如何显著提升系统性能

异步编程的魔力:如何显著提升系统性能 今天我们来聊聊一个对开发者非常重要的话题——异步编程。异步编程是提升系统性能的一种强大手段,尤其在需要高吞吐量和低时延的场景中,异步设计能够显著减少线程等待时间,从而提升整体性能。 异步设计如何提升系统性能? 我们通过…...

Objective-C常用命名规范总结

【OC】常用命名规范总结 文章目录 【OC】常用命名规范总结1.类名&#xff08;Class Name)2.协议名&#xff08;Protocol Name)3.方法名&#xff08;Method Name)4.属性名&#xff08;Property Name&#xff09;5.局部变量/实例变量&#xff08;Local / Instance Variables&…...

转转集团旗下首家二手多品类循环仓店“超级转转”开业

6月9日&#xff0c;国内领先的循环经济企业转转集团旗下首家二手多品类循环仓店“超级转转”正式开业。 转转集团创始人兼CEO黄炜、转转循环时尚发起人朱珠、转转集团COO兼红布林CEO胡伟琨、王府井集团副总裁祝捷等出席了开业剪彩仪式。 据「TMT星球」了解&#xff0c;“超级…...

Java多线程实现之Callable接口深度解析

Java多线程实现之Callable接口深度解析 一、Callable接口概述1.1 接口定义1.2 与Runnable接口的对比1.3 Future接口与FutureTask类 二、Callable接口的基本使用方法2.1 传统方式实现Callable接口2.2 使用Lambda表达式简化Callable实现2.3 使用FutureTask类执行Callable任务 三、…...

解决本地部署 SmolVLM2 大语言模型运行 flash-attn 报错

出现的问题 安装 flash-attn 会一直卡在 build 那一步或者运行报错 解决办法 是因为你安装的 flash-attn 版本没有对应上&#xff0c;所以报错&#xff0c;到 https://github.com/Dao-AILab/flash-attention/releases 下载对应版本&#xff0c;cu、torch、cp 的版本一定要对…...

IT供电系统绝缘监测及故障定位解决方案

随着新能源的快速发展&#xff0c;光伏电站、储能系统及充电设备已广泛应用于现代能源网络。在光伏领域&#xff0c;IT供电系统凭借其持续供电性好、安全性高等优势成为光伏首选&#xff0c;但在长期运行中&#xff0c;例如老化、潮湿、隐裂、机械损伤等问题会影响光伏板绝缘层…...

零基础在实践中学习网络安全-皮卡丘靶场(第九期-Unsafe Fileupload模块)(yakit方式)

本期内容并不是很难&#xff0c;相信大家会学的很愉快&#xff0c;当然对于有后端基础的朋友来说&#xff0c;本期内容更加容易了解&#xff0c;当然没有基础的也别担心&#xff0c;本期内容会详细解释有关内容 本期用到的软件&#xff1a;yakit&#xff08;因为经过之前好多期…...

Java线上CPU飙高问题排查全指南

一、引言 在Java应用的线上运行环境中&#xff0c;CPU飙高是一个常见且棘手的性能问题。当系统出现CPU飙高时&#xff0c;通常会导致应用响应缓慢&#xff0c;甚至服务不可用&#xff0c;严重影响用户体验和业务运行。因此&#xff0c;掌握一套科学有效的CPU飙高问题排查方法&…...

RSS 2025|从说明书学习复杂机器人操作任务:NUS邵林团队提出全新机器人装配技能学习框架Manual2Skill

视觉语言模型&#xff08;Vision-Language Models, VLMs&#xff09;&#xff0c;为真实环境中的机器人操作任务提供了极具潜力的解决方案。 尽管 VLMs 取得了显著进展&#xff0c;机器人仍难以胜任复杂的长时程任务&#xff08;如家具装配&#xff09;&#xff0c;主要受限于人…...

R 语言科研绘图第 55 期 --- 网络图-聚类

在发表科研论文的过程中&#xff0c;科研绘图是必不可少的&#xff0c;一张好看的图形会是文章很大的加分项。 为了便于使用&#xff0c;本系列文章介绍的所有绘图都已收录到了 sciRplot 项目中&#xff0c;获取方式&#xff1a; R 语言科研绘图模板 --- sciRplothttps://mp.…...

HubSpot推出与ChatGPT的深度集成引发兴奋与担忧

上周三&#xff0c;HubSpot宣布已构建与ChatGPT的深度集成&#xff0c;这一消息在HubSpot用户和营销技术观察者中引发了极大的兴奋&#xff0c;但同时也存在一些关于数据安全的担忧。 许多网络声音声称&#xff0c;这对SaaS应用程序和人工智能而言是一场范式转变。 但向任何技…...