一.创建springBoot项目




二.配置ssl证书
- 将证书放到项目的resources文件夹下,如下图所示

- 在application.yml 文件中做如下配置(如果你的项目是application.properties可以修改后缀名为yml或者可以参考yml写法做响应的修改)

server:
port: 6443
ssl:
key-store: classpath:springboot.p12
key-store-password: 123456
keyStoreType: PKCS12
enabled: true
http:
port: 6880
三.创建Tomcat配置类

package com.example.demo.config;
import org.apache.catalina.connector.Connector;
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;
/**
* @author TM-majian
* @version 1.0
* @since 2021/5/7 19:21
*/
@Configuration
public class SSLConfig {
@Value("${http.port}")
private Integer port;
@Bean
public ServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setPort(port);
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
}
四.创建controller测试

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author TM-majian
* @version 1.0
* @since 2021/5/7 19:19
*/
@RestController
public class TestController {
@RequestMapping("/helloHttps")
public String helloHttps(){
return "hello https";
}
}
五.启动项目测试
- 本地测试

通过控制台日志可以看到两个端口都已启动完成,浏览器访问http连接http://localhost:6880/helloHttps

访问https连接
https://localhost:6443/helloH...

**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。