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。
可怜我修改了半天啊。
Spring boot启动Https( 全流程,参考若干资料)
原文:http://www.cnblogs.com/lixiaopei/p/6433513.html