SpringBoot 启用 HTTPS 全流程

这篇具有很好参考价值的文章主要介绍了SpringBoot 启用 HTTPS 全流程。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

配置证书

切换到 jdk 的 bin 目录下,输入命令:

keytool -genkey -alias michaelSpica -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore ebininfosoft-ssl-key.p12 -validity 3650

springboot 开启https,Java,spring boot,https,java

证书会下载到 jdk 的 bin 目录下:

springboot 开启https,Java,spring boot,https,java

放入 SpringBoot 的资源目录,配置 application.properties

# 证书的路径,可用绝对路径,如果放到项目资源文件路径需要添加 classpath:
server.ssl.key-store=classpath:ebininfosoft-ssl-key.p12
# 证书的密码
server.ssl.key-store-password=w50029804
# 证书的类型
server.ssl.key-store-type=PKCS12

springboot 开启https,Java,spring boot,https,java

pom.xml 把这个文件加入编译,如果不加会报错 Could not load key store 'classpath:ebininfosoft-ssl-key.p12'

springboot 开启https,Java,spring boot,https,java

刷新 Maven,clean 后重新 compile。

此时若用 http 访问,则会提示:

springboot 开启https,Java,spring boot,https,java

http 重定向至 https

若要把对 http 端口访问的全部重定向到 https,则需配置转换器,例如将 9002 端口访问都定向到 9000:

package com.huawei.oss.telcloudsimulationuiserver.config;
import org.apache.catalina.Context;
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.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpConnectorConfig {
    /**
     * 获取Http连接器
     * @return Connector
     */
    public Connector getHttpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http"); // 使用http协议
        connector.setSecure(false); // 非安全传输
        connector.setPort(9002); // HTTP监听端口
        connector.setRedirectPort(9000); // 重定向端口
        return connector;
    }
    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
        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(getHttpConnector()); // 添加连接器
        return tomcat;
    }
}

同时启用 http 和 https

如果要在两个端口分别启用 http 和 https,则可做以下配置,例如将 9002 端口作为 http 访问端口:

package com.huawei.oss.telcloudsimulationuiserver.config;
import org.apache.catalina.connector.Connector;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpConnectorConfig {
    private int httpPort = 9002;
    @Bean
    public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(createStandardConnector());
        return tomcat;
    }
    private Connector createStandardConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(httpPort);
        return connector;
    }
}

springboot 开启https,Java,spring boot,https,java文章来源地址https://www.toymoban.com/news/detail-519526.html

到了这里,关于SpringBoot 启用 HTTPS 全流程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • Docker部署SpringBoot +Vue项目流程详解(含域名 + HTTPS)

    Docker部署SpringBoot +Vue项目流程详解(含域名 + HTTPS)

    前言 本次整体部署操作使用 阿里云 服务器,这里我选择的是 香港地区 的 2核2G ECS (可以省略域名备案操作)。 涉及到的中间件如下: Nginx MySQL Redis 后端项目 前端项目 1、选购服务器 首先登录到阿里云的官网,选购一台公网服务器,由于本次我部署项目体量比较小,所以

    2024年02月08日
    浏览(28)
  • Springboot设置Https

    1、修改配置文件application.yml,并将*.jks放到resource目录下。 2、添加http转https的配置 3、修改pom.xml文件 一般到上一步,本地运行没有什么问题,但是打包会有问题。 此时打包应该没有问题,但是部署时可能会出现端口问题,如果出现就添加一下代码 另外,如果还有问题,就很

    2024年02月14日
    浏览(5)
  • 详细SpringBoot框架教程——SpringBoot配置SSL(https)

    详细SpringBoot框架教程——SpringBoot配置SSL(https)

    本篇文章主要内容 SpringBoot配置SSL(https) SpringBoot全局异常处理 SpringBoot 404页面处理 SpringBoot可以通过在application.properties或application.yml配置文件中配置各种server.ssl.*属性来声明性使用SSL(https),比如下面的例子在application.properties中设置SSL属性: 如果使用了上面的配置就表示

    2023年04月09日
    浏览(12)
  • SpringBoot中配置Https入门

    SpringBoot中配置Https入门

    一、生成一个https证书 我们使用Java自带的JDK管理工具keytool来生成一个免费的https证书,在我们的Java安装目录下,在bin目录下我们使用cmd启动命令行窗口,执行如下命令生成一个https证书。 genkey表示要创建一个新的密钥 alias表示keystore的别名  keyalg表示使用的加密算法是RSA key

    2024年02月16日
    浏览(6)
  • 配置https ssl elasticsearch,springboot项目中连接elasticsearch https

    配置https ssl elasticsearch,springboot项目中连接elasticsearch https

    参考之前的文章 创建self-signed证书 下面展示一些 内联代码片 。 启动springboot项目应该可以连接上elasticsearch了。

    2024年02月11日
    浏览(30)
  • SpringBoot配置https(SSL证书)

    SpringBoot配置https(SSL证书)

    第一步: 首先得明白什么是wss协议: 可以看这篇文章:WSS、SSL 和 https 之间的关系 第二步: 先拿到ssl证书:我这边是用的阿里云的免费证书具体获取方法如下: 可以参考:阿里云申请免费 SSL证书 https 的图文教程_林中明月间丶-CSDN博客_阿里云申请免费ssl证书 将下载的证书压缩包解

    2024年02月11日
    浏览(16)
  • SpringBoot项目(Tomcat启动https端口)——springboot配置Tomcat两个端口,https和http的方式 & jar的打包和运行

    SpringBoot项目(Tomcat启动https端口)——springboot配置Tomcat两个端口,https和http的方式 & jar的打包和运行

    1.springboot配置Tomcat两个端口,https和http的方式; 2.在https协议下,发送axios请求没反应,暂时用form表单解决; 3.运行jar包template might not exist报错及解决; 代码位置: https://gitcode.net/Pireley/springboot-tomcat-http-https 严格来说https不是一个独立协议,只是在http协议基础上增加了SSL/T

    2024年02月03日
    浏览(14)
  • SpringBoot将http转换成https

    SpringBoot将http转换成https

    首先看项目,如果有nginx代理,则只需要改nginx配置即可。 如果没有nginx代理,则直接在springboot的resource下增加一个p12文件或者jks文件。 这里用p12来进行演示: 首先要获得ssl证书,命令查看是否有openssl 这里我的版本是3,会有一个bug,当生成p12文件运行时会报错:ObjectIdentifi

    2024年02月01日
    浏览(10)
  • elasticsearch使用SpringBoot 连接Https问题

    elasticsearch使用SpringBoot 连接Https问题

    SpringBoot整合elasticsearch初探:  导入相关的jar包。然后在配置文件中配置es的相关属性: 然后新建一个客户端工具类,将与es交互的所有基础操作写进工具类中,工具类注入连接es的客户端:  client直接注入就好,因为SpringBoot已经将客户端自动注册到Spring容器中了。 客户端尝

    2024年02月14日
    浏览(9)
  • SpringBoot 使用RestTemplate来调用https接口

    使用RestTemplate来访问第三方https接口,接口类型为POST,返回数据为JSON,需要忽略https证书,因此对RestTemplate 进行配置,通过HttpClient的请求工厂(HttpComponentsClientHttpRequestFactory)进行构建。HttpComponentsClientHttpRequestFactory使用一个信任所有站点的HttpClient就可以解决问题了。 中间

    2024年02月13日
    浏览(11)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包