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

网站后台登录代码百度指数有什么参考意义

网站后台登录代码,百度指数有什么参考意义,深圳做网站建设的公司,提高网站收录的方法总结:如何在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.dt0577.cn/news/34023.html

相关文章:

  • 有没有给别人做图赚钱的网站汕头seo推广优化
  • 做移动网站优化优2024年新闻摘抄十条
  • wordpress 微服务seo网络营销推广公司深圳
  • 网站源码论坛海外网站推广的公司
  • 标准化信息网站建设与应用想做网络推广如何去做
  • 做网站的人 优帮云开发一个网站需要多少钱
  • 十大网红电商郑州seo排名优化公司
  • 闵行网站建设查网站是否正规
  • 广告代运营公司百度seo
  • 微网站模板 php网站查询入口
  • 网站qq聊天代码seo服务方案
  • 登录门户网站平面设计培训班学费一般多少
  • 建设网站 编程思路seo关键词排名优化
  • 英文网站模板制作北京seo关键词排名优化软件
  • 东莞常平嘉盛学校seo排名哪家正规
  • 揭阳网站建设网站天津seo标准
  • 网站建设平台流程韶关今日头条新闻
  • 网站备案提交资料1000个关键词
  • 织梦网站排版能调整吗引流推广平台
  • 萝岗区营销型网站建设网站建设的数字化和互联网化
  • 西安建设厅网站新手如何自己做网站
  • 怎样做网站的关键词seo怎么优化武汉厂商
  • ps网页排版设计网站seo排名优化价格
  • 佛山顺德做网站百度收录怎么做
  • 公司网站的seo怎么做网站平台有哪些
  • django网站开发实例源码烘焙甜点培训学校
  • 建网站商城广州seo公司
  • 做网站公司需要多少钱电子商务网站建设论文
  • 求个网站你会感谢我的网络营销的主要工作有哪些
  • 网站开发的职业规划江苏seo网络