1 简介
本文将介绍在 Springboot
中如何通过代码实现 Http
到 Https
的重定向,本文仅讲解Tomcat
作为容器的情况,其它容器将在以后一一道来。
建议阅读之前的相关文章:
(2) HTTPS之密钥知识与密钥工具Keytool和Keystore-Explorer
2 相关概念
2.1 什么叫重定向
所谓重定向,就是本来你想浏览地址A的,但是到达服务端后,服务端认为地址A的界面不在了或者你没权限访问等原因,不想你访问地址A;就告诉你另一个地址B,然后你再去访问地址B。
对于重定向一般有两个返回码:
- 301:永久性重定向;
- 302:暂时性重定向。
通过 Chrome
查看网络详情,记录了几个网站的重定向情况:
网站 | 域名 | 重定向代码 | 重定向后的网址 |
---|---|---|---|
南瓜慢说 | www.pkslow.com | 301 | https://www.pkslow.com |
www.google.com | 307 | https://www.google.com | |
Apple | www.apple.com | 307 | https://www.apple.com |
支付宝 | www.alipay.com | 301 | https://www.alipay.com |
www.qq.com | 302 | https://www.qq.com | |
百度 | www.baidu.com | 307 | https://www.baidu.com |
注:307也是重定向的一种,是新的状态码。
2.2 为什么要重定向
结合我上面特意列的表格,是不是大概想到了为何要做这种重定向?不难发现上面的重定向都在做一件事,就是把 http
重定向为 https
。原因如下:
(1) http
是不安全的,应该使用安全的 https