Linux 系统下用OpenSSL 生成Tomcat 单向和双向认证(环境debain系统、tomcat 7)

首先安装OpenSSL 这个工具

        1、可以在http://distfiles.macports.org/openssl/ 下载最近的OpenSSL的tar包。下载完成后
传到Linux 目录下。(也可以执行apt-get wget http://www.openssl.org/source/openssl-1.0.1g.tar.gz
  2、 进入目录解压tar包   命令为:tar -zxf openssl-1.0.1g.tar.gz(tar名) 。
  3、进入解压的目录:cd openssl-1.0.2j
  4、设定Openssl 安装,( --prefix )参数为欲安装之目录,也就是安装后的档案会出现在该目录下:
       执行命令: ./config --prefix=/usr/local/openssl
  5、执行命令./config -t
  6、执行make,编译Openssl   这里如果出现如下错误 make[1]: gcc: Command not found 
   保证系统网络畅通以后,执行命令 apt-get install gcc 安装GCC(注意,一定要忘了顺畅,不然安装不了)
 7、安装 Openssl: make install
 8、执行以下命令
          [root@localhost /]# cd /usr/local
           [root@localhost local]# ldd /usr/local/openssl/bin/openssl
 9、查看路径 :which openssl
      查看版本: openssl version
     关于安装和升级可以参考  https://www.cnblogs.com/kyoung/p/6801143.html

    

其次是配置

    创建证书目录  

//进入tmp目录
cd /tmp
//创建ca目录,存放证书相关文件
mkdir ca  mkdir service(创建服务器目录) mkdir client(创建客户端目录)
//进入ca
cd ca
    制作根证书
1. 创建根证书密钥文件(自己做CA) root.key
openssl genrsa -des3 -out root.key 2048
输出内容为:
Generating RSA private key, 2048 bit long modulus
.....................................................................................................................+++
..........................+++
e is 65537 (0x010001)
Enter pass phrase for root.key: ← 输入一个新密码 
Verifying – Enter pass phrase for root.key: ← 重新输入一遍密码
2. 创建根证书的申请文件 root.csr
openssl req -new -key root.key -out root.csr
输出内容为:
Enter pass phrase for root.key: ← 输入前面创建的密码 
You are about to be asked to enter information that will be incorporated 
into your certificate request. 
What you are about to enter is what is called a Distinguished Name or a DN. 
There are quite a few fields but you can leave some blank 
For some fields there will be a default value, 
If you enter ‘.’, the field will be left blank. 
—– 
Country Name (2 letter code) [AU]:CN ← 国家代号,中国输入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音 
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 可以不输入 
Common Name (eg, YOUR name) []: ← 此时不输入 
Email Address []:admin@mycompany.com ← 电子邮箱,可随意填

Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 可以不输入 
An optional company name []: ← 可以不输入
3. 创建一个自当前日期起为期十年的根证书 root.crt
openssl x509 -req -days 3650 -sha256 -extfile /usr/local/openssl/ssl/openssl.cnf -extensions v3_ca -signkey root.key -in root.csr -out root.crt
输出内容为:
Signature ok 
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./emailAddress=admin@mycompany.com
Getting Private key 
Enter pass phrase for root.key: ← 输入前面创建的密码
4.根据CA证书生成truststore JKS文件 root.truststore
//这一步只针对双向认证,单向不需要
keytool -keystore root.truststore -keypass 123456 -storepass 123456 -alias ca -import -trustcacerts -file /tmp/ca/root.crt
键入回事后,提示是否信息此证书,输入yes, 则生成truststore成功
    
    制作service服务器端证书
    进入前面创建的服务器目录(cd service)
1.创建服务器证书密钥 server.key
openssl genrsa -des3 -out server.key 2048
输出内容为:
Generating RSA private key, 2048 bit long modulus
...........................+++
...............+++
e is 65537 (0x010001)
Enter pass phrase for server.key: ← 输入前面创建的密码
Verifying - Enter pass phrase for server.key: ← 重新输入一遍密码
运行时会提示输入密码,此密码用于加密key文件(参数des3便是指加密算法,当然也可以选用其他你认为安全的算法.),以后每当需读取此文件(通过openssl提供的命令或API)都需输入口令.如果觉得不方便,也可以去除这个口令,但一定要采取其他的保护措施! 
去除key文件口令的命令: 
openssl rsa -in server.key -out server.key
2.创建服务器证书的申请文件 server.csr
openssl req -new -key server.key -out server.csr
输出内容为:
Enter pass phrase for server.key: ← 输入前面创建的密码
You are about to be asked to enter information that will be incorporated 
into your certificate request. 
What you are about to enter is what is called a Distinguished Name or a DN. 
There are quite a few fields but you can leave some blank 
For some fields there will be a default value, 
If you enter ‘.’, the field will be left blank. 
—– 
Country Name (2 letter code) [AU]:CN ← 国家名称,中国输入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省名,拼音 
Locality Name (eg, city) []:BeiJing ← 市名,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 可以不输入 
Common Name (eg, YOUR name) []:www.xxx.com ← 服务器主机名(或者IP),若填写不正确,浏览器会报告证书无效,但并不影响使用
Email Address []:admin@mycompany.com ← 电子邮箱,可随便填

Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 可以不输入 
An optional company name []: ← 可以不输入
3.创建自当前日期起有效期为期十年的服务器证书 server.crt
openssl x509 -req -days 3650 -sha256 -extfile /usr/local/openssl/ssl/openssl.cnf -extensions v3_req -CA /tmp/ca/root.crt -CAkey /tmp/ca/root.key -CAcreateserial -in server.csr -out server.crt
输出内容为:
Signature ok 
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com 
Getting CA Private Key 
Enter pass phrase for root.key: ← 输入前面创建的密码
4.导出.p12文件 server.p12
openssl pkcs12 -export -in /tmp/service/server.crt -inkey /tmp/service/server.key -out  /tmp/service/server.p12 -name "server"
根据命令提示,输入server.key密码,创建p12密码。
5.将.p12 文件导入到keystore JKS文件 server.keystore
keytool -importkeystore -v -srckeystore  /tmp/service/server.p12 -srcstoretype pkcs12 -srcstorepass 123456 -destkeystore /tmp/service/server.keystore -deststoretype jks -deststorepass 123456
这里srcstorepass后面的123456为server.p12的密码deststorepass后的123456为keyStore的密码
    制作client客户端证书
    进入前面创建的客户端目录(cd client)
1.创建客户端证书密钥文件 client.key
openssl genrsa -des3 -out client.key 2048
输出内容为:
Generating RSA private key, 2048 bit long modulus
...............................+++
.........................+++
e is 65537 (0x010001)
Enter pass phrase for client.key: ← 输入一个新密码 
Verifying – Enter pass phrase for client.key: ← 重新输入一遍密码
2.创建客户端证书的申请文件 client.csr
openssl req -new -key client.key -out client.csr
输出内容为:
Enter pass phrase for client.key: ← 输入上一步中创建的密码 
You are about to be asked to enter information that will be incorporated 
into your certificate request. 
What you are about to enter is what is called a Distinguished Name or a DN. 
There are quite a few fields but you can leave some blank 
For some fields there will be a default value, 
If you enter ‘.’, the field will be left blank. 
—– 
Country Name (2 letter code) [AU]:CN ← 国家名称,中国输入CN 
State or Province Name (full name) [Some-State]:BeiJing ← 省名称,拼音 
Locality Name (eg, city) []:BeiJing ← 市名称,拼音 
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 
Organizational Unit Name (eg, section) []: ← 可以不填 
Common Name (eg, YOUR name) []:Lenin ← 自己的英文名,可以随便填 
Email Address []:admin@mycompany.com ← 电子邮箱,可以随便填

Please enter the following ‘extra’ attributes 
to be sent with your certificate request 
A challenge password []: ← 可以不填 
An optional company name []: ← 可以不填
3.创建一个自当前日期起有效期为十年的客户端证书 client.crt
openssl x509 -req -days 3650 -sha256 -extfile /usr/local/openssl/ssl/openssl.cnf -extensions v3_req -CA /tmp/ca/root.crt -CAkey /tmp/ca/root.key -CAcreateserial -in client.csr -out client.crt
输出内容为:
Signature ok 
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com 
Getting CA Private Key 
Enter pass phrase for root.key: ← 输入上面创建的密码
4.导出.p12文件 client.p12
openssl pkcs12 -export -in /tmp/client/client.crt -inkey /tmp/client/client.key -out  /tmp/client/client.p12 -name "client"
根据命令提示,输入client.key密码,创建p12密码。

  • 解释
名称 
crt证书只含有公钥
p12证书是包含证书(含公钥)和私钥
JKS(Java key store)存放密钥的容器。.jks .keystore .truststore等
KeyStore服务器的密钥存储库,存服务器的公钥私钥证书
TrustStore服务器的信任密钥存储库,存CA公钥

  • 单向认证需要文件
名称 
root.crt客户端使用的CA根证书
server.keystore服务端证书存放的容器
  • 双向认证需要文件
名称 
root.crt客户端使用的CA根证书
client.p12客户端证书包含私钥
root.truststoreCA公钥存放到受信赖的容器
server.keystore服务端证书存放的容器
单向认证

客户端只需要安装root.crt这个CA根证书
服务器端配置server.keystore

配置Tomcat
1.关闭tomcat
tomcatbin目录下执行
shutdown.sh
2.将keystore文件(server.keystore) 放在web服务器上
cp /tmp/service/server.keystore /你的tomcat根目录/conf
3.修改server.xml配置文件
cd /你的tomcat根目录/conf
vi server.xml
找到下面被注释的代码,删除注释符并修改内容(vi命令操作)
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               keystoreFile="/你的tomcat根目录/conf/server.keystore"
               keystorePass="123456"
               clientAuth="false"
                />
4.启动tomcat
tomcatbin目录下执行
startup.sh
5.访问https服务
https://localhost:8443/
https://192.168.1.1:8443/  你的IP
双向认证

客户端需要安装root.crt这个CA根证书,client.p12这个客户端证书(如果是本机访问需要导入到浏览器上)
服务器端配置server.keystore,root.truststore

配置Tomcat
1.关闭tomcat
tomcatbin目录下执行
shutdown.sh
2.将keystore文件(server.keystore) 放在web服务器上
cp /tmp/service/server.keystore /你的tomcat根目录/conf
  将truststore文件(root.truststore) 放在web服务器上
cp /tmp/ca/root.truststore /你的tomcat根目录/conf
3.修改server.xml配置文件
cd /你的tomcat根目录/conf
vi server.xml
找到下面被注释的代码,删除注释符并修改内容(vi命令操作)
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               keystoreFile="/你的tomcat根目录/conf/server.keystore"
               keystorePass="123456"
               truststoreFile="/你的tomcat根目录/conf/root.truststore"
               truststorePass="123456"
               clientAuth="true" 
                />
注意!clientAuth为true。这里和单向的不同。
4.启动tomcat
tomcatbin目录下执行
startup.sh
5.访问https服务
https://localhost:8443/
https://192.168.1.1:8443/  你的IP

配置可以参考 https://www.jianshu.com/p/045f95c008a0



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值