1.准备证书
2.1 springboot 1.x配置
2.2 springboot 2.x配置
1.准备证书:
keytool -genkeypair -alias tomcat -keyalg RSA -keystore C:\tomcat.key #注意要用.key结尾证书文件
更多keytool命令

C:\Users\haonan>keytool 密钥和证书管理工具 命令: -certreq 生成证书请求 -changealias 更改条目的别名 -delete 删除条目 -exportcert 导出证书 -genkeypair 生成密钥对 -genseckey 生成密钥 -gencert 根据证书请求生成证书 -importcert 导入证书或证书链 -importpass 导入口令 -importkeystore 从其他密钥库导入一个或所有条目 -keypasswd 更改条目的密钥口令 -list 列出密钥库中的条目 -printcert 打印证书内容 -printcertreq 打印证书请求的内容 -printcrl 打印 CRL 文件的内容 -storepasswd 更改密钥库的存储口令 使用 "keytool -command_name -help" 获取 command_name 的用法 C:\Users\haonan>keytool -genkeypair -help keytool -genkeypair [OPTION]... 生成密钥对 选项: -alias <alias> 要处理的条目的别名 -keyalg <keyalg> 密钥算法名称 -keysize <keysize> 密钥位大小 -sigalg <sigalg> 签名算法名称 -destalias <destalias> 目标别名 -dname <dname> 唯一判别名 -startdate <startdate> 证书有效期开始日期/时间 -ext <value> X.509 扩展 -validity <valDays> 有效天数 -keypass <arg> 密钥口令 -keystore <keystore> 密钥库名称 -storepass <arg> 密钥库口令 -storetype <storetype> 密钥库类型 -providername <providername> 提供方名称 -providerclass <providerclass> 提供方类名 -providerarg <arg> 提供方参数 -providerpath <pathlist> 提供方类路径 -v 详细输出 -protected 通过受保护的机制的口令 使用 "keytool -help" 获取所有可用命令
2.springboot 配置:
->application.properties
server.port=8443 server.ssl.key-store=classpath:tomcat.key server.ssl.key-store-type=JKS server.ssl.key-store-password=123456
->把生产的证书复制到springboot项目的resource目录中去
2.1 springboot 1.x配置:

// 对http协议进行https跳转,这个connector会设置到web容器中
@Bean
public Connector httpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
}
// 自定义web容器的container
// springboot 1.x & 2.x 的区别在于WebServletFactory的类型
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
2.2 springboot 2.x配置

// 对http协议进行https跳转,这个connector会设置到web容器中
@Bean
public Connector httpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
}
// 自定义web容器的container
// springboot 1.x & 2.x 的区别在于WebServletFactory的类型
@Bean
public TomcatServletWebServerFactory servletContainer() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
}
-> https ?
-> tomcat架构
