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

相关推荐
开着拖拉机回家12 小时前
【Ambari】使用 Knox 进行 LDAP 身份认证
大数据·hadoop·gateway·ambari·ldap·knox
୧⍢⃝୨ LonelyCoder1 天前
FreePBX修改IP地址和端口以及添加SSL证书开启HTTPS访问
tcp/ip·https·ssl
yang_shengy2 天前
【JavaEE】网络(6)
服务器·网络·http·https
OkeyProxy2 天前
HTTP、HTTPS和SOCKS5代理協議
网络协议·https·云计算·代理服务器·海外ip代理
m0_748232922 天前
Android Https和WebView
android·网络协议·https
BothSavage2 天前
Knife4j在Gateway下的URI优化以及热刷新
windows·gateway
ZachOn1y3 天前
计算机网络:应用层 —— 应用层概述
计算机网络·http·https·应用层·dns
壹佰大多3 天前
【spring-cloud-gateway总结】
java·spring·gateway
网安墨雨3 天前
iOS应用网络安全之HTTPS
web安全·ios·https
龙哥·三年风水3 天前
workman服务端开发模式-应用开发-后端api推送修改二
分布式·gateway·php