tomcat中如何配置https

来源:互联网 发布:python http接口 编辑:程序博客网 时间:2024/05/16 06:22

1、自己颁发证书(进入安装jdk目录下的bin文件夹,查找使用 java -verbose  最后一行就是)


输入 keytool -genkey -alias xpdlapp -keyalg RSA -keystore 1024 -validity 365 -keystore d:/app.keystore


-genkeypair  表示生成密钥
 -keyalg      指定密钥算法,这里指定为RSA算法。
 -keysize     指定密钥长度,默认1024位,这里指定为2048位。
 -sigalg      指定数字签名算法,这里指定为SHA1withRSA算法。
 -validity    指定证书有效期,这里指定为36000天。
 -alias       指定别名,这里是www.lesaas.cn
 -keystore    指定密钥库存储位置,这里是d:/app.keystore




2、修改tomcat中conf中的server.xml文件
a、将d:/app.keystore复制到tomcat的conf文件夹下
b、将8443端口注释的代码打开,添加文件路径和密码


<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
      keystoreFile="conf/app.keystore" keystorePass="123456"
               clientAuth="false" sslProtocol="TLS" />


3、在项目的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>


现在可以直接访问https://localhost:8443/test
也可以访问http://localhost:8080,会自动跳转到https,如果希望只能够通过https访问,可以在server.xml中将下面的配置注释:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
原创粉丝点击