关于java:SpringBoot2X-同时支持http和https

一.创立springBoot我的项目

二.配置ssl证书

  1. 将证书放到我的项目的resources文件夹下,如下图所示
  2. 在application.yml 文件中做如下配置(如果你的我的项目是application.properties能够批改后缀名为yml或者能够参考yml写法做响应的批改)
server:
  port: 6443
  ssl:
    key-store: classpath:springboot.p12
    key-store-password: 123456
    keyStoreType: PKCS12
    enabled: true

http:
  port: 6880

三.创立Tomcat配置类

package com.example.demo.config;

import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Value;
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 TM-majian
 * @version 1.0
 * @since 2021/5/7 19:21
 */
@Configuration
public class SSLConfig {
    @Value("${http.port}")
    private Integer port;

    @Bean
    public ServletWebServerFactory servletContainer() {

        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(port);
        tomcat.addAdditionalTomcatConnectors(connector);
        return tomcat;
    }

}

四.创立controller测试

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author TM-majian
 * @version 1.0
 * @since 2021/5/7 19:19
 */
@RestController
public class TestController {
        @RequestMapping("/helloHttps")
        public String helloHttps(){
            return "hello https";
        }

}

五.启动我的项目测试

  1. 本地测试

    通过控制台日志能够看到两个端口都已启动实现,浏览器拜访http连贯http://localhost:6880/helloHttps

    拜访https连贯
    https://localhost:6443/helloH…

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理