
- Spring Boot 教程
- Spring Boot - 首頁
- Spring Boot - 簡介
- Spring Boot - 快速入門
- Spring Boot - 啟動引導
- Spring Tool Suite
- Spring Boot - Tomcat 部署
- Spring Boot - 構建系統
- Spring Boot - 程式碼結構
- Spring Bean 和依賴注入
- Spring Boot - 執行器
- Spring Boot - 啟動器
- Spring Boot - 應用屬性
- Spring Boot - 配置
- Spring Boot - 註解
- Spring Boot - 日誌記錄
- 構建 RESTful Web 服務
- Spring Boot - 異常處理
- Spring Boot - 攔截器
- Spring Boot - Servlet 過濾器
- Spring Boot - Tomcat 埠號
- Spring Boot - Rest 模板
- Spring Boot - 檔案處理
- Spring Boot - 服務元件
- Spring Boot - Thymeleaf
- 使用 RESTful Web 服務
- Spring Boot - CORS 支援
- Spring Boot - 國際化
- Spring Boot - 排程
- Spring Boot - 啟用 HTTPS
- Spring Boot - Eureka 伺服器
- 使用 Eureka 註冊服務
- 閘道器代理伺服器和路由
- Spring Cloud 配置伺服器
- Spring Cloud 配置客戶端
- Spring Boot - Actuator
- Spring Boot - Admin 伺服器
- Spring Boot - Admin 客戶端
- Spring Boot - 啟用 Swagger2
- Spring Boot - 使用 SpringDoc OpenAPI
- Spring Boot - 建立 Docker 映象
- 追蹤微服務日誌
- Spring Boot - Flyway 資料庫
- Spring Boot - 傳送郵件
- Spring Boot - Hystrix
- Spring Boot - WebSocket
- Spring Boot - 批處理服務
- Spring Boot - Apache Kafka
- Spring Boot - Twilio
- Spring Boot - 單元測試用例
- Rest Controller 單元測試
- Spring Boot - 資料庫處理
- 保護 Web 應用程式
- Spring Boot - 使用 JWT 的 OAuth2
- Spring Boot - Google Cloud Platform
- Spring Boot - Google OAuth2 登入
- Spring Boot 資源
- Spring Boot - 快速指南
- Spring Boot - 有用資源
- Spring Boot - 討論
Spring Boot - 啟用 HTTPS
預設情況下,Spring Boot 應用程式啟動時使用 HTTP 8080 埠。
... [2024-09-10T16:57:23Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Starting ProtocolHandler ["http-nio-8080"] [2024-09-10T16:57:23Z] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [main] [243] [INFO ] Tomcat started on port 8080 (http) with context path '/' [2024-09-10T16:57:24Z] [org.springframework.boot.StartupInfoLogger] [main] [56] [INFO ] Started DemoApplication in 1.558 seconds (process running for 2.343)
您需要按照以下步驟在 Spring Boot 應用程式中配置 HTTPS 和 443 埠:
獲取 SSL 證書 – 建立自簽名證書或從證書頒發機構獲取證書
啟用 HTTPS 和 443 埠
自簽名證書
要建立自簽名證書,Java 執行時環境捆綁了證書管理實用程式 keytool。此實用程式用於建立自簽名證書。程式碼如下所示:
keytool -genkey -alias tomcat -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650 Enter keystore password: Re-enter new password: What is your first and last name? [Unknown]: What is the name of your organizational unit? [Unknown]: What is the name of your organization? [Unknown]: What is the name of your City or Locality? [Unknown]: What is the name of your State or Province? [Unknown]: What is the two-letter country code for this unit? [Unknown]: Is CN = Unknown, OU=Unknown, O = Unknown, L = Unknown, ST = Unknown, C = Unknown correct? [no]: yes Generating 2,048 bit RSA key pair and self-signed certificate (SHA384withRSA) with a validity of 3,650 days for: CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown
此程式碼將生成名為 keystore.p12 的 PKCS12 金鑰庫檔案,證書別名為 tomcat。我們將金鑰庫儲存在 **E:/ > Dev** 目錄中,金鑰庫密碼為 springboot。
配置 HTTPS
我們需要將伺服器埠 (443)、金鑰庫檔案路徑、金鑰庫密碼、金鑰庫型別和金鑰別名提供給 application.properties 檔案。請觀察以下程式碼:
server.port: 443 server.ssl.key-store: E:/Dev/keystore.p12 server.ssl.key-store-password: springboot server.ssl.keyStoreType: PKCS12 server.ssl.keyAlias: tomcat
如果您使用 YAML 屬性,可以使用以下 application.yml 程式碼:
server: port: 443 ssl: key-store: E:/Dev/keystore.p12 key-store-password: springboot keyStoreType: PKCS12 keyAlias: tomcat
您可以建立一個可執行的 JAR 檔案,並使用以下 Maven 或 Gradle 命令執行 Spring Boot 應用程式。
對於 Maven,您可以使用以下命令:
mvn clean install
“BUILD SUCCESS”之後,您可以在 target 目錄下找到 JAR 檔案。
對於 Gradle,您可以使用以下命令:
gradle clean build
“BUILD SUCCESSFUL”之後,您可以在 build/libs 目錄下找到 JAR 檔案。
現在,使用以下命令執行 JAR 檔案:
java –jar <JARFILE>
現在,應用程式已在使用 https 的 Tomcat 443 埠上啟動,如下所示:
[2024-09-10T17:01:59Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Starting ProtocolHandler ["https-jsse-nio-443"] [2024-09-10T17:01:59Z] [org.apache.juli.logging.DirectJDKLog] [main] [173] [INFO ] Connector [https-jsse-nio-443], TLS virtual host [_default_], certificate type [UNDEFINED] configured from keystore [C:\Users\Tutorialspoint\.keystore] using alias [tomcat] with trust store [null] [2024-09-10T17:01:59Z] [org.springframework.boot.web.embedded.tomcat.TomcatWebServer] [main] [243] [INFO ] Tomcat started on port 443 (https) with context path '/' [2024-09-10T17:01:59Z] [org.springframework.boot.StartupInfoLogger] [main] [56] [INFO ] Started DemoApplication in 1.789 seconds (process running for 2.557)
廣告