SpringBoot 2.x中为tomcat配置ssl(https)支持

参考来源:https://www.cnblogs.com/imfjj/p/9058443.html

https://blog.csdn.net/jackymvc/article/details/81077885

配置证书:

keytool -genkeypair -alias tomcat -keyalg RSA -keystore tomcat.key

依次填入以下内容:

mcat.key
输入密钥库口令:
再次输入新口令:
您的名字与姓氏是什么?
  [Unknown]:  localhost
您的组织单位名称是什么?
  [Unknown]:  localhost
您的组织名称是什么?
  [Unknown]:  xxx Co,.Ltd
您所在的城市或区域名称是什么?
  [Unknown]:  KunShan
您所在的省/市/自治区名称是什么?
  [Unknown]:  SuZhou
该单位的双字母国家/地区代码是什么?
  [Unknown]:  China
CN=localhost, OU=localhost, O="xxxCo,.Ltd", L=KunShan, ST=SuZhou, C=Chin
a是否正确?
  [否]:y

输入 <tomcat> 的密钥口令
        (如果和密钥库口令相同, 按回车):
再次输入新口令:

把生成的证书放入 resources目录

配置application.yml

debug: true
server:
  port: 8110
  tomcat:
    max-threads: 800
    accept-count: 30000
    min-spare-threads: 20
    max-connections: 30000
  ssl:
    key-store: classpath:tomcat.key
    key-store-type: JKS
    key-alias: tomcat
    #证书密码
    key-store-password: xxxx

中途遇到报错:

2019-09-09 08:43:49.752 default [main] DEBUG o.s.b.d.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.boot.web.embedded.tomcat.ConnectorStartFailedException: Connector configured to listen on port 8110 failed to start
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.checkThatConnectorsHaveStarted(TomcatWebServer.java:228)
    at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start(TomcatWebServer.java:203)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.startWebServer(ServletWebServerApplicationContext.java:300)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:162)
    at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:553)
    at org.springframework.context.support.AbstractApplicationContext.jrLockAndRefresh(AbstractApplicationContext.java:40002)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:41008)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
    at com.xxx.web.WebApplication.main(WebApplication.java:34)
2019-09-09 08:43:49.752 default [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - 

解决方法:

参考来源:Spring Boot- The Tomcat connector configured to listen on port 8080 failed to start

SpringBoot 2.x

新增一个组件类


@Component
public class CustomContainer implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

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

    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        factory.setPort(serverPort);
    }
}


 

 SpringBoot 1.x解决方法:

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {

        container.setPort(8085);

    }

}
原文地址:https://www.cnblogs.com/passedbylove/p/11489686.html