SpringBoot配置Https和Http访问,Tomcat配置Https访问

本文介绍了如何在SpringBoot项目中配置HTTPS和HTTP访问,包括在CMD中使用JDK生成证书,将证书放入resources目录,配置application.properties或application.yml,以及解决项目打包时因多个主类导致的错误。同时,文章提到了Tomcat配置HTTPS访问的步骤,通过修改server.xml实现8443端口的HTTPS访问,并在web.xml中添加配置以实现HTTP到HTTPS的自动跳转。
摘要由CSDN通过智能技术生成

为了满足客户的需求,
首先cmd进入jdk的bin目录

keytool -genkey -v -alias testKey -keyalg RSA -validity 3650 -keystore C:\jdk证书\test.keystore

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

生成好之后把生成好的文件复制到resources目录下
在这里插入图片描述
application.properties

#是否开启缓存,开发时可以设置为 false,默认为 true
spring.thymeleaf.cache=false
#检查模版是否存在,默认为 true
spring.thymeleaf.check-template=true
#检查模版位置是否存在,默认为 true
spring.thymeleaf.check-template-location=true
#模版文件编码
spring.thymeleaf.encoding=UTF-8
#模版文件位置
spring.thymeleaf.prefix=classpath:/templates/
#Content-Type配置
spring.thymeleaf.servlet.content-type=text/html
#模版文件后缀
spring.thymeleaf.suffix=.html

spring.devtools.restart.enabled=true


server.port=8888
# 配置ssl证书
server.ssl.protocol=TLS
server.ssl.key-store=classpath:server.keystore
server.ssl.key-store-password=123456
#server.ssl.key-alias=tomcat
server.ssl.key-store-type=JKS

application.yml

server:

  port:8443

  ssl:

    protocol: TLS

    key-store: classpath:server.keystore

    key-alias: tomcat

    enabled:true

    key-store-password: 123456

    key-store-type: JKS



作者:kitter2020
链接:https://www.jianshu.com/p/b13e86242fe9
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

之后运行项目,恭喜,你已经成功运行https
在这里插入图片描述
在同一个项目配置http访问
在这里插入图片描述
在这里插入图片描述

import org.apache.catalina.connector.Connector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class HttpReq {
   
    @Bean
    public ServletWebServerFactory servletContainer() {
   
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory(
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值