Java web项目 本地配置https调试

泪湿孤枕 提交于 2020-01-22 00:37:58

一、创建密匙

  网上有很多教程,就不在此赘述了。

  假设最后生成的密匙为tomcat.keystore 密码为123456。

二、配置tomcat

  首先,将密匙移到tomcat下根目录下。

  进入conf文件夹,配置sever.xml。

  window系统下,本地配置:

<Connector port="8080" protocol="org.apache.coyote.http11.Http11Protocol" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" useBodyEncodingForURI="true"
               keystoreFile="tomcat.keystore" keystorePass="123456"/>

  其中,重点为:将 protocol="HTTP/1.1" 替换为 protocol="org.apache.coyote.http11.Http11Protocol"。

  相关参数来源见如下官方网址:http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration

三、程序HTTP自动跳转到HTTPS

  在程序中web.xml中加入:

<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>

  注:此项是选填项,若跳过该步,则http,https均可使用。

四、最后启动项目

  使用https://127.0.0.1:8080访问页面,页面成功打开即tomcat下的https配置成功。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!