<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont

本文涉及的产品
转发路由器TR,750小时连接 100GB跨地域
简介: 生成ssl证书请参考如下:http://blog.csdn.net/u014410763/article/details/50555902参考文章:https://yq.

生成ssl证书请参考如下:

http://blog.csdn.net/u014410763/article/details/50555902

参考文章:

https://yq.aliyun.com/articles/40408

客户端私钥与证书导出(java需要特定格式)

openssl pkcs12 - export  -clcerts -name foobar  -inkey  client.key  - in   client .crt -out  client .keystore

服务器端私钥与证书导出

openssl pkcs12 - export  -clcerts -name foobar  -inkey  server.key  - in   server .crt -out  server .keystore
受信任CA证书导出

keytool -importcert -trustcacerts - alias foobar  - file  ca .crt  -keystore  ca-trust .keystore

注意代码中的密码,可能你设置的各个证书的不同

服务器端代码

package online.geekgalaxy.test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.security.KeyStore;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.TrustManagerFactory;


public class sslServer {

    private SSLServerSocket sslServerSocket;

    public static void main(String[] args) throws Exception {

        sslServer server = new sslServer();

        server.init();

        System.out.println("SSLServer initialized.");

        server.process();

    }



    //服务器端将要使用到server.keystore和ca-trust.keystore

    private void init() throws Exception {

        int port = 1234;

        String keystorePath = "certs/server.keystore";

        String trustKeystorePath = "certs/ca-trust.keystore";

        String keystorePassword = "";
        String caPassword = "111111";

        SSLContext context = SSLContext.getInstance("SSL");



        //客户端证书库

        KeyStore keystore = KeyStore.getInstance("pkcs12");

        FileInputStream keystoreFis = new FileInputStream(keystorePath);

        keystore.load(keystoreFis, keystorePassword.toCharArray());

        //信任证书库

        KeyStore trustKeystore = KeyStore.getInstance("jks");

        FileInputStream trustKeystoreFis = new FileInputStream(trustKeystorePath);

        trustKeystore.load(trustKeystoreFis, caPassword.toCharArray());



        //密钥库

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunx509");

        kmf.init(keystore, keystorePassword.toCharArray());


        //信任库

        TrustManagerFactory tmf = TrustManagerFactory.getInstance("sunx509");

        tmf.init(trustKeystore);



        //初始化SSL上下文

        context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

        //初始化SSLSocket

        sslServerSocket = (SSLServerSocket)context.getServerSocketFactory().createServerSocket(port);

        //设置这个SSLServerSocket需要授权的客户端访问

        sslServerSocket.setNeedClientAuth(true);

    }



    private void process() throws Exception {

        String bye = "Bye!";

        byte[] buffer = new byte[50];

        while(true) {

            Socket socket = sslServerSocket.accept();

            InputStream in = socket.getInputStream();

            in.read(buffer);

            System.out.println("Received: " + new String(buffer));

            OutputStream out = socket.getOutputStream();

            out.write(bye.getBytes());

            out.flush();

        }

    }
}

客户端代码

package online.geekgalaxy.test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyStore;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManagerFactory;


public class sslClient {

    private SSLSocket sslSocket;

    public static void main(String[] args) throws Exception {

        sslClient client = new sslClient();

        client.init();

        System.out.println("SSLClient initialized.");

        client.process();

    }



    //客户端将要使用到client.keystore和ca-trust.keystore

    private void init() throws Exception {

        String host = "127.0.0.1";

        int port = 1234;

        String keystorePath = "certs/client.keystore";

        String trustKeystorePath = "certs/ca-trust.keystore";

        String keystorePassword = "";
        String caPassword = "111111";

        SSLContext context = SSLContext.getInstance("SSL");

        //客户端证书库

        KeyStore clientKeystore = KeyStore.getInstance("pkcs12");

        FileInputStream keystoreFis = new FileInputStream(keystorePath);

        clientKeystore.load(keystoreFis, keystorePassword.toCharArray());

        //信任证书库

        KeyStore trustKeystore = KeyStore.getInstance("jks");

        FileInputStream trustKeystoreFis = new FileInputStream(trustKeystorePath);

        trustKeystore.load(trustKeystoreFis, caPassword.toCharArray());



        //密钥库

        KeyManagerFactory kmf = KeyManagerFactory.getInstance("sunx509");

        kmf.init(clientKeystore, keystorePassword.toCharArray());


        //信任库

        TrustManagerFactory tmf = TrustManagerFactory.getInstance("sunx509");

        tmf.init(trustKeystore);



        //初始化SSL上下文

        context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);



        sslSocket = (SSLSocket)context.getSocketFactory().createSocket(host, port);

    }



    private void process() throws Exception {

        //往SSLSocket中写入数据

        String hello = "hello boy!";

        OutputStream out = sslSocket.getOutputStream();

        out.write(hello.getBytes(), 0, hello.getBytes().length);

        out.flush();



        //从SSLSocket中读取数据

        InputStream in = sslSocket.getInputStream();

        byte[] buffer = new byte[50];

        in.read(buffer);

        System.out.println(new String(buffer));

    }
}


目录
相关文章
|
Web App开发 前端开发
|
Web App开发 Apache
|
Web App开发 前端开发 Java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
 Connection reset by peer的常见原因: 1)服务器的并发连接数超过了其承载量,服务器会将其中一些连接关闭;    如果知道实际连接服务器的并发客户数没有超过服务器的承载量,看下有没有网络流量异常。
920 0
|
Web App开发 前端开发 Java
|
Web App开发 前端开发 数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
数据仓库建设步骤Posted on 2015-03-04 10:18 xuzhengzhu 阅读(1164) 评论(0) 编辑 收藏 1.系统分析,确定主题 确定一下几个因素:    ·操作出现的频率,即业务部门每隔多长时间做一次查询分析。
945 0
|
Web App开发 前端开发 测试技术
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
ClusterId read in ZooKeeper is null. Re-running the program after fixing issue 1 will result in the following e...
874 0
|
Web App开发 前端开发 API
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
     比如RDD里的计算调用了别的组件类里的方法(比如hbase里的put方法),那么序列化时,会将该方法所属的对象的所有变量都序列化的,可能有些根本没有实现序列化导致直接报错。
774 0
|
Web App开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
1、使用nvidia-smi pmon 查看linux系统的gpu情况,如下: 显然是2张显卡,如何让它们都工作呢 2、keras提供了keras.
1009 0
|
Web App开发 前端开发
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
解决方式如下: 1)删除虚拟机配置文件下面的.lck文件 2)删除类型为快照的.vmsn/.vmsd文件 然后重启虚拟机即可
858 0
|
Web App开发 前端开发 Java
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><meta http-equiv="Cont
一个测试环境hadoop集群由于磁盘满导致宕机,启动后发现journalnode报如下异常: 2018-03-19 20:48:04,817 WARN  namenode.
752 0
AI助理
登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问

你好,我是AI助理

可以解答问题、推荐解决方案等