Tomcat的Https设置及Http自动跳转Https

https相关介绍

可以这样理解https=http+ssl。

https的设置

1、申明CA(Certificate Authority)数字证书

(1)这里推荐阿里平台免费的CA数字证书。

https://common-buy.aliyun.com/?commodityCode=cas#/buy

按提示进行操作即可,证书签发成功后,下载相关文档,内容如下:

第一个红框是密钥文件,第二是密码。

(2)将生成的密钥文件上传至云服务器,如/usr/local/tomcat/conf下

(3)编辑conf/server.xml文件

添加

<Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtocol"
    maxThreads="150" SSLEnabled="true"
    clientAuth="false" sslProtocl="TLS"
    keystoreType="PKCS12"
    keystorePass="214103137910210"
    keystoreFile="conf/214103137910210.pfx"
>

</Connector

(4)将8080端口改为80,8443改为443

将8009处的8443改为443

这样https请求时就不用自动附带8443端口了。

http自动跳转https

1、编辑conf/web.xml文件,在welcom-file-list下添加

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

2、web.xml中<security-constraint>和四种认证类型

(1)security-constraint的子元素<http-method>是可选的,如果没有http-method元素,这表示将禁止所有HTTP方法访问相应的资源。

(2)web-resource-collection

此元素确定应该保护的资源,所有security-constraint元素必须包含至少一个web-resource-collection项,此元素由一个给定任意标识名称的web-resource-name元素、一个确定应该保护的url-pattern元素、一个http-method元素和一个description元素。

(3)auth-constraint

指出哪些用户具有保护资源的访问权,由一个或多个role-name元素,description元素。

(4)user-data-constraint

指出访问相关资源时,进行传输层保护。它包含一个transport-guarantee元素和description元素。

transport-guarantee为NONE值将对所用的通讯协议不加限制,INTEGRAL和CONFIDENTIAL虽然在原理上有差别,但在当前实践中,它们都只是简单的要求使用SSL。

猜你喜欢

转载自blog.csdn.net/CHS007chs/article/details/85695511