Springboot设置Https

1、修改配置文件application.yml,并将*.jks放到resource目录下。

bash 复制代码
server:
  port: 8080
  ssl:
    key-store: classpath:*.jks
    key-store-password: *
    key-store-type: JKS
    enabled: true
    key-alias: boe.com.cn

2、添加http转https的配置

java 复制代码
@Configuration
public class TomcatConfig {
    @Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint constraint = new SecurityConstraint();
                constraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                constraint.addCollection(collection);
                context.addConstraint(constraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(httpConnector());
        return tomcat;
    }

    @Bean
    public Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        //Connector监听的http的端口号
        connector.setPort(8082);
        connector.setSecure(false);
        //监听到http的端口号后转向到的https的端口号
        connector.setRedirectPort(8080);
        return connector;
    }
}

3、修改pom.xml文件

一般到上一步,本地运行没有什么问题,但是打包会有问题。

xml 复制代码
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <nonFilteredFileExtensions>
            <!-- 过滤后缀为jks的证书文件 -->
            <nonFilteredFileExtension>jks</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>

此时打包应该没有问题,但是部署时可能会出现端口问题,如果出现就添加一下代码

xml 复制代码
<resource>
  <directory>src/main/resources</directory>
  <filtering>true</filtering>
  <excludes>
      <!-- 替换成自己的证书文件 -->
      <exclude>*.jks</exclude>
  </excludes>
</resource>
<resource>
  <directory>src/main/resources</directory>
  <filtering>false</filtering>
  <includes>
      <!-- 替换成自己的证书文件 -->
      <include>*.jks</include>
  </includes>
</resource>

另外,如果还有问题,就很可能是端口问题,8090这个端口配置一直不成功,换成8080就好了。

相关推荐
阿虎儿3 分钟前
Linux 下为局域网 IP(如 192.168.1.100)打造不受浏览器警告的自签名 SSL/TLS 证书
linux·安全·https
天丁o13 分钟前
Spring Boot + MyBatis Plus 考勤日报统计报表:打卡记录聚合、异常分类和明细下钻 Demo
spring boot·mybatis plus·企业数字化·考勤系统·报表统计
Devin~Y42 分钟前
从本地生活电商到 AI RAG:互联网大厂 Java 面试场景完整实战
java·spring boot·redis·elasticsearch·spring cloud·kafka·rag
米码收割机43 分钟前
SA508-3钢回火焊道焊接温度场数值模拟(模拟图+论文)
spring boot·express·宠物
JackSparrow4141 小时前
前端安全之JS混淆+请求加密+请求签名以提升爬虫难度
前端·javascript·后端·爬虫·python·安全
geovindu1 小时前
go:loghelper
开发语言·后端·golang
小满zs2 小时前
Go语言第四章(类型转换)
后端·go
米码收割机2 小时前
【项目】spring boot+vue3 宠物领养系统(源码+文档)【独一无二】
java·spring boot·宠物
人间凡尔赛3 小时前
告别冷启动!WebAssembly + Spin 实战:Serverless 延迟从 1 秒降到 1 毫秒
后端·云原生·serverless·webassembly·spin
陈随易10 小时前
bm2,MoonBit实现的pm2替代品
前端·后端·程序员