前面写的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加密的更多相关文章

  1. MySQL的SSL加密连接与性能开销

    本文转载自:http://www.innomysql.net/article/23959.html(只作转载, 不代表本站和博主同意文中观点或证实文中信息) Contents [hide] 1 前言 ...

  2. Self Host WebApi服务传输层SSL加密(服务器端+客户端调用)

    接上篇<WebApi服务URI加密及验证的两种方式>,在实际开发中,仅对URI进行加密是不够的,在传输层采用SSL加密也是必须的. 如果服务寄宿于IIS,那对传输层加密非常简单仅需要配置一 ...

  3. MySQL(MariaDB)的 SSL 加密复制

    背景: 在默认的主从复制过程或远程连接到MySQL/MariaDB所有的链接通信中的数据都是明文的,在局域网内连接倒问题不大:要是在外网里访问数据或则复制,则安全隐患会被放大很多.由于项目要求需要直接 ...

  4. 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接

    由于项目中必须得用JDK6来作为Java环境,于是连接SQLServer时出现了com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法通过使用安 ...

  5. nginx配置ssl加密(单双向认证、部分https)

    nginx配置ssl加密(单双向认证.部分https) nginx下配置ssl本来是很简单的,无论是去认证中心买SSL安全证书还是自签署证书,但最近公司OA的一个需求,得以有个机会实际折腾一番.一开始 ...

  6. 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 ...

  7. 如何让服务端同时支持WebSocket和SSL加密的WebSocket(即同时支持ws和wss)?

    自从HTML5出来以后,使用WebSocket通信就变得火热起来,基于WebSocket开发的手机APP和手机游戏也越来越多.我的一些开发APP的朋友,开始使用WebSocket通信,后来觉得通信不够 ...

  8. 项目实战1—LNMP的搭建、nginx的ssl加密、身份验证的实现

    总项目流程图,详见 http://www.cnblogs.com/along21/p/8000812.html 实战一:搭建lnmp及类小米等商业网站的实现 环境:关闭防火墙,selinux 1.安装 ...

  9. [转帖]nginx配置ssl加密(单/双向认证、部分https)

    nginx配置ssl加密(单/双向认证.部分https) https://segmentfault.com/a/1190000002866627   nginx下配置ssl本来是很简单的,无论是去认证 ...

随机推荐

  1. django MongoDB上传文件

    django上传文件,查询到的资料都是用的django自己的models.Model类,去定义一个FileField类型的存储文件,并且在里面加一句upload_to,如下所示:   但是如果用mon ...

  2. cf451C-Predict Outcome of the Game

    http://codeforces.com/problemset/problem/451/C A - Predict Outcome of the Game Time Limit:2000MS     ...

  3. day4:vcp考试

    Q61. Which two statements are true regarding Virtual SAN Fault Domains? (Choose two.)A. They enable ...

  4. 230. Kth Smallest Element in a BST 找到bst中的第k小的元素

    [抄题]: Given a binary search tree, write a function kthSmallest to find the kth smallest element in i ...

  5. [leetcode]277. Find the Celebrity谁是名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  6. Ubuntu-18.04Python2与Python3自由切换

    一.配置ssh链接 安装openssh-server devops@devops-virtual-machine:~$ sudo apt-get install openssh-server 二.安装 ...

  7. VC++ 定时器使用总结

    VC++    WM_TIMER   定时器使用方法       在编程时,会经常使用到定时器.使用定时器的方法比较简单,通常告诉Windows一个时间间隔,然后WINDOWS以此时间间隔周期性触发程 ...

  8. Linux的crontab应注意事项

    今天遇到一个问题,困扰了好久,刚开始时以为crontab定时任务配置错误,后经过验证没有错误,然后又怀疑到是不是权限问题呀?将权限跟改为root后,重新配置crontab定时任务,还是不行,真是让人气 ...

  9. 连接redis

  10. Laravel 5 如何对部份 URI 禁用 CSRF 验证

    打开中间件 VerifyCsrfToken.php 在其 $except 属性中添加要禁用的 uri,如: api/user/add api/user/* api/*