注意:在实际的项目开发中,一般都会在nginx中配置https,不会在Tomcat中进行配置
步骤1:利用jdk自带的keytool工具生成https证书
keytool -genkey -alias myhttps -keyalg RSA -keysize 2048 -keystore wen_key.p12 -validity 365
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SdncbKhE-1611023097847)(C:\Users\23746\AppData\Roaming\Typora\typora-user-images\image-20210118164326823.png)]](https://i-blog.csdnimg.cn/blog_migrate/47a3d76600388ed5d76b1703241adfe1.png)
最后会在当前目录下生成一个密钥文件
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K64KH31R-1611023097850)(C:\Users\23746\AppData\Roaming\Typora\typora-user-images\image-20210118164428161.png)]](https://i-blog.csdnimg.cn/blog_migrate/f75289b608afb1c8c7cb92cbe801fcd9.png)
将该文件拷贝到SpringBoot项目的resources目录下,并在application.properties配置文件中配置如下ssl信息
server.ssl.key-alias=myhttps
server.ssl.key-store=classpath:wen_key.p12
server.ssl.key-store-password=123456
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MWbgp7WT-1611023097853)(C:\Users\23746\AppData\Roaming\Typora\typora-user-images\image-20210118165336581.png)]](https://i-blog.csdnimg.cn/blog_migrate/68590d44748ff75497163d9bc98c529e.png)
再使用http请求访问接口时就会报如下错误
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rslgnVen-1611023097855)(C:\Users\23746\AppData\Roaming\Typora\typora-user-images\image-20210118165438442.png)]](https://i-blog.csdnimg.cn/blog_migrate/f60f10c4bd5492ecbce78238ad7f205f.png)
只能使用https请求访问接口,如果想支持http请求,比如当客户端使用http协议的8081端口访问接口,服务器接收到请求之后自动转发到https的8080端口,可以进行如下配置
//这里面的配置代码都是一些程式化的,按照下面的配置就可以支持http请求
@Configuration
public class TomcatConfig {
@Bean
TomcatServletWebServerFactory tomcatServletWebServerFactory(){
TomcatServletWebServerFactory tomcatServletWebServerFactory = new TomcatServletWebServerFactory(){
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection securityCollection = new SecurityCollection();
securityCollection.addPattern("/*");
securityConstraint.addCollection(securityCollection);
context.addConstraint(securityConstraint);
}
};
tomcatServletWebServerFactory.addAdditionalTomcatConnectors(myConnector());
return tomcatServletWebServerFactory;
}
// 下面配置:当使用http协议的8081端口请求数据会转发到8080端口
private Connector myConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8081);
connector.setSecure(false);
connector.setRedirectPort(8080);
return connector;
}
}
再启动SpringBoot程序时可以在控制台看到内嵌的Tomcat已经开启了2个端口,http的8081端口和https的8080端口
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pWynzVEC-1611023097857)(C:\Users\23746\AppData\Roaming\Typora\typora-user-images\image-20210118171151702.png)]](https://i-blog.csdnimg.cn/blog_migrate/92bc1dae0165442208348af41c28eb5a.png)
写一个测试接口
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
在浏览器中输入http://localhost:8081/hello则会自动跳转到https://localhost:8080/hello,当然也可以直接访问https://localhost:8080/hello

本文介绍如何在SpringBoot项目中配置HTTPS,并通过Tomcat实现从HTTP到HTTPS的自动转发,包括证书生成、配置SSL信息及配置Tomcat连接器。
3275

被折叠的 条评论
为什么被折叠?



