Spring Boot如何配置SSL实现同时支持http和https协议(超简单)

简介: Spring Boot如何配置SSL实现同时支持http和https协议(超简单)

一、背景



最近想搞一个小程序玩玩,然后小程序是需要https协议的,于是乎,我在华为云上申请了一个免费证书服务,验证过后就可以申请证书,申请完成后下载,里面会有两个文件,一个是server.jks,一个是keyStorePass。接下来会有大用处。


二、properties配置文件



server:
  port: 8088
  ssl:
    key-store: classpath:cert/server.jks #密钥存储文件
    key-store-password: Er1$KOUZP$ut94Ct # keyStorePass文件种的值
    key-store-type: JKS # 密钥文件类型


三、tomcat启动配置



import org.springframework.context.annotation.Configuration;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.context.annotation.Bean;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
@Configuration
public class WebConfig {
    /**
     * http重定向到https
     * @return
     */
    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(org.apache.catalina.Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;
    }
    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //Connector监听的http的默认端口号
        //其中两个端口不能一样
        connector.setPort(8080);
        connector.setSecure(false);
        //监听到http的端口号后转向到的https的端口号,也就是项目配置的port
        connector.setRedirectPort(8088);
        return connector;
    }
}


三、测试结果



  1. 直接http://localhost:8088/doc.html访问

  2. 通过https访问https://localhost:8088.doc.html


  1. 640.png


  1. 通过http访问http://localhost:8080/doc.html


  1. 总结 测试2和测试3的结果是一致的,因为3只是做了一个重定向,重定向到2,所以最终显示结果是一样的。四、提示 如果仅仅是实现https,只需要做properties配置文件配置即可。


目录
相关文章
|
5天前
|
jenkins 应用服务中间件 持续交付
如何配置 Nginx 作为 Jenkins 的反向代理并启用 SSL 加密
如何配置 Nginx 作为 Jenkins 的反向代理并启用 SSL 加密
26 8
|
5天前
|
负载均衡 前端开发 应用服务中间件
使用Nginx配置SSL以及部署前端项目
本文介绍了如何使用Nginx配置SSL证书以启用HTTPS,并展示了如何通过Nginx部署前端项目,包括配置SSL证书、设置代理和负载均衡的示例。
23 2
|
23天前
|
Java Spring
spring restTemplate 进行http请求的工具类封装
spring restTemplate 进行http请求的工具类封装
37 3
|
21天前
|
应用服务中间件 网络安全 nginx
使用Nginx Proxy Manager配置Halo的反向代理和申请 SSL 证书
本文引导如何用Nginx Proxy Manager (NPM)配置Halo的反向代理与SSL证书。NPM简化了Nginx的配置流程,适合无Nginx基础的用户。安装NPM无需额外安装Nginx,避免端口冲突。通过`docker-compose.yaml`启动NPM服务,并映射必要的端口。配置Halo反向代理需登录NPM面板,添加代理主机,设置域名、转发IP等参数。NPM支持自动申请与续期SSL证书,确保网站安全访问。更多Halo安装细节,请参考[如何在Linux云服务器上通过Docker Compose部署安装Halo](https://zhangfeidezhu.com/?p=631).
73 0
使用Nginx Proxy Manager配置Halo的反向代理和申请 SSL 证书
|
28天前
|
安全 Java Spring
Spring问题之如何配置Bean的初始化方法和销毁方法
Spring问题之如何配置Bean的初始化方法和销毁方法
|
5天前
|
缓存 Ubuntu 应用服务中间件
如何在 Ubuntu 14.04 上配置 Varnish Cache 4.0 实现 SSL 终止
如何在 Ubuntu 14.04 上配置 Varnish Cache 4.0 实现 SSL 终止
12 0
|
5天前
|
存储 安全 Linux
如何在 CentOS VPS 上配置 vsftpd 使用 SSL/TLS
如何在 CentOS VPS 上配置 vsftpd 使用 SSL/TLS
10 0
|
16天前
|
应用服务中间件 Linux API
Linux 利用 Cloudflare API 配置 acme.sh 自动续签 SSL (Apache、Nginx适用)
安装acme.sh工具,命令为`curl https://get.acme.sh | sh -s email=你的邮箱`。接着配置Cloudflare API,创建并记录API令牌及Zone ID。最后通过`acme.sh --issue -d 你的域名 --dns dns_cf`签发SSL证书,对于Nginx可使用`acme.sh --install-cert`命令安装证书,并设置自动重载Nginx服务。
|
2月前
|
网络安全
宝塔配置ssl证书没有生效
宝塔配置ssl证书没有生效
63 4
|
2月前
|
前端开发 小程序 应用服务中间件
在服务器上正确配置域名https证书(ssl)及为什么不推荐使用宝塔申请免费ssl证书
在服务器上正确配置域名https证书(ssl)及为什么不推荐使用宝塔申请免费ssl证书
146 4