Spring boot內置Tomcat上支持Https端口。
步驟:
1. 生成證書或者從機構購買證書
下面生成自定義的證書
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
2. 把證書添加到項目中
將生成的keystore.p12放入到資源文件夾下面。具體放在哪里呢?
我的項目是放在: src/main/resources下面
3. 修改Spring-boot的配置文件,打開https
在yml配置文件中,使用證書,打開https:
server: port: 12002 ssl: key-store: classpath:keystore.p12 key-store-password: peipei123 keyStoreType: PKCS12 keyAlias: tomcat
4. 編譯,啟動,用瀏覽器訪問。(這個地方最坑爹了)
直接編譯,啟動。一般到這個地方就沒問題。
但是但是,我那個倒霉蛋啊。我就是啟動不了。
最后查了一下,為什么呢?
我們實際使用的keystore.p12正式是我們真的keystore.p12嗎?
修改pom.xml,告訴丫,build的時候別修改我的證書。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<!-- 過濾后綴為pem、pfx的證書文件 -->
<nonFilteredFileExtensions>
<nonFilteredFileExtension>pem</nonFilteredFileExtension>
<nonFilteredFileExtension>pfx</nonFilteredFileExtension>
<nonFilteredFileExtension>p12</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
到此才能完全搞定https。
可憐我修改了半天啊。