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

建什么网站能百度收录中国工业机械加工网

建什么网站能百度收录,中国工业机械加工网,合肥软件开发网站建设,网站备案后更换主机“不积跬步#xff0c;无以至千里。” 说明 其实Netty使用SslHandler实现加密通信单向认证和双向认证在代码上区别不大#xff0c;下面是双向认证的代码示例 引入依赖 dependencygroupIdio.netty/groupIdartifactIdnetty-all/artifac… “不积跬步无以至千里。” 说明 其实Netty使用SslHandler实现加密通信单向认证和双向认证在代码上区别不大下面是双向认证的代码示例 引入依赖 dependencygroupIdio.netty/groupIdartifactIdnetty-all/artifactIdversion4.1.100.Final/version /dependency生成keystore.jks文件 keytool -genkeypair -alias your_alias -keyalg RSA -keystore keystore.jks -keysize 2048Server端 import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.ssl.SslHandler; import io.netty.util.CharsetUtil;import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; import java.io.FileInputStream; import java.nio.charset.StandardCharsets; import java.security.KeyStore;public class NettySslServer {private static final int PORT 8888;public static void main(String[] args) throws Exception {// 加载SSL证书String serverKeyStorePath /home/admin/server/keystore.jks;String clientKeyStorePath /home/admin/client/keystore.jks;String serverKeyStorePassword happya;String clientKeyStorePassword happya;// 创建SSL上下文SSLContext sslContext SSLContext.getInstance(TLS);sslContext.getServerSessionContext().setSessionCacheSize(1);sslContext.getServerSessionContext().setSessionTimeout(60);// 配置密钥库KeyStore keyStore KeyStore.getInstance(JKS);keyStore.load(new FileInputStream(serverKeyStorePath), serverKeyStorePassword.toCharArray());KeyManagerFactory keyManagerFactory KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());keyManagerFactory.init(keyStore, serverKeyStorePassword.toCharArray());// 配置信任库KeyStore trustStore KeyStore.getInstance(JKS);trustStore.load(new FileInputStream(clientKeyStorePath), clientKeyStorePassword.toCharArray());TrustManagerFactory trustManagerFactory TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());trustManagerFactory.init(trustStore);// 初始化SSLContextsslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);// 创建EventLoopGroupEventLoopGroup bossGroup new NioEventLoopGroup();EventLoopGroup workerGroup new NioEventLoopGroup();try {// 创建服务器BootstrapServerBootstrap serverBootstrap new ServerBootstrap();serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializerSocketChannel() {Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline ch.pipeline();// 在ChannelPipeline中添加SSL处理器SSLEngine sslEngine sslContext.createSSLEngine();sslEngine.setUseClientMode(false);pipeline.addLast(new SslHandler(sslEngine));// 添加加密通信处理器pipeline.addLast(new SecureChatServerHandler());}}).childOption(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);// 启动服务器System.out.println(Begin to start ssl server);ChannelFuture future serverBootstrap.bind(PORT).sync();System.out.println(Ssl server started);future.channel().closeFuture().sync();} finally {workerGroup.shutdownGracefully();bossGroup.shutdownGracefully();}}public static class SecureChatServerHandler extends ChannelInboundHandlerAdapter {Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {// 当连接建立时发送欢迎消息System.out.println(Server channel active : ctx.channel().toString());ctx.channel().writeAndFlush(Unpooled.wrappedBuffer(Welcome to the secure chat server!\n.getBytes(StandardCharsets.UTF_8)));ctx.channel().writeAndFlush(Unpooled.wrappedBuffer(Your connection is protected by SSL.\n.getBytes(StandardCharsets.UTF_8)));}Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf byteBuf (ByteBuf) msg;System.out.println(Server received message: byteBuf.toString(CharsetUtil.UTF_8));super.channelRead(ctx, msg);}Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {// 处理异常cause.printStackTrace();ctx.close();}} }Client端 import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; 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.SslHandler; import io.netty.util.CharsetUtil;import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; import javax.net.ssl.TrustManagerFactory; import java.io.FileInputStream; import java.nio.charset.StandardCharsets; import java.security.KeyStore;public class NettySslClient {private static final String HOST localhost;private static final int PORT 8888;public static void main(String[] args) throws Exception {// 加载SSL证书String serverKeyStorePath /home/admin/server/keystore.jks;String clientKeyStorePath /home/admin/client/keystore.jks;String serverKeyStorePassword happya;String clientKeyStorePassword happya;// 创建SSL上下文SSLContext sslContext SSLContext.getInstance(TLS);sslContext.getServerSessionContext().setSessionCacheSize(1);// session cache timeout (sessionTimeout) s default value is 86400s (24hr), and reduced to 60s to reduce IO block issue.sslContext.getServerSessionContext().setSessionTimeout(60);// 配置密钥库KeyStore keyStore KeyStore.getInstance(JKS);keyStore.load(new FileInputStream(clientKeyStorePath), clientKeyStorePassword.toCharArray());KeyManagerFactory keyManagerFactory KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());keyManagerFactory.init(keyStore, clientKeyStorePassword.toCharArray());// 配置信任库KeyStore trustStore KeyStore.getInstance(JKS);trustStore.load(new FileInputStream(serverKeyStorePath), serverKeyStorePassword.toCharArray());TrustManagerFactory trustManagerFactory TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());trustManagerFactory.init(trustStore);sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);// 创建EventLoopGroupEventLoopGroup group new NioEventLoopGroup();try {// 创建客户端BootstrapBootstrap bootstrap new Bootstrap();bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializerSocketChannel() {Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline ch.pipeline();// 在ChannelPipeline中添加SSL处理器SSLEngine sslEngine sslContext.createSSLEngine();sslEngine.setUseClientMode(true);pipeline.addLast(new SslHandler(sslEngine));// 添加加密通信处理器pipeline.addLast(new SecureChatClientHandler());}});// 连接服务器System.out.println(Begin to start ssl client);ChannelFuture future bootstrap.connect(HOST, PORT).sync();System.out.println(Ssl client started);future.channel().closeFuture().sync();} finally {group.shutdownGracefully();}}public static class SecureChatClientHandler extends ChannelInboundHandlerAdapter {Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {// 连接建立时发送一条消息给服务器System.out.println(Client channel active : ctx.channel().toString());ctx.channel().writeAndFlush(Unpooled.wrappedBuffer(Hello from client!\n.getBytes(StandardCharsets.UTF_8)));}Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {ByteBuf byteBuf (ByteBuf) msg;System.out.println(Client received message: \n byteBuf.toString(CharsetUtil.UTF_8));super.channelRead(ctx, msg);}Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {// 处理异常cause.printStackTrace();ctx.close();}} }
http://www.zqtcl.cn/news/23393/

相关文章:

  • php网站制作过程中遇到的问题及解决办法建网站的成本计算
  • 网站开发的关键绥化市住房和城乡建设网网站
  • 宁波网站的优化标识设计图片
  • 网站建设微信公众号做网站和app哪个难
  • 高密做网站的公司哈尔滨建设部网站
  • 网站建设问题整改报告无锡君通科技服务有限公司
  • 做庭院景观的那个网站推广好phpcms wap网站搭建
  • 如何创办一个赚钱的网站怎么下载网页上的视频
  • 国外优秀人像摄影网站wordpress 福利 源码
  • 做二手房又做网站的手机app快速开发平台
  • 网站开发维护求职信苏州优化外包
  • 江苏网站设计网页设计素材整理分级是什么意思
  • 安徽茶叶网站建设学校网站要求
  • .net网站程序网页设计与制作作业成品免费
  • 做网站l价格中国老区建设促进会网站
  • 网站备案需要拍照备案网站域名被抢注
  • 旅行社建设网站wordpress图片主题
  • 私自建设网站绿色食品网站模板.htm
  • 乐陵市人力资源中心网站西安招聘网最新招聘
  • 怀柔重庆网站建设规模以上工业企业分析
  • h5建站是什么怎么自己做网站服务器
  • 中国建设银行掌上银行官方网站网上可以报警备案吗
  • 嘉兴seo排名扣费做好的网站怎么优化
  • 手机网站建设团队网站建设详细流程视频
  • 网页游戏网站火谷歌推广开户多少费用
  • 帝国cms调用网站名称WordPress蜘蛛爬行插件
  • html编辑工具有哪些便宜的seo官网优化
  • 杭州门户网站建设专业的高端网站制作公司
  • 网站的空间定制app开发
  • 郑州住房和城乡建设部网站百度海南分公司