一文教你将 SpringBoot 网站升级为 HTTPS

今年 2 月份,谷歌宣布:7 月起,Chrome 浏览器的地址栏将把全部 HTTP 标示为不安全网站!现在已是 6 月底了,是时候将抛弃容易被第三方监控的 HTTP 拥抱 HTTPS 了。web

下面就从 HTTPS 证书申请、网站 HTTPS 的配置、HTTP 重定向到 HTTPS 三个方面教你将一个 SpringBoot 网站升级为安全的 HTTPS。spring

证书申请

目前国内提供免费 HTTPS 证书的云服务商并很少,一贯不大方的腾讯此次却是很大方,腾讯云提供了免费的亚洲诚信品牌免费型 DV 版 SSL 证书,注册认证过的用户便可免费申请 20 个免费证书。apache

建立证书

选择证书

首先你须要注册认证腾讯云,而后进入到 SSL证书 管理菜单,点击申请证书按钮,而后在弹框中选择免费版DVSSL证书,点击肯定按钮。 编程

选择证书

完善域名信息

而后填写你的域名信息,通用名称即为你要申请证书的域名,申请邮箱填写我的经常使用邮箱便可,以下图所示: 浏览器

填写域名信息

完成域名验证

选择DNS验证后,会看到如下信息: tomcat

DNS验证

此时进入到域名提供商的后台,添加一条解析便可: 安全

添加TXT解析

最后回到证书验证界面,点击验证便可认证成功,认证成功后坐等审核经过便可,通常审核时间为几分钟到几小时不等。bash

审核完成后,就能够在证书列表界面下载证书了,下载的到的文件是一个 ZIP 压缩包,压缩包里面包含了各类经常使用的网站托管软件所需的证书格式: 服务器

证书压缩包内容

网站配置

拷贝密钥

SpringBoot 默认使用 Tomcat 进行网站托管,所以从压缩包中的 Tomcat 目录将证书(文件后缀为jks)拷贝到 SpringBoot 工程的 resources 目录下便可: app

拷贝证书到 resources 目录

更新配置

证书拷贝完整后,打开配置文件 application.yml,而后修改网站的端口为 443key-store-password 能够在证书压缩包的 Tomcat 文件夹下的文本文件中找到,此外配置一下 SSL 证书的类型以及路径等信息便可:

server:
  address: 0.0.0.0
  port: 443
  ssl:
    enabled: true
    key-store: classpath:luooqi.com.jks
    key-store-password: xxxxxxxxxxxx
    key-store-type: JKS
复制代码

至此即可以启动运行项目,能够在地址栏输入 https://yourdomain 便可访问,在服务器上发布后能够看到以下效果:

认证发布成功

将 HTTP 请求重定向到HTTPS

网站虽然升级到了 HTTPS,可是不少老用户并不知情,当他们访问旧版的 HTTP 地址时会发现网站已经没法访问:

旧版 HTTP 网址没法正常访问

所以最佳的作法是将 HTTP 重定向到 HTTPS,下面就教你如何经过代码实现。

添加 HTTP 端口配置

首先在配置文件中添加自定义的 HTTP 端口配置:

http-port: 80
复制代码

创建重定向关系

新建一个配置类 HttpsConfiguration,在类中将配置文件中自定义的 HTTP 端口和 HTTPS 的端口都注入进来,而后建立一个新的 Connector 来处理 HTTP 请求,同时设置 Connector 的端口为注入的 HTTP 端口,重定向端口(setRedirectPort)为新的 HTTPS 端口。

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpsConfiguration {
    @Value("${http-port}")
    private int port;

    @Value("${server.port}")
    private int sslPort;

    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(redirectConnector());
        return tomcat;
    }

    private Connector redirectConnector() {
        Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
        connector.setScheme("http");
        connector.setPort(port);
        connector.setSecure(false);
        connector.setRedirectPort(sslPort);
        return connector;
    }
}
复制代码

特别注意

上面代码中须要特别注意的是,TomcatServletWebServerFactory 必需要在其 postProcessContext 方法中添加 HTTP 的匹配范围 addPattern("/*"),不然重定向无效。


Any Code,Code Any!

扫码关注『AnyCode』,编程路上,一块儿前行。

相关文章
相关标签/搜索