完整代碼地址在結尾!久妆!
第一步妒御,獲取ssl證書,有兩種方式
- 自己通過jdk的keytool命令生成
- 通過證書授權機構購買
本文采用第二種镇饺,在阿里云購買了免費的證書乎莉,需要提交資料,等待審核通過即可奸笤。
第二步惋啃,購買后在阿里云控制臺下載證書,類型選為Tomcat的即可
下載后在證書目錄下執(zhí)行阿里云提供的命令监右,密碼都填pfx-password.txt中的內(nèi)容(需要三次)边灭,會生成javalsj.jks文件
keytool -importkeystore -srckeystore xxx.pfx -destkeystore javalsj.jks -srcstoretype PKCS12 -deststoretype JKS
第三步,創(chuàng)建工程健盒,并將上面生成的javalsj.jks文件移動到resources目錄下
第四步绒瘦,在pom.xml加入依賴,如下
<!-- 排除 Tomcat 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除 Tomcat 依賴 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Undertow -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<!-- Undertow 相關依賴 -->
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<version>2.1.4.Final</version>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
<version>2.1.4.Final</version>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-websockets-jsr</artifactId>
<version>2.1.4.Final</version>
</dependency>
第五步扣癣,在application.yml配置文件配置kafka
# http服務端口
custom:
server:
http:
port: 8087
spring:
application:
name: https-demo-server
# 開啟https支持
server:
http2:
enabled: true
port: 8443 # https服務端口
servlet:
context-path: /api # 接口統(tǒng)一前綴
ssl: # 指定ssl證書
key-store: classpath:javalsj.jks
key-store-password: tuc0KI70
undertow: # 配置 undertow 服務器
buffer-size: 512
io-threads: 2
worker-threads: 20
第六步惰帽,創(chuàng)建WebServerConfig類,如下
import io.undertow.Undertow;
import io.undertow.UndertowOptions;
import io.undertow.servlet.api.SecurityConstraint;
import io.undertow.servlet.api.SecurityInfo;
import io.undertow.servlet.api.TransportGuaranteeType;
import io.undertow.servlet.api.WebResourceCollection;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Description: 采用Undertow作為服務器父虑,支持Https服務配置
* @Author: luoyu
* @Date: 2020/7/16 10:39 下午
* @Version: 1.0.0
*/
@Configuration
public class WebServerConfig {
/**
* http服務端口
*/
@Value("${custom.server.http.port}")
private Integer httpPort;
/**
* https服務端口
*/
@Value("${server.port}")
private Integer httpsPort;
/**
* @author jinhaoxun
* @description 采用Undertow作為服務器该酗。
* Undertow是一個用java編寫的、靈活的士嚎、高性能的Web服務器呜魄,提供基于NIO的阻塞和非阻塞API,特點:
* 非常輕量級莱衩,Undertow核心瓶子在1Mb以下爵嗅。它在運行時也是輕量級的,有一個簡單的嵌入式服務器使用少于4Mb的堆空間笨蚁。
* 支持HTTP升級睹晒,允許多個協(xié)議通過HTTP端口進行多路復用。
* 提供對Web套接字的全面支持赚窃,包括JSR-356支持册招。
* 提供對Servlet 3.1的支持岔激,包括對嵌入式servlet的支持勒极。還可以在同一部署中混合Servlet和本機Undertow非阻塞處理程序。
* 可以嵌入在應用程序中或獨立運行虑鼎,只需幾行代碼辱匿。
* 通過將處理程序鏈接在一起來配置Undertow服務器键痛。它可以對各種功能進行配置,方便靈活匾七。
* @return ServletWebServerFactory
*/
@Bean
public ServletWebServerFactory undertowFactory() {
UndertowServletWebServerFactory undertowFactory = new UndertowServletWebServerFactory();
undertowFactory.addBuilderCustomizers((Undertow.Builder builder) -> {
builder.addHttpListener(httpPort, "0.0.0.0");
// 開啟 HTTP2
builder.setServerOption(UndertowOptions.ENABLE_HTTP2, true);
});
undertowFactory.addDeploymentInfoCustomizers(deploymentInfo -> {
// 開啟 HTTP 自動跳轉至 HTTPS
deploymentInfo.addSecurityConstraint(new SecurityConstraint()
.addWebResourceCollection(new WebResourceCollection().addUrlPattern("/*"))
.setTransportGuaranteeType(TransportGuaranteeType.CONFIDENTIAL)
.setEmptyRoleSemantic(SecurityInfo.EmptyRoleSemantic.PERMIT))
.setConfidentialPortManager(exchange -> httpsPort);
});
return undertowFactory;
}
}
第七步絮短,創(chuàng)建TestController類,如下
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description:
* @Author: luoyu
* @Date: 2020/7/16 10:39 下午
* @Version: 1.0.0
*/
@RestController
@RequestMapping("/test")
public class TestController {
/**
* @author luoyu
* @description 測試接口
*/
@GetMapping(value = "/get", produces = "application/json; charset=UTF-8")
public String getTest() throws Exception {
return "Hello";
}
}