启用Https
启用Https协议,需要在服务端有一个证书。开发环境可以在通过如下指令生成一个证书及其KeyStore。
keytool -genkeypair -alias springboot -keyalg RSA -dname "CN=SERVER1,OU=Unit,O=Elim,L=City,S=Province,C=CN" -keypass 123456 -keystore server.jks -storepass 123456 -storetype jks
上面指令会在当前路径下生成一个名为server.jks的KeyStore。我们可以把它放到我们的Spring Boot工程中,然后在Spring Boot的application.properties中通过server.ssl.xxx
指定启用Https相关的配置信息。下面的属性都很好理解,笔者就不再赘述了。
server.ssl.enabled=true
server.ssl.keyAlias=springboot
server.ssl.keyPassword=123456
server.ssl.keyStorePassword=123456
server.ssl.keyStore=classpath:config/server.jks
server.ssl.keyStoreType=JKS
这样当重新启动应用后,再访问应用就只能通过https协议访问了。假设我们希望同时开启http和https协议访问,那么它们需要在不同的端口上。我们之前已经启用了https协议,假设是监听在端口8888上,现在我们需要开启一个http协议,监听在8081端口上。那么我们可以定义一个TomcatServletWebServerFactory
类型的bean,通过它额外添加一个通过http协议监听在8081端口的Connector,比如下面这样。
@Configuration
public class TomcatConfiguration {
@Bean
public TomcatServletWebServerFactory servletContainer(){
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addAdditionalTomcatConnectors(this.httpConnector());
return tomcat;
}
private Connector httpConnector(){
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setPort(8081);
connector.setSecure(false);
return connector;
}
}
这样我们的应用就可以同时通过http://localhost:8081
和https://localhost:8888
访问了。如果我们希望同时开启http和https协议,但是当用户通过http协议访问时我们强制它通过https协议访问。则可以把我们的TomcatServletWebServerFactory
定义成如下这样。它通过对Context的自定义指定了必须通过https协议访问,在Connector中指定了http协议访问时跳转到8888端口。
@Configuration
@Profile("https")
public class TomcatConfiguration {
@Bean
public TomcatServletWebServerFactory servletContainer(){
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addContextCustomizers(context -> {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
});
tomcat.addAdditionalTomcatConnectors(redirectConnector());
return tomcat;
}
private Connector redirectConnector(){
Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
connector.setScheme("http");
connector.setPort(8081);
connector.setSecure(false);
connector.setRedirectPort(8888);
return connector;
}
}
参考文档
https://memorynotfound.com/spring-boot-configure-tomcat-ssl-https/
(注:本文基于Spring Boot 2.0.3所写)
相关推荐
适合初学者spring-boot——web笔记
spring boot整合JPA——demo
日志,通常不会在需求阶段作为一个功能单独提出来,也不会在产品方案中看到它的细节。但是,这丝毫不影响它在任何一个系统中的重要的地位。 为了保证服务的高可用,发现问题一定要即使,解决问题一定要迅速,所以...
Spring boot——@DeclareParents例子...
java毕业设计——基于spring boot的就业信息管理网站设计与实现(源码+数据库).zip java毕业设计——基于spring boot的就业信息管理网站设计与实现(源码+数据库).zip java毕业设计——基于spring boot的就业信息管理...
spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决...
本课程内容包括Spring简介、Spring Boot简介、安装JDK、安装Maven、第一个Spring Boot程序(使用Spring Initializr...Spring Boot Actuator(启用生产功能、终点)、阿里云服务器、Xshell、Xftp、Spring Boot部署等等...
获取Spring Boot 2微框架的可重用代码配方和代码段 了解Spring Boot 2如何与其他Spring API,工具和框架集成 访问Spring MVC和新的Spring Web Sockets,以实现更简单的Web开发 使用微服务进行Web服务开发并与Spring ...
Beginning Spring Boot 2 Beginning Spring Boot 2 Beginning Spring Boot 2
基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring ...
基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 ...
java毕业设计——基于spring boot的音乐播放网站设计与实现(源码+数据库).zip java毕业设计——基于spring boot的音乐播放网站设计与实现(源码+数据库).zip java毕业设计——基于spring boot的音乐播放网站设计与...
This book will help you understand what Spring Boot is, how Spring Boot helps you build Spring-based applications quickly and easily, and the inner workings of Spring Boot using easy-to-follow ...
Spring Boot简化了基于Spring的应用开发,通过少量的代码就能创建一个独立的、产品级别的Spring应用。Spring Boot为Spring平台及第三方库提供开箱即用的设置,这样你就可以有条不紊地开始。多数Spring Boot应用只...
Pro Spring Boot 2: An Authoritative Guide to Building Microservices, Web and Enterprise Applications, and Best Practices Quickly and productively develop complex Spring applications and microservices...
java毕业设计——基于spring boot的旅游网站设计与实现(源码+数据库).zip java毕业设计——基于spring boot的旅游网站设计与实现(源码+数据库).zip java毕业设计——基于spring boot的旅游网站设计与实现(源码+...
介绍一个基于Spring Boot 3.0、Spring Cloud 2022 & Alibaba的微服务RBAC权限管理系统。该系统可以实现微服务RBAC权限管理,通过RBAC权限管理机制对用户访问系统的权限进行限制,从而提高系统的安全性和可用性。同时...
Spring Boot视频教程大合集,完美帮助你学习Spring Boot,内部有3套Spring Boot学习视频教程,另附一篇Security Oauth2.0认证授权视频教程
Pro Spring Boot is your authoritative hands-on practical guide for increasing your Spring Framework-based enterprise Java and cloud application productivity while decreasing development time using the...
java毕业设计——基于spring boot的理财管理网站设计与实现(源码+数据库).zip java毕业设计——基于spring boot的理财管理网站设计与实现(源码+数据库).zip java毕业设计——基于spring boot的理财管理网站设计与...