云服务器添加HTTPS的解决方案

申请和获取证书
今天在阿里云申请了SSL证书。点击下载按钮
在这里插入图片描述
我选择了tomcat。
在这里插入图片描述

下载zip文件,并且解压
在这里插入图片描述

然后将这两个文件上传到服务器指定的文件夹下面。

上传地址:
在这里插入图片描述
这时候,我就要生成密钥了。就是生成那个domains.jks的文件。
然后把domains.jks拷贝到tomcat的conf中。
在这里插入图片描述

部署使用证书,验证HTTPS
开始配置server.xml。keystorePass=“这个是你的密钥” 这里是存放密钥的。密钥就在pfx-password.txt文件夹中。

<Connector                                                                                                                              
                port="443" protocol="HTTP/1.1"
                SSLEnabled="true" maxThreads="15"
                scheme="https" secure="true"
                keystoreFile="conf/domains.jks" 
                keystorePass="这个是你的密钥"
                clientAuth="false"
                sslProtocol="TLS" />

80端口也默认从定向到443端口

<Connector port="80" protocol="HTTP/1.1" redirectPort="443" />

在tomcat的conf下的web.xml中的welcome-file-list。配置如下内容。

<welcome-file-list>
	<welcome-file>index.html</welcome-file>
	<welcome-file>index.htm</welcome-file>
	<welcome-file>index.jsp</welcome-file>

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

</welcome-file-list>

那么我们重启服务器,能够看到https添加成功。
在这里插入图片描述
这样还没有结束,如果我们输入http://www.marsdl.com。没有自动走https协议,这个时候,我们还需要配置刚才的web.xml。
添加这个配置即可。

<security-constraint>
 <web-resource-collection>
 <web-resource-name>Automatic Forward to HTTPS/SSL
 </web-resource-name>
 <url-pattern>/*</url-pattern>
 </web-resource-collection>
 <user-data-constraint>
	<transport-guarantee>CONFIDENTIAL</transport-guarantee>
 </user-data-constraint>
</security-constraint>

这次我们直接在浏览器地址输入输入 www.marsdl.com,就会自动走https。
这里的原理是服务器rewrite url。因为浏览器都是默认http协议,在第一次请求后,服务器重写url变成https,这样下次浏览器会依据这个地址进行下一次的访问。

猜你喜欢

转载自blog.csdn.net/Hello_Ray/article/details/85157674