您的位置:首页 > 理论基础 > 计算机网络

linux tomcat 配置https

2016-07-07 11:04 411 查看

一.认识https

 网上已经有很多介绍https的原理,这里我只概述一下。

相对于http的明文传输,https是基于ssl的加密传输协议。

具体参见:http://jingyan.baidu.com/article/a948d6515d3e850a2dcd2ee6.html

二.获取证书

获取证书的途径大概有两种,其一为使用jdk自带的keytool工具制作,其二为在网上购买或者申请免费的证书。

网上申请免费证书主要之对于个人使用,需要的自行百度。

以下介绍使用jdk自带的keytool工具生成证书:

1.进入jdk的bin目录下 cd /yourpath/jdkxx/bin

2.输入命令keytool -genkey -v -alias tomcat -keyalg RSA -keystore /usr/local/tomcat8888/conf/tomcat.keystore -validity 365(其中-alias tomcat 指定昵称,-keyalg RSA指定加密类型,-keystore /..指定证书存储路径及名称,-validity 365指定证书过期时间,天为单位。更多keytool参数请使用命令 keytool -help查看)

之后提示Enter keystore password: (设置证书密码,请输入至少6位字符)

Re-enter new password:(新密码确认)

What is your first and last name?
  [Unknown]:(必填,且为tomcat主键的访问域名或者ip)

What is the name of your organizational unit?
  [Unknown]:(组织单位名称,随便填。。)

What is the name of your organization?
  [Unknown]:(组织名称,自行决定)

......以下按提示输入即可,确认信息时检查无误输入y确认后输入初始设定密码即可

以上证书生成完毕,可到之地路径查看证书。

三.tomcat配置https

进入tomcat的conf路径,打开server.xml文件 vim server.xml

找到

<!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
-->

注释内容,复制或者打开注释修改为

<Connector port="8443" protocol="HTTP/1.1"
     SSLEnabled="true" maxThreads="150" scheme="https"
     secure="true" clientAuth="false" sslProtocol="TLS"
     keystoreFile="/usr/local/tomcat8888/conf/tomcat.keystore" keystorePass="yourpassword" />

此时浏览器访问https://yourdomainname:8443端口时会出现证书不安全提示,如果访问http://yourdomainname:8080则不提示,依然为http访问,如果要实现任意端口访问均自动为https,则增加一下操作:

vim web.xml

在</welcome-file-list>后加入配置

<login-config>
    <!-- Authorization setting for SSL -->
    <auth-method>CLIENT-CERT</auth-method>
     <realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<security-constraint>
            <!-- Authorization setting for SSL -->
    <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>

实现强制https访问,则访问http://yourdomainname:port时也会自动跳转到https.

over!此处主要作为个人笔记,如能给他人带来益处则甚好。



阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: