gateway 支持同时使用 https 和 http访问

java 复制代码
import org.apache.catalina.connector.Connector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Configuration
@ConditionalOnProperty(name = "server.ssl.enabled", havingValue = "true", matchIfMissing = true)
public class HttpsConfiguration {

    @Value("${server.http.port:7081}")
    private Integer httpPort;

    // 此处注释的代码在gateway 不生效,在其他模块是可以同时启动 https 和 http 访问的
    /*@Bean
    public TomcatServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        tomcat.addAdditionalTomcatConnectors(httpConnector()); // ???不起作用, 没有监听到 httpPort 端口
        return tomcat;
    }

    private Connector httpConnector() {
        Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setPort(httpPort);
        return connector;
    }*/

    @Autowired
    private HttpHandler httpHandler;

    private WebServer webServer;

    @PostConstruct
    public void start(){
        NettyReactiveWebServerFactory factory = new NettyReactiveWebServerFactory(httpPort);
        webServer = factory.getWebServer(httpHandler);
        webServer.start();
    }

    @PreDestroy
    public void stop(){
        webServer.stop();
    }
}

参考:https://blog.csdn.net/siqiangming/article/details/130283755

相关推荐
2501_915106321 天前
App HTTPS 抓包 工程化排查与工具组合实战
网络协议·ios·小程序·https·uni-app·php·iphone
00后程序员张1 天前
混淆 iOS 类名与变量名的实战指南,多工具组合把混淆做成工程能力(混淆 iOS 类名变量名/IPA 成品混淆Ipa/Guard CLI 实操)
android·ios·小程序·https·uni-app·iphone·webview
記億揺晃着的那天2 天前
解密 HTTPS:从握手到安全通信
网络协议·安全·https·ssl证书
夜郎king2 天前
UniHttp/Jsoup Https SSL证书验证失败:SunCertPathBuilderException解决方案详解
https·ssl·jsoup访问https·unihttp访问https
才聚PMP2 天前
关于开启NPDP项目2025年第二次续证工作的通知
网络协议·https·ssl
toooooop82 天前
Nginx 反向代理 HTTPS CDN 配置检查清单(避坑版)
运维·nginx·https·cdn
尽兴-2 天前
[特殊字符] 微前端部署实战:Nginx 配置 HTTPS 与 CORS 跨域解决方案(示例版)
前端·nginx·https·跨域·cors·chrom
2501_916008893 天前
HTTPS 请求抓包,从原理到落地排查的工程化指南(Charles / tcpdump / Wireshark / Sniffmaster)
ios·小程序·https·uni-app·wireshark·iphone·tcpdump
三口吃掉你3 天前
微服务之网关(Spring Cloud Gateway)
java·网关·微服务·gateway
尽兴-3 天前
macOS 系统下 Chrome 浏览器安装 HTTPS 证书完整指南
chrome·macos·https·证书·ssl·pem·crt