Mina的ssl加密
前面写的Mina的示例,都是可以通过telnet登录的,并且可以相互交互。
现在采用ssl加密的方式,对建立连接时,进行加密连接。这样,双方只有统一的加密,然后才可以连接。
密钥的生成之前有说过,里面有引用站内某个程序员的博文,这里用的代码也是出自他手。
package com.example.mina.ssl; import java.io.File;
import java.security.KeyStore; import javax.net.ssl.SSLContext; import org.apache.mina.filter.ssl.KeyStoreFactory;
import org.apache.mina.filter.ssl.SslContextFactory; public class SSLContextGenerator { /**
* 这个方法,通过keystore和truststore文件返回一个SSLContext对象
*
* @return
*/
public SSLContext getSslContext() {
SSLContext sslContext = null;
try {
/*
* 提供keystore的存放目录,读取keystore的文件内容
*/
File keyStoreFile = new File("G:/ssl/keystore.jks"); /*
* 提供truststore的存放目录,读取truststore的文件内容
*/
File trustStoreFile = new File(
"G:/ssl/truststore.jks"); if (keyStoreFile.exists() && trustStoreFile.exists()) {
final KeyStoreFactory keyStoreFactory = new KeyStoreFactory();
System.out.println("Url is: " + keyStoreFile.getAbsolutePath());
keyStoreFactory.setDataFile(keyStoreFile); /*
* 这个是当初我们使用keytool创建keystore和truststore文件的密码,也是上次让你们一定要记住密码的原因了
*/
keyStoreFactory.setPassword("123456"); final KeyStoreFactory trustStoreFactory = new KeyStoreFactory();
trustStoreFactory.setDataFile(trustStoreFile);
trustStoreFactory.setPassword("123456"); final SslContextFactory sslContextFactory = new SslContextFactory();
final KeyStore keyStore = keyStoreFactory.newInstance();
sslContextFactory.setKeyManagerFactoryKeyStore(keyStore); final KeyStore trustStore = trustStoreFactory.newInstance();
sslContextFactory.setTrustManagerFactoryKeyStore(trustStore);
sslContextFactory
.setKeyManagerFactoryKeyStorePassword("123456");
sslContext = sslContextFactory.newInstance();
System.out.println("SSL provider is: "
+ sslContext.getProvider());
} else {
System.out
.println("Keystore or Truststore file does not exist");
}
} catch (Exception ex) {
ex.printStackTrace();
}
return sslContext;
}
}
然后在客户端和服务端都注册一个占据在第一个位置的ssl过滤器,这样ssl就生效了。
/*
* 获取过滤器链,用于添加过滤器
*/
DefaultIoFilterChainBuilder chain = connector.getFilterChain(); /*
* 2.为连接添加过滤器,SSL、日志、编码过滤器
*/
// SSLContextGenerator是我们自己写的一个SSL上下文产生器,稍后会讲到
SslFilter sslFilter = new SslFilter(
new SSLContextGenerator().getSslContext());
// 设置为客户端模式
sslFilter.setUseClientMode(true);
// a.ssl过滤器,这个一定要第一个添加,否则数据不会进行加密
chain.addFirst("sslFilter", sslFilter); // b.添加日志过滤器
chain.addLast("logger", new LoggingFilter()); // c.添加字符的编码过滤器
chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8")))); /*
Mina的ssl加密的更多相关文章
- MySQL的SSL加密连接与性能开销
本文转载自:http://www.innomysql.net/article/23959.html(只作转载, 不代表本站和博主同意文中观点或证实文中信息) Contents [hide] 1 前言 ...
- Self Host WebApi服务传输层SSL加密(服务器端+客户端调用)
接上篇<WebApi服务URI加密及验证的两种方式>,在实际开发中,仅对URI进行加密是不够的,在传输层采用SSL加密也是必须的. 如果服务寄宿于IIS,那对传输层加密非常简单仅需要配置一 ...
- MySQL(MariaDB)的 SSL 加密复制
背景: 在默认的主从复制过程或远程连接到MySQL/MariaDB所有的链接通信中的数据都是明文的,在局域网内连接倒问题不大:要是在外网里访问数据或则复制,则安全隐患会被放大很多.由于项目要求需要直接 ...
- 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接
由于项目中必须得用JDK6来作为Java环境,于是连接SQLServer时出现了com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法通过使用安 ...
- nginx配置ssl加密(单双向认证、部分https)
nginx配置ssl加密(单双向认证.部分https) nginx下配置ssl本来是很简单的,无论是去认证中心买SSL安全证书还是自签署证书,但最近公司OA的一个需求,得以有个机会实际折腾一番.一开始 ...
- Chapter 1 Securing Your Server and Network(5):使用SSL加密会话
原文:Chapter 1 Securing Your Server and Network(5):使用SSL加密会话 原文出处:http://blog.csdn.net/dba_huangzj/art ...
- 如何让服务端同时支持WebSocket和SSL加密的WebSocket(即同时支持ws和wss)?
自从HTML5出来以后,使用WebSocket通信就变得火热起来,基于WebSocket开发的手机APP和手机游戏也越来越多.我的一些开发APP的朋友,开始使用WebSocket通信,后来觉得通信不够 ...
- 项目实战1—LNMP的搭建、nginx的ssl加密、身份验证的实现
总项目流程图,详见 http://www.cnblogs.com/along21/p/8000812.html 实战一:搭建lnmp及类小米等商业网站的实现 环境:关闭防火墙,selinux 1.安装 ...
- [转帖]nginx配置ssl加密(单/双向认证、部分https)
nginx配置ssl加密(单/双向认证.部分https) https://segmentfault.com/a/1190000002866627 nginx下配置ssl本来是很简单的,无论是去认证 ...
随机推荐
- Linux Tomcat重新启动
在Linux系统下,重启Tomcat使用命令操作的! 首先,进入Tomcat下的bin目录 cd /usr/local/tomcat/bin 使用Tomcat关闭命令 ./shutdown.sh 查看 ...
- mac安装protobuf2.4.1时报错./include/gtest/internal/gtest-port.h:428:10: fatal error: 'tr1/tuple' file not found和google/protobuf/message.cc:175:16: error: implicit instantiation of undefined template
通过网上下载的protobuf2.4.1的压缩文件,然后进行安装,./configure和make时遇到了两个问题. 正常的安装步骤如下: ./configure make make check m ...
- 10-能不能组成m
/* 找数达人 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 小明最近做出了一道题:如何在一组数 ...
- 安装phpcms时出现Warning: ob_start(): output handler \'ob_gzhandler\' conflicts with \'zlib
1. 解决方法一: 打开phpcms/base.php,在第57行,修改如下: if(pc_base::load_config('system','gzip') && function ...
- [Groovy] 学习Groovy的好网站(内容全面)
https://www.tutorialspoint.com/groovy/index.htm
- 获取GUID的GET网址:createguid.com
1.在浏览器的地址栏中输入createguid.com,回车之后即可得到一个GUID 2.在JMeter中可以这样填写HTTP Request 然后通过正则表达式提取器提取GUID <texta ...
- ubuntu and centos各种上网代理设置
1.Ubuntu 一. apt-get 代理设置 由于公司通过代理上网,firefox的代理设置很容易就搞定了,但是通过apt-get安装软件还是不行,于是,查阅了很多资料,最多的方法就是网上流传的三 ...
- c++日志练习
使用ostream流创建写入log日志文件 使用宏 配置文件大小和间隔时间 当创建文件时间间隔或文件大小大于指定数字 则创建新文件 文件名由时间自动命名 /********************* ...
- 向一个文件流写入一个数据块---fwrite
函数原型:int fwrite(const void *buffer,size_t size,size_t count,FILE *stream); 参数说明:buffer:用于写入到文件的数据地址. ...
- 【Java】JavaWeb文件上传和下载
文件上传和下载在web应用中非常普遍,要在jsp环境中实现文件上传功能是非常容易的,因为网上有许多用java开发的文件上传组件,本文以commons-fileupload组件为例,为jsp应用添加文件 ...