一、操作流程

1.获取证书文件

从证书供应商处下载对应tomcat适配的jks文件以及秘钥。

2.迁移证书

a.将jks证书文件拷贝至项目资源目录resouces下。
b.也可将证书存放至项目外,但后续配置路径需使用绝对路径。

3.修改项目配置文件

#https加密端口号 443
server.port=443
#SSL证书路径 一定要加上classpath:
#改成自己需要的证书名
server.ssl.key-store=classpath:*****.jks
#SSL证书密码
server.ssl.key-store-password=12345678
#证书类型
server.ssl.key-store-type=JKS
server.ssl.enabled=true

3.修改项目启动类

package com.barry.login;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.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class DiyApplication  {

    public static void main(String[] args) {
        SpringApplication.run(DiyApplication.class, args);
    }

    /**
     * http重定向到https
     * @return
     */
    @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("/*");
                collection.addMethod(DEFAULT_PROTOCOL);
                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监听的http的端口号
        connector.setPort(8080);
        connector.setSecure(false);
        //监听到http的端口号后转向到的https的端口号
        connector.setRedirectPort(443);//可调整到自己所需要的端口号,与配置文件中端口统一        return connector;
    }}

4.修改pom配置

防止java编译时对证书文件进行压缩破坏秘钥。
配置完成后先执行clean。

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>*.jks</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <includes>
                <include>*.jks</include>
            </includes>
        </resource>
    </resources></build>

二、问题整理

1.项目编译打包时报错

Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources) on project ***: Input length = 1 -> [Help 1]
解决方案:
在上述pom中添加的配置项导致报错,删除后改为将证书文件迁移至项目外,配置文件路径改为绝对路径,证书就能生效。

2.http的post请求被重定向至https的get方法导致无法定位。

解决方案:
在上述启动类的servletContainer方法中添加 collection.addMethod(DEFAULT_PROTOCOL);

转自:https://www.jianshu.com/p/e6de273342ae