1

我们如何使用 Netty 服务器为 Spring Boot 应用程序配置自定义 SSLContext?

从源代码中,我看到“reactor.ipc.netty.http.server.HttpServerOptions”是一些服务器启动选项,但我找不到配置它们的方法。

是否有任何处理程序可以通过它注入我们的自定义 SSLContext?

我正在寻找与此类似的东西(使用 ssl 的 Spring 5 WebClient),其中 WebClient 通过“reactor.ipc.netty.http.client.HttpClientOptions”配置了自定义 SSLContext。

4

1 回答 1

2

Netty 可以像 spring-boot 2 中的示例一样自定义。

  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
  import org.springframework.boot.web.server.ErrorPage;
  import org.springframework.boot.web.server.Ssl;
  import org.springframework.boot.web.server.WebServerFactoryCustomizer;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.annotation.ImportResource;

  /**
   * author : Mohammad Ghoreishi
   */
  @Configuration
  @ImportResource({"classpath:convert-iban-service.xml", "classpath:config-loader-context.xml", "classpath*:error-resolver.xml"})
  @EnableAutoConfiguration
  public class Application {

    public static void main(String[] args) throws Exception {
      SpringApplication.run(Application.class, args);
    }

    @Bean
    public WebServerFactoryCustomizer<NettyReactiveWebServerFactory> customizer(){
      return new WebServerFactoryCustomizer<NettyReactiveWebServerFactory>() {
        @Override
        public void customize(NettyReactiveWebServerFactory factory) {
          Ssl ssl = new Ssl();
          // Your SSL Cusomizations
          ssl.setEnabled(true);
          ssl.setKeyStore("/path/to/keystore/keystore.jks");
          ssl.setKeyAlias("alias");
          ssl.setKeyPassword("password");
          factory.setSsl(ssl);
          factory.addErrorPages(new ErrorPage("/errorPage"));
        }
      };
    }
  }
于 2018-10-06T07:46:11.783 回答