文章目录

  • 前置准备
  • 云计算域名注册
  • 自生成SSL证书
  • 创建SpringBoot项目
  • 配置yml或者properties文件
  • 启动项配置
  • 大功告成


前置准备

云计算域名注册

首先我们要解决证书问题,如果你有云域名就可以免费获取一年的证书,具体的可以参考各大云计算平台,找到你购买的域名产品即可下载,下面以腾讯云为例

登录腾讯计算—> 进入控制台

如何获取springboot程序端口号_spring boot


如果没有找个SSL证书,可以在这里搜索

如何获取springboot程序端口号_http_02


申请免费的域名证书,这里使用免费1年的,如果你有马内,你可以用付费的。

如何获取springboot程序端口号_spring boot_03


填写必要信息

如何获取springboot程序端口号_tomcat_04

提交审核,并且等待几分钟,签发成功下载

如何获取springboot程序端口号_https_05


因为SpringBoot是内置的Tomcat,因此证书文件我们下载这个

如何获取springboot程序端口号_如何获取springboot程序端口号_06


下载的是一个压缩包,里面包含你的证书密码文件(txt)和一个pfx证书文件

如何获取springboot程序端口号_如何获取springboot程序端口号_07


文件就先准备到这里

自生成SSL证书

JDK1.4版本之后就支持自生成域名证书了

首先我们找到JDK bin目录下

如何获取springboot程序端口号_如何获取springboot程序端口号_08

keytool -genkey -alias 名称 -keystore 证书名称.pfx -storetype PKCS12 -keyalg RSA -validity 365 -keysize 2048

-keystore 后面的证书名称.pfx 前面最后跟上你要生成文件的路径,不然你不一定能找得到

创建SpringBoot项目

配置yml或者properties文件

这里创建项目过程就省略了

主要是配置

如何获取springboot程序端口号_https_09

# SSL证书
server:
  tomcat:
    uri-encoding: utf-8
  ssl:
    key-store: example.pfx #你的证书路径,这里建议把pfx文件放在项目的根路径下,与src平级即可
    key-store-password: 123456 # 密钥
    keyStoreType: PKCS12 
  port: 9000 # https端口
  httpport: 9001

至于文件放的位置,主要考虑一下三种情况

  • 多模块,应当放在父级别根目录下
  • 放在resources下,需要追加classpath:
  • 放在其他盘符,需要写绝对路径

启动项配置

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.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
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;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication(
        scanBasePackages = {
                "com.personloger.service",
                "com.personloger.console",
                "com.personloger.config"
        }
)
@MapperScan("com.personloger.dao")
@EnableScheduling
public class ConsoleApplication {

    @Value("${server.httpport}")
    private Integer port;

    @Value("${server.port}")
    private Integer ports;

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


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

    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){
        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(connector);
        return tomcat;
    }

}

大功告成

如何获取springboot程序端口号_tomcat_10


可以用Swagger文档测试一下即可