创建https访问证书,增加证书到tomcat配置
1、 创建证书
首先进入JAVA_HOME的bin目录下输入如下代码:
cd /usr/java/jdk1.7.0_79/bin/
keytool -genkey -alias tomcat -keyalg RSA -keystore /usr/local/tomcat/tomcat.keystore -validity 36500
Enter keystore password: #此处需要输入大于6个字符的字符串
Re-enter new password:
What is your first and last name? #“您的名字与姓氏是什么?”这是必填项,
[Unknown]: haha
What is the name of your organizational unit? #“你的组织单位名称是什么?”可以按照需要填写也可以不填写直接回车,实验中直接回 车
[Unknown]:
What is the name of your organization? #“您的组织名称是什么?”,同上直接回车
[Unknown]:
What is the name of your City or Locality? #“您所在城市或区域名称是什么?,同上直接回车
[Unknown]:
What is the name of your State or Province? #“您所在的州或者省份名称是什么?”
[Unknown]:
What is the two-letter country code for this unit? #“该单位的两字母国家代码是什么?”
[Unknown]:
Is CN=10.15.24.254, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct? #系统询问“正确 吗?”时,对照 输入信息,如果符合要求则使用键盘输入字母“y”,否则输入“n”重新填写上面的信息
[no]: y
Enter key password for
(RETURN if same as keystore password): #输入的主密码,这项较为重要,会在tomcat配置文件中使用,建议输入与keystore的密码一 致,设置其它密码也可以
Re-enter new password:
2、修改tomcat的server.xml配置文件,使其支持https
cd /tomcat/conf/
vim server.xml
增加https的节点配置,下面这个节点在 server.xml中是存在的,但是被注释掉了,你将注释取消后,增加两个属性,一个是证书路径,一个是证书密码即可。
三 、防火墙端口配置
vim /etc/sysconfig/iptables进入端口设置文件编辑状态
这里要注意一下centos有的版本没有iptables文件,如果没有就需要自己创建一个
创建方法:cd /etc/sysconfig/
然后执行:iptables -P OUTPUT ACCEPT service iptables save service iptables restart
创建完毕后再运行: vim /etc/sysconfig/iptables
tomcat中对http访问设置的端口是8080,这里我没设置该端口所以http访问方式作废。只能访问https,tomcat中https的默认设置端口是8443
但是https自默认端口是443所以只要将tomcat中的8443修改为443就可以直接在ip前面加上https://ip直接访问。
四、搭建中遇到的坑
我这里使用的centos服务器是6.5版本,系统自带了openjdk 1.7.*,第一步中要卸载系统自带的openjdk的原因是因为openjdk下的 toolkey生成的证书,是无效证书,无法访问https,你可以使用 curl -v https://ip运行之后等一会出现错误编号,然后可以打开:http://www-archive.mozilla.org/projects/security/pki/nss/ref/ssl/sslerr.html 进行对比查询。就因为这openjdk搞得我多次重次环境,之后突然想到可能会是这个原因,网上一查真有人说 了。
yum update nss 升级nss,不管你是哪个版本的centos你最后都执行下这句代码。否则你通过
curl访问ip会遇到 NSS ERROR -59990
B.打开tomcat目录下的server.xml文件并找到关于ssl的相关段
- <!-- Define a SSL HTTP/1.1 Connector on port 8443
- This connector uses the JSSE configuration, when using APR, the
- connector should be using the OpenSSL style configuration
- described in the APR documentation -->
- <!--<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
- maxThreads="150" scheme="https" secure="true"
- clientAuth="false" sslProtocol="TLS" />-->
C.去掉注释,添keystoreFile="C:\Tomcat\GMAE3.0Tomcat\tomcat.keystore"
keystorePass="tomcat"的属性
改动完成后配置为:
- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" keystoreFile="C:\Tomcat\GMAE3.0Tomcat\tomcat.keystore" keystorePass="tomcat" sslProtocol="TLS" />
D. 若想同时去掉8443端口号,将上述配置的8443改成443:
-
<Connector port="443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" keystoreFile="C:\Tomcat\GMAE3.0Tomcat\tomcat.keystore" keystorePass="tomcat" sslProtocol="TLS" />
另外将
- <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
改成
- <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="443" URIEncoding="UTF-8"/>
转自:http://www.linuxidc.com/Linux/2016-08/134339.htm