我是靠谱客的博主 飞快酒窝,最近开发中收集的这篇文章主要介绍NettyWebServer开启http端口并实现http自动跳转https,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.定义配置文件类
package com.emily.cloud.gateway.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.server.Shutdown;

/**
 * @program: EmilyGateway
 * @description: Netty服务器配置
 * @create: 2021/01/13
 */
@ConfigurationProperties(prefix = "server.http")
public class ServerProperties {
    /**
     * 是否开启http端口号
     */
    private boolean enable;
    /**
     * http端口号
     */
    private int port = 8080;
    /**
     * 关闭模式
     */
    private Shutdown shutdown = Shutdown.IMMEDIATE;
    /**
     * http请求是否自动跳转到https
     */
    private boolean httpToHttps = true;

    public boolean isEnable() {
        return enable;
    }

    public void setEnable(boolean enable) {
        this.enable = enable;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Shutdown getShutdown() {
        return shutdown;
    }

    public void setShutdown(Shutdown shutdown) {
        this.shutdown = shutdown;
    }

    public boolean isHttpToHttps() {
        return httpToHttps;
    }

    public void setHttpToHttps(boolean httpToHttps) {
        this.httpToHttps = httpToHttps;
    }
}

2.配置NettyWebServer服务配置类,实现独立端口及自动跳转https
package com.emily.cloud.gateway.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @program: EmilyGateway
 * @description: 开启http端口
 * @create: 2021/01/13
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ServerProperties.class)
@ConditionalOnProperty(prefix = "server.http", name = "enable", havingValue = "true", matchIfMissing = true)
public class NettyWebServerAutoConfiguration {
    @Autowired
    private HttpHandler httpHandler;
    @Autowired
    private ServerProperties serverProperties;

    private WebServer webServer;

    @PostConstruct
    public void start() {
        NettyReactiveWebServerFactory factory = new NettyReactiveWebServerFactory(serverProperties.getPort());
        //设置关机模式
        factory.setShutdown(serverProperties.getShutdown());
        if (serverProperties.isHttpToHttps()) {
            webServer = factory.getWebServer(((request, response) -> {
                URI uri = request.getURI();
                try {
                    response.getHeaders().setLocation(new URI(StringUtils.replace(uri.toString(), "http", "https")));
                } catch (URISyntaxException e) {
                    return Mono.error(e);
                }
                return response.setComplete();
            }));
        } else {
            factory.getWebServer(httpHandler);
        }
        webServer.start();
    }

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

}

将上述两个加入系统之中,然后启动就可以实现开启独立http端口及http自动跳转https的能力;

GitHub地址:https://github.com/mingyang66/EmilyGateway

最后

以上就是飞快酒窝为你收集整理的NettyWebServer开启http端口并实现http自动跳转https的全部内容,希望文章能够帮你解决NettyWebServer开启http端口并实现http自动跳转https所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部