当前位置:   article > 正文

Spring boot2.X 配置https

Spring boot2.X 配置https

背景

最近项目组说要将 http 升级成 https 访问,证书也给到我们这边了,当然我们这边用的是个二级域名,采用的是通配符访问的方式,比如一级域名是这样(com.chinaunicom.cn),我们的则是(ams.chinaunicom.cn),跟域名申请方要的证书和密钥。然后我们就开始进行了配置,前端项目是用的 CLB 配置的端口转发,图形界面化的操作,将证书上传后就可以了,然后将我们申请的域名解析到了我们服务器的 IP 地址。我以为后端也要开启 https 访问,所以晚上就了个班,就自己搞了搞,其实也不是特别复杂,在这里跟大家分享一下,互相学习进步。

  1. 校验提供的证书信息
    使用 jdk 提供的 keytool 工具,来查看证书信息,命令介绍:
keytool -help
密钥和证书管理工具

命令:

 -certreq            生成证书请求
 -changealias        更改条目的别名
 -delete             删除条目
 -exportcert         导出证书
 -genkeypair         生成密钥对
 -genseckey          生成密钥
 -gencert            根据证书请求生成证书
 -importcert         导入证书或证书链
 -importpass         导入口令
 -importkeystore     从其他密钥库导入一个或所有条目
 -keypasswd          更改条目的密钥口令
 -list               列出密钥库中的条目
 -printcert          打印证书内容
 -printcertreq       打印证书请求的内容
 -printcrl           打印 CRL 文件的内容
 -storepasswd        更改密钥库的存储口令

使用 "keytool -command_name -help" 获取 command_name 的用法
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

使用命令查看证书信息,如下:

keytool -v -list -keystore ~/Documents/tomcat.jks
  • 1

输入证书口令,看到如下界面,图中标记的信息,我们在配置后端服务时要使用:

因为当时拿到这个证书时,keytool工具提示让迁移到行业标准格式 PKCS12,提供了如下命令:

Warning:
JKS 密钥库使用专用格式。建议使用 "keytool -importkeystore -srckeystore /Users/cainiao007/Documents/tomcat.jks -destkeystore /Users/cainiao007/Documents/tomcat.jks -deststoretype pkcs12" 迁移到行业标准格式 PKCS12。
  • 1
  • 2

那么我们就是用该命里进行迁移,输入证书口令,如下:

红色为升级后的证书,绿色的为升级前的版本,对老证书进行备份,真的是 666。

注意,此时的证书内容类型已经变了,不再是 JKS 了,我们再次使用命令对其内容进行查看,如下:

  1. 证书及配置文件
    将升级后的 jks 证书复制到工程的 resources 文件夹中,并增加如下配置:
  2. 修改 maven 打包配置,否则项目打包后,会将证书中的特殊符号进行处理,导致证书的大小会发生改变,大小发生改变后,就无法使用了,如下:
<plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <version>3.2.0</version>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
                        <nonFilteredFileExtension>jks</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>
        </plugins>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  1. 增加配置类,将http请求转发为 https 请求,如下:
package cn.chinaunicom.config;

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.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description
 * @Author pgq
 * @DATE 2024/3/26 18:01
 * @Version 1.0
 */
@Configuration
public class HttpToHttpsConfig {

    private Integer httpPort=8082;

    private Integer httpsPort=443;

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

    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(httpPort);
        connector.setSecure(false);
        connector.setRedirectPort(httpsPort);
        return connector;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

这样及支持 http,也支持 https,如下:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/328920
推荐阅读
  

闽ICP备14008679号