zoukankan      html  css  js  c++  java
  • Tomcat 启用 HTTPS

    Tomcat 启用 HTTPS

      最近在阿里云购买了一个云服务器ECS,简单的部署了个人网站,使用ip地址访问一切正常,我之前在TK上申请了一个免费域名,就绑定了二级域名到阿里云,第一天正常访问,第二天就不行了,访问站点直接提示域名未注册(未备案.....),无法访问,显然被阿里云拦截了,通过IP地址访问还是正常的。

      后来GOOGLE了一下,一般是备案域名和主机。。。,还有一种方式是启用HTTPS,http是明文传输,阿里云能够看到我访问的域名,发现在域名未注册然后拦截,但HTTPS是加密的,所以可以正常访问(哈哈)。

    引用链接:

    https://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html

    http://www.oschina.net/question/12_23148

    http://www.cnblogs.com/f1194361820/p/4748590.html

    基本步骤:

    1. 使用 java 创建一个 keystore 文件
    2. 配置 Tomcat 以使用该 keystore 文件
    3. 测试
    4. 配置应用以便使用 SSL ,例如 https://localhost:8443/yourApp

    执行 keytool -genkey -alias tomcat -keyalg RSA 结果如下

    loiane:bin loiane$ keytool -genkey -alias tomcat -keyalg RSA
    Enter keystore password:  password
    Re-enter new password: password
    What is your first and last name?
      [Unknown]:  Loiane Groner
    What is the name of your organizational unit?
      [Unknown]:  home
    What is the name of your organization?
      [Unknown]:  home
    What is the name of your City or Locality?
      [Unknown]:  Sao Paulo
    What is the name of your State or Province?
      [Unknown]:  SP
    What is the two-letter country code for this unit?
      [Unknown]:  BR
    Is CN=Loiane Groner, OU=home, O=home, L=Sao Paulo, ST=SP, C=BR correct?
      [no]:  yes
      
    Enter key password for
        (RETURN if same as keystore password):  password
    Re-enter new password: password

    这样就在用户的主目录下创建了一个 .keystore 文件

    配置 Tomcat 以使用 keystore 文件

    打开 server.xml 找到下面被注释的这段

    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
        maxThreads="150" scheme="https" secure="true"
        clientAuth="false" sslProtocol="TLS" />
    -->

    取消注释,并将内容改为

    Connector SSLEnabled="true" acceptCount="100" clientAuth="false"
        disableUploadTimeout="true" enableLookups="false" maxThreads="25"
        port="8443" keystoreFile="${user.home}/.keystore" keystorePass="password"
        protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https"
        secure="true" sslProtocol="TLS" />

    测试1:只启用HTTPS,不启用HTTP

    在server.xml中,为8080端口的Connector加上注释,并且去掉8443端口的Connector的注释。然后启动tomcat。

    分别通过httphttps访问docs:

    http://localhost:8080/docshttps://localhost:8443/docs

    结果:只有https可以访问。

    测试2:HTTP、HTTPS同时启用

    在server.xml中,去掉8080、8443端口的Connector的注释然后启动tomcat。

    分别用httphttps访问docs:

    http://localhost:8080/docshttps://localhost:8443/docs

    结果:两者都可正常访问。

    配置应用使用 SSL

    打开应用的 web.xml 文件,增加配置如下:

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>securedapp</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>

    将 URL 映射设为 /* ,这样你的整个应用都要求是 HTTPS 访问,而 transport-guarantee 标签设置为 CONFIDENTIAL 以便使应用支持 SSL。

    如果你希望关闭 SSL ,只需要将 CONFIDENTIAL 改为 NONE 即可

    SSL证书服务

    文章到这里基本上就应该结束了,可是,当我通过https访问网站时问题提示“There is a problem with this website’s security certificate.”,自己还好说无所谓,但是别人访问的时候也提示,可能很大程度上,别人可能就不会再访问了。。。。,

    所以需要SSL证书服务,网上也有很多免费的(https://www.zhihu.com/question/19578422

    我选择的是https://buy.wosign.com/free/

    Create a local Certificate Signing Request (CSR)

    使用你之前创建的.keystore

    keytool -certreq -keyalg RSA -alias tomcat -file certreq.csr -keystore <your_keystore_filename>

    一般顺利的话最终你可以拿到一些*.crt文件,

    接下来就是把这些文件导入到.keystore文件里。

    比如我拿到的是四个压缩包

    for Apache.zip
    for IIS.zip
    for Nginx.zip
    for Other Server.zip

    然后我使用的是for Other Server,里面有四个.crt

    1_cross_Intermediate.crt
    2_issuer_Intermediate.crt
    3_user_contacts09.ml.crt
    root.crt

    先执行root,1_cross_Intermediate.crt,2_issuer_Intermediate.crt

    keytool -import -alias <root> -keystore <your_keystore_filename>
        -trustcacerts -file <filename_of_the_chain_certificate>

    最后执行 3_user_contacts09.ml.crt

    keytool -import -alias tomcat -keystore <your_keystore_filename>
        -file <your_certificate_filename>

    最后得到的.keystore直接替换之前的就可以了,现在再次访问就不会提示你证书问题了,哈哈。。。。。

  • 相关阅读:
    caffe BUG
    Ubuntu安装低版本gcc
    OpenCV的CV :: findHomography运行时错误
    opencv中使用 SURF算法匹配的遇到的问题
    C++ 类中特殊的成员变量(常变量、引用、静态)的初始化方法 --转
    SIFI与surf
    opencv复习
    opencv老版本的IplImage使用
    python学习笔记-对象持久化保存与恢复
    Styles and Themes
  • 原文地址:https://www.cnblogs.com/tianzhenyun/p/5284311.html
Copyright © 2011-2022 走看看