您的位置:首页 > 运维架构 > Tomcat

linux tomcat7 配置证书(JKS和PKCS12格式)

2017-04-17 14:26 393 查看

1. 环境说明

系统:debian7

JDK版本:1.7

tomcat版本:7

2. 启动tomcat7说明

apt-get install tomcat7之后,tomcat相关文件会被分配到两个目录:一个是/var/lib/tomcat7,另外一个是/usr/share/tomcat7。

这两个目录catalina.home=/usr/share/tomcat7;catalina.base=/var/lib/tomcat7。

在/usr/share/tomcat7/bin目录下有个startup.sh脚本,如果只执行这个脚本启动tomcat,会报错,说找不到/usr/share/tomcat7/conf/server.xml,而事实上/usr/share/tomcat7/conf目录是不存在的,更别提server.xml文件了。究其原因是因为/usr/share/tomcat7/bin/catalina.sh没有设置catalina.base和catalina.home,执行startup.sh默认catalina.base和catalina.home都是/usr/share/tomcat7目录,从而无法引用到/var/lib/tomcat7/conf/server.xml。解决方法就是在catalina.sh脚本内添加下面两句:

CATALINA_HOME=/usr/share/tomcat7

CATALINA_BASE=/var/lib/tomcat7

然后startup.sh启动就没问题了。

当然也可以不用配置这两个环境变量,直接用service tomcat7 start|stop|restart,或者/etc/init.d/tomcat7 start|stop|restart

3. 生成证书

Tomcat 目前只能操作 JKS、PKCS11、PKCS12 格式的密钥存储库。JKS 是 Java 标准的“Java 密钥存储库”格式,是通过 keytool 命令行工具创建的。该工具包含在 JDK 中。PKCS12 格式一种互联网标准,可以通过 OpenSSL 和 Microsoft 的 Key-Manager 来。下面将介绍使用JKS和PKCS12格式密钥存储库给tomcat7配置证书。

3.1 生成JKS密钥存储库

[root@localhost ~]# $JAVA_HOME/bin/keytool -genkey -alias tomcat -keyalg RSA -keystore /usr/share/tomcat7/bin/.keystore -validity 36500

输入keystore密码: 

再次输入新密码: 

您的名字与姓氏是什么?

[Unknown]: aaaaaa

您的组织单位名称是什么?

[Unknown]: aaaaaa

您的组织名称是什么?

[Unknown]: aaaaaa

您所在的城市或区域名称是什么?

[Unknown]: aaaaaa

您所在的州或省份名称是什么?

[Unknown]: aaaaaa

该单位的两字母国家代码是什么

[Unknown]: CN

CN=aaaaaa, OU=aaaaaa, O=aaaaaa, L=aaaaaa, ST=aaaaaa, C=CN 正确吗?

[y/n]: y

输入<tomcat>的主密码

(如果和 keystore 密码相同,按回车):

[root@localhost ~]# cd /usr/share/tomcat7/bin/

[root@localhost bin]# keytool -selfcert -alias tomcat -keystore .keystore

输入keystore密码: 

[root@localhost bin]# keytool -export -alias tomcat -keystore .keystore -storepass aaaaaa -rfc -file tomcat.cer

保存在文件中的认证 <tomcat.cer>

3.2 生成PKCS12格式密钥存储库

创建密钥文件,自己做CA:

openssl genrsa -des3 -out openssl.pem 2048

输出内容为:
Generating RSA private key, 2048 bit long modulus
.....................................................................................................................+++
..........................+++
e is 65537 (0x010001)
Enter pass phrase for openssl.pem: ← 输入一个新密码
Verifying – Enter pass phrase for openssl.pem: ← 重新输入一遍密码
创建证书申请文件:

openssl req -new -key openssl.pem -out openssl.csr

输出内容为:
Enter pass phrase for openssl.pem: ← 输入前面创建的密码
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 []: ← 可以不输入
创建证书文件,有效期设为100年:

openssl x509 -req -days 36500 -sha256 -signkey openssl.pem -in openssl.csr -out openssl.crt

输出内容为:
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./emailAddress=admin@mycompany.com
Getting Private key
Enter pass phrase for openssl.pem: ← 输入前面创建的密码
导出.p12文件:

openssl pkcs12 -export -in openssl.crt -inkey openssl.pem -out openssl.p12 -name "openssl"

根据命令提示,输入openssl.pem密码,创建p12密码。

将.p12 文件导入到keystore JKS文件 server.keystore:

keytool -importkeystore -v -srckeystore  openssl.p12 -srcstoretype pkcs12 -srcstorepass aaaaaa -destkeystore openssl.keystore -deststoretype jks -deststorepass aaaaaa

这里srcstorepass后面的aaaaaa为openssl.p12的密码,deststorepass后的aaaaaa为keyStore的密码

4. 配置tomcat

vim /var/lib/tomcat7/conf/server.xml

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
keystoreFile="/usr/share/tomcat7/bin/.keystore"
keystorePass="aaaaaa"
clientAuth="false" sslProtocol="TLS" />


https默认端口是443,但是我发现使用443端口配置会导致这个connector无法启动。1000以内的端口是需要用root权限去启动的,但是service tomcat7 start的时候用的其实是tomcat7用户,并非root。该如何使用root账号启动tomcat暂时没有去研究。

在此处配置connector的时候只需要使用到前面生成的keystore即可。

5. 配置防火墙

iptables -I INPUT -s yourIpAddress -p tcp --dport 8443 -j ACCEPT

 iptables -I INPUT 2 -p tcp --dport 8443 -j DROP  //直接将这一条规则插到防火墙配置的最后就好了。数字2根据当前机器共有多少条规则进行修改,比如有10条规则,那么就改成10,不懂可以评论。

到此tomcat7配置就完成了,可以https://host:8443的方式访问/var/lib/tomcat7/webapps目录内的web站点了。

6. 强制http转为https

我们可以在/var/lib/tomcat7/conf/web.xml文件末尾加上以下内容:

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<!-- 强制将http请求转换为https -->
<login-config>
<auth-method>CLIENT-CERT</auth-method>
<realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<security-constraint>
<web-resource-collection >
<web-resource-name >SSL</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>
重启tomcat便可以将访问web站点的http请求全部转为https请求。

当然使用这个配置是有个前提的,那就是/var/lib/tomcat7/conf/server.xml中必须配置8080端口的redirectPort="8443"

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
URIEncoding="UTF-8"
redirectPort="8443" />
并且配置了8443端口的connector

<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
keystoreFile="/var/lib/tomcat7/key/.keystore"
keystorePass="aaaaaa"
clientAuth="false" sslProtocol="TLS" />


整个配置过程中如果出现启动问题,可以查看/var/lib/tomcat7/logs目录内的catalina.yyyy-mm-day.log,这个日志会记录启动tomcat7的异常问题。

本文在配置证书过程中参考了以下博客,在此表示感谢。

http://www.cnblogs.com/sixiweb/p/3339698.html

http://www.jianshu.com/p/045f95c008a0# 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tomcat 证书 https JKS PKCS12