netty ssl 服务器
一、证书
(本文只介绍windows版,其他系统只供参考)
1.生成ca证书
下载 openssl 并安装 未编译 编译好
在
openssl/bin
目录下打开命令行,输入
openssl req -new -x509 -keyout ca.key -out ca.crt -config openssl.cnf
在本目录得到 ca.key 和 ca.crt 文件
2.生成服务端和客户端私钥
命令行输入
openssl genrsa -des3 -out server.key 1024
openssl genrsa -des3 -out client.key 1024
密码自己设定,好几个密码,别弄乱了就好,分不清的话都设成一样的
3.根据 key 生成 csr 文件
openssl req -new -key server.key -out server.csr -config openssl.cnf
openssl req -new -key client.key -out client.csr -config openssl.cnf
4.根据 ca 证书 server.csr 和 client.csr 生成 x509 证书
openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
openssl x509 -req -days 3650 -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt
5.将 key 文件进行 PKCS#8 编码
openssl pkcs8 -topk8 -in server.key -out pkcs8_server.key -nocrypt
openssl pkcs8 -topk8 -in client.key -out pkcs8_client.key -nocrypt
最后得到有用的文件分别为
服务器端: ca.crt、server.crt、pkcs8_server.key
客户端端: ca.crt、client.crt、pkcs8_client.key
二、服务器端代码
Main.java
public class Main {private static final int m_port = 23333;public void run() throws Exception {File certChainFile = new File(".\\ssl\\server.crt");File keyFile = new File(".\\ssl\\pkcs8_server.key");File rootFile = new File(".\\ssl\\ca.crt");SslContext sslCtx = SslContextBuilder.forServer(certChainFile, keyFile).trustManager(rootFile).clientAuth(ClientAuth.REQUIRE).build();EventLoopGroup bossGroup = new NioEventLoopGroup(1);EventLoopGroup workerGroup = new NioEventLoopGroup();try {ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new Initializer(sslCtx));ChannelFuture f = b.bind(m_port).sync();f.channel().closeFuture().sync();} finally {bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}public static void main(String[] args) throws Exception {new Main().run();}
}
Initializer.java
public class Initializer extends ChannelInitializer<SocketChannel> {private final SslContext sslCtx;public Initializer(SslContext sslCtx) {this.sslCtx = sslCtx;}@Overridepublic void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast(sslCtx.newHandler(ch.alloc()));pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));pipeline.addLast(new StringDecoder());pipeline.addLast(new StringEncoder());pipeline.addLast(new Handler());}
}
Handler.java
public class Handler extends SimpleChannelInboundHandler<String> {@Overridepublic void handlerAdded(ChannelHandlerContext ctx) throws Exception {Channel incoming = ctx.channel();ctx.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " 加入\n");}@Overrideprotected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {Channel incoming = ctx.channel();System.out.println("收到消息" + s)}@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {Channel incoming = ctx.channel();System.out.println(incoming.remoteAddress() + "在线");}@Overridepublic void handlerRemoved(ChannelHandlerContext ctx) throws Exception {Channel incoming = ctx.channel();ctx.writeAndFlush("[SERVER] - " + incoming.remoteAddress() + " 离开\n");}@Overridepublic void channelInactive(ChannelHandlerContext ctx) throws Exception {Channel incoming = ctx.channel();System.out.println(incoming.remoteAddress() + "掉线");}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {Channel incoming = ctx.channel();System.out.println(incoming.remoteAddress() + "异常");// 当出现异常就关闭连接cause.printStackTrace();ctx.close();}
三、客户端代码
Main.java
public class Main {private static String m_host = "127.0.0.1";private static int m_prot = 23333;public static void main(String[] args) throws Exception {new Main().run();}public void run() throws Exception {File certChainFile = new File(".\\ssl\\client.crt");File keyFile = new File(".\\ssl\\pkcs8_client.key");File rootFile = new File(".\\ssl\\ca.crt"); final SslContext sslCtx = SslContextBuilder.forClient().keyManager(certChainFile, keyFile).trustManager(rootFile).build();EventLoopGroup group = new NioEventLoopGroup();try {Bootstrap b = new Bootstrap();b.group(group).channel(NioSocketChannel.class).handler(new Initializer(sslCtx));Channel ch = b.connect(m_host, m_prot).sync().channel();BufferedReader in = new BufferedReader(new InputStreamReader(System.in));while (true) {ch.writeAndFlush(in.readLine() + "\r\n");}} finally {group.shutdownGracefully();}}
}
Initializer.java
public class Initializer extends ChannelInitializer<SocketChannel>{private final SslContext sslCtx;public Initializer(SslContext sslCtx) {this.sslCtx = sslCtx;}@Overridepublic void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast(sslCtx.newHandler(ch.alloc()));pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));pipeline.addLast(new StringDecoder());pipeline.addLast(new StringEncoder());pipeline.addLast(new Handler());}
}
Handler.java
public class Handler extends SimpleChannelInboundHandler<String>{protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {System.out.println("收到:" + s);}
}
netty ssl 服务器相关推荐
- python里面ca_Python SSL服务器提供中间CA证书
我使用Python(2.7)SSL模块编写一些服务器代码,如下所示: ssock = ssl.wrap_socket(sock, ca_certs="all-ca.crt", ke ...
- 分配服务器证书在哪,手把手教你如何申请安装ssl服务器证书
现如今,互联网已经被普及,越来越多的用户也都开始意识到网络安全的重要性.为此更多的用户开始了解ssl证书服务,希望能借助ssl服务器证书来解决网络安全问题.目前贝锐官网已经支持免费申请开通ssl证书啦 ...
- AFNetworking框架下的SSL服务器证书的自定义验证
2019独角兽企业重金招聘Python工程师标准>>> # AFNetworking框架下的SSL服务器证书的自定义验证 ## 如何使用本地证书进行SSL验证 #### 开启SSL验 ...
- Windows下基于IIS服务的SSL服务器的配置
Windows下基于IIS服务的SSL服务器的配置 实验环境 Windows Server 2008 R1(CA) Windows Server 2008 R2(web服务器) Windows 7 x ...
- Netty游戏服务器实战开发(6):Netty整合Zookeeper实现分布式服务发现与注册
1:Zookeeper基础 安装zookeeper.当然,很多时候我们会在Windows上开发,所以,我们需要在本地搭建一个zookeeper环境.方便开发过程中的测试. 首先我们去Apache上下载 ...
- VeriSign SSL服务器证书——128位支持型SSL证书(VeriSign Secure Site)_VeriSign数字证书产品...
VeriSign SSL服务器证书--128位支持型SSL证书(VeriSign Secure Site) • 全球最为知名的SSL证书品牌 • 40/56/128/256 位自适用加密 • 支持主流 ...
- VeriSign扩展验证EV SSL证书–128位支持型SSL服务器证书(VeriSign Secure Site with EV)
VeriSign扩展验证EV SSL证书–128位支持型SSL服务器证书(VeriSign Secure Site with EV) • 全球最为知名的SSL证书品牌 • 40/56/128/256 ...
- VeriSign扩展验证EV SSL证书——128位强制型SSL服务器证书(VeriSign Secure Site Pro with EV)...
VeriSign扩展验证EV SSL证书--128位强制型SSL服务器证书(VeriSign Secure Site Pro with EV) • 全球最为知名的SSL证书品牌 • SGC128位强制 ...
- 如何申请服务器证书,申请域名的步骤有哪些?如何申请安装ssl服务器证书?
申请网站域名随着科技的不断发展进入了人们的视野,申请网站域名最主要的是要选择一个正规的地方去申请.接下来就是要了解申请域名的步骤了,现在注册网站域名的网址比较多,市场上新旧域名注册商有上万家,每一家都 ...
最新文章
- 【计算机网络】网络层 : ICMP 协议 ( ICMP 差错报文 | 差错报文分类 | ICMP 询问报文 | ICMP 应用 | Ping | Traceroute )
- 使用UltraISO为U盘或内存卡制作系统安装工具
- 坡道行驶电动小车_事发红绿灯路口!东莞一女子骑电动滑板车被撞致颅内出血…...
- 4.WaitForSingleObject函数分析
- 逆向工程生成的Mapper.xml以及*Example.java详解
- 分披萨问题_比萨问题–建造者与装饰者
- linux 外壳的概念,Linux 与 Linux 发行版的一些概念
- android51版本小游戏,世界游戏大全51游戏下载-世界游戏大全51预约 安卓版v1.0.0-PC6手游网...
- 简单电脑***《菜鸟级》
- QQ群排名霸屏技术居然是这样简单
- python qq空间留言批量删除_易语言实现QQ空间留言批量删除的代码
- HTML 下拉式菜单
- ShareX的使用说明
- Java Runtime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Ecli
- Python Open3D点云配准点对点,点对面ICP(Iterative Closest Point)
- 线上软测培训机构柠檬班与iTEST.AI平台赋能测试行业共同发展、共绽异彩
- 模拟京东快递单号查询案例2020/11/24
- 建立arm linux运行环境,构建 arm-linux 仿真运行环境 (skyeye + arm-linux + NFS)
- 毕业设计说明书(论文)结构-系统设计方面
- 学编程买什么类型的电脑适合?从预算到配置,给你安排的明明白白!