Skip to content

Commit

Permalink
[type:feat] support http2 server (#5125)
Browse files Browse the repository at this point in the history
* [type:feat] support http2 ssl

* [type:feat] support http2 ssl

* [type:feat] support http2 ssl

* [type:feat] support http2 ssl

* [type:feat] support http2 ssl

---------

Co-authored-by: xiaoyu <xiaoyu@apache.org>
  • Loading branch information
moremind and yu199195 committed Sep 8, 2023
1 parent 84068fc commit 38182ed
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ public class Ssl {
* Installs the netty InsecureTrustManagerFactory. This is insecure and not
* suitable for production.
*/
private boolean useInsecureTrustManager;
private boolean useInsecureTrustManager = Boolean.FALSE;

/**
* Trusted certificates for verifying the remote endpoint's certificate.
Expand Down Expand Up @@ -812,7 +812,7 @@ public class Ssl {
/**
* The default ssl configuration type. Defaults to JDK Provider.
*/
private SslProvider defaultConfigurationType = SslProvider.JDK;
private SslProvider defaultConfigurationType;

/**
* Is use insecure trust manager boolean.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
Expand Down Expand Up @@ -55,6 +56,7 @@ public void before() {
.withBean(ShenyuConfigurationTest.class)
.withBean(DefaultServerCodecConfigurer.class)
.withBean(DefaultErrorAttributes.class)
.withBean(ServerProperties.class)
.withPropertyValues(
"debug=true",
"shenyu.cross.enabled=true",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.plugin.httpclient.config.HttpClientProperties;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.PropertyMapper;
import reactor.netty.http.Http11SslContextSpec;
import reactor.netty.http.Http2SslContextSpec;
import reactor.netty.http.HttpProtocol;
import reactor.netty.http.client.HttpClient;
import reactor.netty.resources.ConnectionProvider;
import reactor.netty.resources.LoopResources;
import reactor.netty.tcp.DefaultSslContextSpec;
import reactor.netty.tcp.SslProvider;
import reactor.netty.transport.ProxyProvider;

Expand All @@ -44,10 +47,15 @@ public class HttpClientFactory extends AbstractFactoryBean<HttpClient> {
private final HttpClientProperties properties;

private final LoopResources loopResources;

private final ServerProperties serverProperties;

public HttpClientFactory(final HttpClientProperties httpClientProperties, final LoopResources loopResources) {
public HttpClientFactory(final HttpClientProperties httpClientProperties,
final LoopResources loopResources,
final ServerProperties serverProperties) {
this.properties = httpClientProperties;
this.loopResources = loopResources;
this.serverProperties = serverProperties;
}

@Override
Expand Down Expand Up @@ -78,18 +86,18 @@ private HttpClient setHttpClientProxy(final HttpClient httpClient, final HttpCli
}

private void setSsl(final SslProvider.SslContextSpec sslContextSpec, final HttpClientProperties.Ssl ssl) {
SslProvider.ProtocolSslContextSpec spec = DefaultSslContextSpec.forClient()
.configure(sslContextBuilder -> {
X509Certificate[] trustedX509Certificates = ssl.getTrustedX509CertificatesForTrustManager();
if (ArrayUtils.isNotEmpty(trustedX509Certificates)) {
sslContextBuilder.trustManager(trustedX509Certificates);
} else if (ssl.isUseInsecureTrustManager()) {
sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
}
sslContextBuilder.keyManager(ssl.getKeyManagerFactory());
sslContextBuilder.sslProvider(ssl.getDefaultConfigurationType());
});
sslContextSpec.sslContext(spec)
SslProvider.ProtocolSslContextSpec clientSslContext = (serverProperties.getHttp2().isEnabled())
? Http2SslContextSpec.forClient() : Http11SslContextSpec.forClient();
clientSslContext.configure(sslContextBuilder -> {
X509Certificate[] trustedX509Certificates = ssl.getTrustedX509CertificatesForTrustManager();
if (ArrayUtils.isNotEmpty(trustedX509Certificates)) {
sslContextBuilder.trustManager(trustedX509Certificates);
} else if (ssl.isUseInsecureTrustManager()) {
sslContextBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
}
sslContextBuilder.keyManager(ssl.getKeyManagerFactory());
});
sslContextSpec.sslContext(clientSslContext)
.handshakeTimeout(ssl.getHandshakeTimeout())
.closeNotifyFlushTimeout(ssl.getCloseNotifyFlushTimeout())
.closeNotifyReadTimeout(ssl.getCloseNotifyReadTimeout());
Expand All @@ -102,6 +110,9 @@ protected HttpClient createInstance() {
ConnectionProvider connectionProvider = buildConnectionProvider(pool);
HttpClient httpClient = HttpClient.create(connectionProvider)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, properties.getConnectTimeout());
if (serverProperties.getHttp2().isEnabled()) {
httpClient = httpClient.protocol(HttpProtocol.HTTP11, HttpProtocol.H2);
}
HttpClientProperties.Proxy proxy = properties.getProxy();
if (StringUtils.isNotEmpty(proxy.getHost())) {
httpClient = setHttpClientProxy(httpClient, proxy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -71,13 +72,15 @@ public LoopResources httpClientLoopResource(final HttpClientProperties propertie
*
* @param properties the properties
* @param provider the loop resources bean provider
* @param serverProperties the server properties
* @return the http client
*/
@Bean
@ConditionalOnMissingBean({HttpClient.class, HttpClientFactory.class})
public HttpClientFactory httpClient(final HttpClientProperties properties,
final ObjectProvider<LoopResources> provider) {
return new HttpClientFactory(properties, provider.getIfAvailable());
final ObjectProvider<LoopResources> provider,
final ServerProperties serverProperties) {
return new HttpClientFactory(properties, provider.getIfAvailable(), serverProperties);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

import java.time.Duration;

import io.netty.handler.ssl.SslProvider;
import org.apache.shenyu.plugin.api.ShenyuPlugin;
import org.apache.shenyu.plugin.httpclient.config.HttpClientProperties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Configuration;
Expand All @@ -49,7 +49,8 @@ public class HttpClientPluginConfigurationTest {
public void before() {
applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(HttpClientPluginConfiguration.class))
.withBean(HttpClientPluginConfigurationTest.class);
.withBean(HttpClientPluginConfigurationTest.class)
.withBean(ServerProperties.class);
}

@Test
Expand All @@ -71,8 +72,7 @@ public void testHttpClientProperties() {
"shenyu.httpclient.ssl.X509Certificate[]=[]",
"shenyu.httpclient.ssl.handshakeTimeout=10000",
"shenyu.httpclient.ssl.closeNotifyFlushTimeout=3000",
"shenyu.httpclient.ssl.closeNotifyReadTimeout=0",
"shenyu.httpclient.ssl.SslProvider.DefaultConfigurationType=1"
"shenyu.httpclient.ssl.closeNotifyReadTimeout=0"
)
.run(context -> {
HttpClientProperties properties = context.getBean("httpClientProperties", HttpClientProperties.class);
Expand All @@ -92,7 +92,6 @@ public void testHttpClientProperties() {
assertNotNull(properties.getSsl().getTrustedX509Certificates());
assertThat(properties.getSsl().getCloseNotifyFlushTimeout(), is(Duration.ofMillis(3000)));
assertThat(properties.getSsl().getCloseNotifyReadTimeout(), is(Duration.ZERO));
assertThat(properties.getSsl().getDefaultConfigurationType(), is(SslProvider.JDK));
});
}

Expand Down

0 comments on commit 38182ed

Please sign in to comment.