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

相关推荐
Mysticbinary2 小时前
BurpSuite 代理原理 和 证书钉扎检测技术
http·https·网络代理·代理·网络流量·websockets·证书钉扎
想睡hhh21 小时前
HTTPS协议——对于HTTP的协议的加密
http·https
司徒小夜2 天前
HTTP与HTTPS杂谈-HTTPS防御了什么
网络·http·https
一只游鱼2 天前
利用keytool实现https协议(生成自签名证书)
网络协议·http·https·keytool
吐个泡泡v2 天前
网络编程基础:一文搞懂 Socket、HTTP、HTTPS、TCP/IP、SSL 的关系
网络·网络协议·http·https·socket·ssl·tcp
2501_916008892 天前
uni-app iOS 文件管理与 itools 配合实战,多工具协作的完整流程
android·ios·小程序·https·uni-app·iphone·webview
2501_916007472 天前
uni-app iOS 文件调试常见问题与解决方案:结合 itools、克魔、iMazing 的实战经验
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 天前
uni-app 项目 iOS 上架踩坑经验总结 从证书到审核的避坑指南
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者82 天前
iOS 上架 uni-app 流程全解析,从打包到发布的完整实践
android·ios·小程序·https·uni-app·iphone·webview
2501_916008893 天前
iOS 抓包工具有哪些?全面盘点主流工具与功能对比分析
android·ios·小程序·https·uni-app·iphone·webview