Skip to content

Commit 53607ea

Browse files
committed
Merge pull request #19399 from bono007
* gh-19205: Polish "Add SSL support to RSocketServer" Add SSL support to RSocketServer Closes gh-19399
2 parents dd02404 + 0715750 commit 53607ea

File tree

7 files changed

+202
-28
lines changed

7 files changed

+202
-28
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketProperties.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,12 +19,15 @@
1919
import java.net.InetAddress;
2020

2121
import org.springframework.boot.context.properties.ConfigurationProperties;
22+
import org.springframework.boot.context.properties.NestedConfigurationProperty;
2223
import org.springframework.boot.rsocket.server.RSocketServer;
24+
import org.springframework.boot.web.server.Ssl;
2325

2426
/**
2527
* {@link ConfigurationProperties properties} for RSocket support.
2628
*
2729
* @author Brian Clozel
30+
* @author Chris Bono
2831
* @since 2.2.0
2932
*/
3033
@ConfigurationProperties("spring.rsocket")
@@ -59,6 +62,9 @@ public static class Server {
5962
*/
6063
private String mappingPath;
6164

65+
@NestedConfigurationProperty
66+
private Ssl ssl;
67+
6268
public Integer getPort() {
6369
return this.port;
6470
}
@@ -91,6 +97,14 @@ public void setMappingPath(String mappingPath) {
9197
this.mappingPath = mappingPath;
9298
}
9399

100+
public Ssl getSsl() {
101+
return this.ssl;
102+
}
103+
104+
public void setSsl(Ssl ssl) {
105+
this.ssl = ssl;
106+
}
107+
94108
}
95109

96110
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ RSocketServerFactory rSocketServerFactory(RSocketProperties properties, ReactorR
9797
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
9898
map.from(properties.getServer().getAddress()).to(factory::setAddress);
9999
map.from(properties.getServer().getPort()).to(factory::setPort);
100+
map.from(properties.getServer().getSsl()).to(factory::setSsl);
100101
factory.setRSocketServerCustomizers(customizers.orderedStream().collect(Collectors.toList()));
101102
return factory;
102103
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/rsocket/RSocketServerAutoConfigurationTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ void shouldSetLocalServerPortWhenRSocketServerPortIsSet() {
9191
});
9292
}
9393

94+
@Test
95+
void shouldUseSslWhenRocketServerSslIsConfigured() {
96+
reactiveWebContextRunner()
97+
.withPropertyValues("spring.rsocket.server.ssl.keyStore=classpath:rsocket/test.jks",
98+
"spring.rsocket.server.ssl.keyPassword=password", "spring.rsocket.server.port=0")
99+
.run((context) -> assertThat(context).hasSingleBean(RSocketServerFactory.class)
100+
.hasSingleBean(RSocketServerBootstrap.class).hasSingleBean(RSocketServerCustomizer.class)
101+
.getBean(RSocketServerFactory.class)
102+
.hasFieldOrPropertyWithValue("ssl.keyStore", "classpath:rsocket/test.jks")
103+
.hasFieldOrPropertyWithValue("ssl.keyPassword", "password"));
104+
}
105+
94106
@Test
95107
void shouldUseCustomServerBootstrap() {
96108
contextRunner().withUserConfiguration(CustomServerBootstrapConfig.class).run((context) -> assertThat(context)
1.25 KB
Binary file not shown.

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/netty/NettyRSocketServerFactory.java

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
import org.springframework.boot.rsocket.server.RSocketServer;
3838
import org.springframework.boot.rsocket.server.RSocketServerCustomizer;
3939
import org.springframework.boot.rsocket.server.RSocketServerFactory;
40+
import org.springframework.boot.web.embedded.netty.SslServerCustomizer;
41+
import org.springframework.boot.web.server.Ssl;
42+
import org.springframework.boot.web.server.SslStoreProvider;
4043
import org.springframework.http.client.reactive.ReactorResourceFactory;
4144
import org.springframework.util.Assert;
4245

@@ -45,6 +48,7 @@
4548
* by Netty.
4649
*
4750
* @author Brian Clozel
51+
* @author Chris Bono
4852
* @since 2.2.0
4953
*/
5054
public class NettyRSocketServerFactory implements RSocketServerFactory, ConfigurableRSocketServerFactory {
@@ -61,6 +65,10 @@ public class NettyRSocketServerFactory implements RSocketServerFactory, Configur
6165

6266
private List<RSocketServerCustomizer> rSocketServerCustomizers = new ArrayList<>();
6367

68+
private Ssl ssl;
69+
70+
private SslStoreProvider sslStoreProvider;
71+
6472
@Override
6573
public void setPort(int port) {
6674
this.port = port;
@@ -76,6 +84,16 @@ public void setTransport(RSocketServer.Transport transport) {
7684
this.transport = transport;
7785
}
7886

87+
@Override
88+
public void setSsl(Ssl ssl) {
89+
this.ssl = ssl;
90+
}
91+
92+
@Override
93+
public void setSslStoreProvider(SslStoreProvider sslStoreProvider) {
94+
this.sslStoreProvider = sslStoreProvider;
95+
}
96+
7997
/**
8098
* Set the {@link ReactorResourceFactory} to get the shared resources from.
8199
* @param resourceFactory the server resources
@@ -133,21 +151,27 @@ private ServerTransport<CloseableChannel> createTransport() {
133151
}
134152

135153
private ServerTransport<CloseableChannel> createWebSocketTransport() {
154+
HttpServer httpServer = HttpServer.create();
136155
if (this.resourceFactory != null) {
137-
HttpServer httpServer = HttpServer.create().runOn(this.resourceFactory.getLoopResources())
138-
.bindAddress(this::getListenAddress);
139-
return WebsocketServerTransport.create(httpServer);
156+
httpServer = httpServer.runOn(this.resourceFactory.getLoopResources());
157+
}
158+
if (this.ssl != null && this.ssl.isEnabled()) {
159+
SslServerCustomizer sslServerCustomizer = new SslServerCustomizer(this.ssl, null, this.sslStoreProvider);
160+
httpServer = sslServerCustomizer.apply(httpServer);
140161
}
141-
return WebsocketServerTransport.create(getListenAddress());
162+
return WebsocketServerTransport.create(httpServer.bindAddress(this::getListenAddress));
142163
}
143164

144165
private ServerTransport<CloseableChannel> createTcpTransport() {
166+
TcpServer tcpServer = TcpServer.create();
145167
if (this.resourceFactory != null) {
146-
TcpServer tcpServer = TcpServer.create().runOn(this.resourceFactory.getLoopResources())
147-
.bindAddress(this::getListenAddress);
148-
return TcpServerTransport.create(tcpServer);
168+
tcpServer = tcpServer.runOn(this.resourceFactory.getLoopResources());
149169
}
150-
return TcpServerTransport.create(getListenAddress());
170+
if (this.ssl != null && this.ssl.isEnabled()) {
171+
TcpSslServerCustomizer sslServerCustomizer = new TcpSslServerCustomizer(this.ssl, this.sslStoreProvider);
172+
tcpServer = sslServerCustomizer.apply(tcpServer);
173+
}
174+
return TcpServerTransport.create(tcpServer.bindAddress(this::getListenAddress));
151175
}
152176

153177
private InetSocketAddress getListenAddress() {
@@ -157,4 +181,21 @@ private InetSocketAddress getListenAddress() {
157181
return new InetSocketAddress(this.port);
158182
}
159183

184+
private static final class TcpSslServerCustomizer extends SslServerCustomizer {
185+
186+
private TcpSslServerCustomizer(Ssl ssl, SslStoreProvider sslStoreProvider) {
187+
super(ssl, null, sslStoreProvider);
188+
}
189+
190+
private TcpServer apply(TcpServer server) {
191+
try {
192+
return server.secure((contextSpec) -> contextSpec.sslContext(getContextBuilder()));
193+
}
194+
catch (Exception ex) {
195+
throw new IllegalStateException(ex);
196+
}
197+
}
198+
199+
}
200+
160201
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/server/ConfigurableRSocketServerFactory.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@
1818

1919
import java.net.InetAddress;
2020

21+
import org.springframework.boot.web.server.Ssl;
22+
import org.springframework.boot.web.server.SslStoreProvider;
23+
2124
/**
2225
* A configurable {@link RSocketServerFactory}.
2326
*
@@ -45,4 +48,16 @@ public interface ConfigurableRSocketServerFactory {
4548
*/
4649
void setTransport(RSocketServer.Transport transport);
4750

51+
/**
52+
* Sets the SSL configuration that will be applied to the server's default connector.
53+
* @param ssl the SSL configuration
54+
*/
55+
void setSsl(Ssl ssl);
56+
57+
/**
58+
* Sets a provider that will be used to obtain SSL stores.
59+
* @param sslStoreProvider the SSL store provider
60+
*/
61+
void setSslStoreProvider(SslStoreProvider sslStoreProvider);
62+
4863
}

0 commit comments

Comments
 (0)