当前位置: 首页 > wzjs >正文

浦口区城乡建设集团网站软文营销广告案例

浦口区城乡建设集团网站,软文营销广告案例,番禺做网站多少钱,网站建设预算表总结:如何在SpringBoot中使用https协议以及自签证书? 前提一:什么是http协议?前提二:什么是https协议?一生成自签证书二 将证书转换为PKCS12格式三 配置SpringBoot(1)修改配置文件&a…

总结:如何在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

http://www.dtcms.com/wzjs/277659.html

相关文章:

  • 企业网站做备案青岛seo全网营销
  • 网站设计规划的一般流程上海seo优化bwyseo
  • 看男女做那个真实视频网站如何刷关键词指数
  • 网站开发公司外包最佳磁力吧cili8
  • 网站建设有云端吗如何做营销推广
  • 江西建设信息网站自媒体135网站
  • 建一个国外的网站杭州哪家seo公司好
  • 上海网络推广公司网站设计网页
  • 1688是b2b还是b2cseo的五个步骤
  • php网站开发web实例新手20种引流推广方法
  • 做个人网站怎么做今日头条新闻大事件
  • 做it的要给赌场网站做维护吗微信推广软件
  • 深圳市做网站有哪些公司定制网站建设电话
  • 天津建设公司网站免费关键词优化排名软件
  • 有谁认识做微网站的广告营销的经典案例
  • 台州企业做网站深圳网站建设公司排名
  • 付费推广网站长沙百度网站优化
  • 十大高端网站定制设计无锡网站排名公司
  • 网页设计的基本原则郑州网站优化外包
  • 自己买台服务器做网站世界十大搜索引擎及地址
  • 做网站一个程序员够吗产品营销推广
  • app充值网站开发线下推广都有什么方式
  • 学校网络推广方案江北seo
  • 从哪里可以建公司网站google搜索网址
  • 网站的制作步骤包括兔子bt搜索
  • 网站建设的校内实习日志佛山企业用seo策略
  • icoc.cc是哪个网站域名制作公司官网多少钱
  • 交互网站模板湖南关键词优化首选
  • 微信开发小程序开发网站建设广州网站排名推广
  • 上海网站优化加盟信息流优化师招聘