欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > 总结:如何在SpringBoot中使用https协议以及自签证书?

总结:如何在SpringBoot中使用https协议以及自签证书?

2025/5/11 18:37:17 来源:https://blog.csdn.net/weixin_48033662/article/details/145639616  浏览:    关键词:总结:如何在SpringBoot中使用https协议以及自签证书?

总结:如何在SpringBoot中使用https协议以及自签证书?

  • 前提一:什么是http协议?
  • 前提二:什么是https协议?
  • 一·生成自签证书
  • 二· 将证书转换为PKCS12格式
  • 三· 配置SpringBoot
    • (1)修改配置文件:application.properties或application.yml
    • (2) 配置HTTP重定向到HTTPS(可选)
    • (3)控制器示例代码:
    • (4)完整项目结构
  • 四·验证成功
    • (1)启动Spring Boot应用:
    • (2)处理浏览器警告
    • (3)使用https协议访问:https://localhost:8443/hello(浏览器会提示证书不安全,点击“继续”即可)。
    • (4)使用http协议访问:http://localhost:8080/hello,会自动跳转到HTTPS(如果配置了HTTP重定向)
    • (5)使用Java代码发起https请求验证:成功!
    • (6)https工具类代码地址:

前提一:什么是http协议?

https://t4dmw.blog.csdn.net/article/details/123313047

前提二:什么是https协议?

https://t4dmw.blog.csdn.net/article/details/123385750

一·生成自签证书

使用OpenSSL生成自签证书(如果已有证书可跳过此步骤):

openssl req -x509 -newkey rsa:4096 -keyout private_key.pem -out public_cert.pem -days 365 -nodes

生成的文件:

private_key.pem:私钥文件。

public_cert.pem:证书文件,包含公钥。

在这里插入图片描述
在这里插入图片描述

二· 将证书转换为PKCS12格式

Spring Boot需要PKCS12格式的证书文件(.p12或.pfx)。使用以下命令转换:

openssl pkcs12 -export -in public_cert.pem -inkey private_key.pem -out keystore.p12 -name "mycert"

输入密码:设置证书库密码(如123456)生成keystore.p12文件。
在这里插入图片描述

三· 配置SpringBoot

将证书文件(keystore.p12)复制到Spring Boot项目的src/main/resources目录下。

(1)修改配置文件:application.properties或application.yml

server:ssl:# 开启sslenabled: true# keystore文件key-store: classpath:keystore.p12# keystore格式key-store-type: PKCS12# keystore密码key-store-password: 123456# keystore别名key-alias: mycert# https端口port: 8443

(2) 配置HTTP重定向到HTTPS(可选)

如果希望所有HTTP请求自动重定向到HTTPS,可以添加以下配置,HTTP端口在代码中设置为8080

(2-1) 添加Tomcat配置类

package com.example.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.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @author liumingfu* @date 2020/06/08 22:06* @description: https配置*/
@Configuration
public class HttpsConfig {@Beanpublic ServletWebServerFactory servletContainer() {TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {@Overrideprotected void postProcessContext(Context context) {SecurityConstraint securityConstraint = new SecurityConstraint();securityConstraint.setUserConstraint("CONFIDENTIAL");SecurityCollection collection = new SecurityCollection();collection.addPattern("/*");securityConstraint.addCollection(collection);context.addConstraint(securityConstraint);}};// 添加HTTP连接器,重定向到HTTPStomcat.addAdditionalTomcatConnectors(redirectConnector());return tomcat;}/*** 访问http协议8080端口,重定向到https协议8443端口** @param* @return* @author LiuMingFu* @date 2025/2/14*/private Connector redirectConnector() {Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");connector.setScheme("http");connector.setPort(8080);  // HTTP端口connector.setSecure(false);connector.setRedirectPort(8443);  // HTTPS端口return connector;}
}

(3)控制器示例代码:

控制层

package com.example.controller;import com.example.service.MyService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** @Description TODO* @Author LiuMingFu* @Date 2025/2/13 17:58*/
@RestController
public class MyController {@Resourceprivate MyService myService;@RequestMapping("/hello")public String getHello(){return myService.getHello();}
}

service接口层

package com.example.service;import org.springframework.stereotype.Service;/*** @Description TODO* @Author LiuMingFu* @Date 2025/2/13 18:00*/
@Service
public interface MyService {String getHello();
}

service接口实现层

package com.example.service.impl;import com.example.service.MyService;
import org.springframework.stereotype.Repository;/*** @Description TODO* @Author LiuMingFu* @Date 2025/2/13 18:00*/
@Repository
public class MyServiceImpl implements MyService {@Overridepublic String getHello() {return "hello https";}
}

(4)完整项目结构

在这里插入图片描述

四·验证成功

(1)启动Spring Boot应用:

在这里插入图片描述

在这里插入图片描述

(2)处理浏览器警告

  • 自签证书会导致浏览器显示“不安全”警告。在开发环境中,可以手动信任证书:
  • 将public_cert.pem导入浏览器或操作系统的信任库。
  • 对于测试工具(如Postman或curl),添加-k或–insecure参数忽略证书验证。

(3)使用https协议访问:https://localhost:8443/hello(浏览器会提示证书不安全,点击“继续”即可)。

在这里插入图片描述

(4)使用http协议访问:http://localhost:8080/hello,会自动跳转到HTTPS(如果配置了HTTP重定向)

在这里插入图片描述
在这里插入图片描述

(5)使用Java代码发起https请求验证:成功!

注意:使用代码方式,访问的接口域名,必须跟证书的域名地址一致,否则报错
在这里插入图片描述

(6)https工具类代码地址:

https://blog.csdn.net/weixin_48033662/article/details/145640901

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词