From 91233dd1818f76cac1e1722bcd9a3470af050f72 Mon Sep 17 00:00:00 2001 From: Andrey Litvitski Date: Tue, 1 Jul 2025 16:22:27 +0300 Subject: [PATCH 1/3] add ability to filter out services from being bound to server factories (#207) Signed-off-by: Andrey Litvitski --- .../grpc/server/DefaultGrpcServerFactory.java | 10 ++++- .../grpc/server/NettyGrpcServerFactory.java | 4 +- .../server/ServerServiceDefinitionFilter.java | 38 ++++++++++++++++ .../server/ShadedNettyGrpcServerFactory.java | 4 +- .../GrpcServerFactoryConfigurations.java | 13 ++++-- .../GrpcServerAutoConfigurationTests.java | 45 +++++++++++++++++++ 6 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 spring-grpc-core/src/main/java/org/springframework/grpc/server/ServerServiceDefinitionFilter.java diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java index 3b4646be..4e96cb8a 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java @@ -68,17 +68,21 @@ public class DefaultGrpcServerFactory> implements Grp private ClientAuth clientAuth; + private ServerServiceDefinitionFilter serviceFilter; + public DefaultGrpcServerFactory(String address, List> serverBuilderCustomizers) { this.address = address; this.serverBuilderCustomizers = Objects.requireNonNull(serverBuilderCustomizers, "serverBuilderCustomizers"); } public DefaultGrpcServerFactory(String address, List> serverBuilderCustomizers, - KeyManagerFactory keyManager, TrustManagerFactory trustManager, ClientAuth clientAuth) { + KeyManagerFactory keyManager, TrustManagerFactory trustManager, ClientAuth clientAuth, + ServerServiceDefinitionFilter serviceFilter) { this(address, serverBuilderCustomizers); this.keyManager = keyManager; this.trustManager = trustManager; this.clientAuth = clientAuth; + this.serviceFilter = serviceFilter; } protected String address() { @@ -94,7 +98,9 @@ public Server createServer() { @Override public void addService(ServerServiceDefinition service) { - this.serviceList.add(service); + if (this.serviceFilter == null || this.serviceFilter.filter(service, this)) { + this.serviceList.add(service); + } } /** diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java index 2e6c9c1f..f02b7c00 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java @@ -37,8 +37,8 @@ public class NettyGrpcServerFactory extends DefaultGrpcServerFactory> serverBuilderCustomizers, KeyManagerFactory keyManager, - TrustManagerFactory trustManager, ClientAuth clientAuth) { - super(address, serverBuilderCustomizers, keyManager, trustManager, clientAuth); + TrustManagerFactory trustManager, ClientAuth clientAuth, ServerServiceDefinitionFilter serviceFilter) { + super(address, serverBuilderCustomizers, keyManager, trustManager, clientAuth, serviceFilter); } @Override diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/ServerServiceDefinitionFilter.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/ServerServiceDefinitionFilter.java new file mode 100644 index 00000000..afeeb4e6 --- /dev/null +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/ServerServiceDefinitionFilter.java @@ -0,0 +1,38 @@ +/* + * Copyright 2023-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.grpc.server; + +import io.grpc.ServerServiceDefinition; + +/** + * Strategy to determine whether a {@link ServerServiceDefinition} should be included for + * a given {@link GrpcServerFactory server factory}. + * + * @author Andrey Litvitski + */ +@FunctionalInterface +public interface ServerServiceDefinitionFilter { + + /** + * Determine whether the given {@link ServerServiceDefinition} should be included for + * the provided {@link GrpcServerFactory server factory}. + * @param serviceDefinition the gRPC service definition under consideration. + * @param serverFactory the server factory in use. + * @return {@code true} if the service should be included; {@code false} otherwise. + */ + boolean filter(ServerServiceDefinition serviceDefinition, GrpcServerFactory serverFactory); + +} diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java index 3ad992a8..4700d4c6 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java @@ -37,8 +37,8 @@ public class ShadedNettyGrpcServerFactory extends DefaultGrpcServerFactory> serverBuilderCustomizers, KeyManagerFactory keyManager, - TrustManagerFactory trustManager, ClientAuth clientAuth) { - super(address, serverBuilderCustomizers, keyManager, trustManager, clientAuth); + TrustManagerFactory trustManager, ClientAuth clientAuth, ServerServiceDefinitionFilter serviceFilter) { + super(address, serverBuilderCustomizers, keyManager, trustManager, clientAuth, serviceFilter); } @Override diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java index edebcad5..edb5fd92 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java @@ -17,6 +17,7 @@ package org.springframework.grpc.autoconfigure.server; import java.util.List; +import java.util.Optional; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.TrustManagerFactory; @@ -35,6 +36,7 @@ import org.springframework.grpc.server.InProcessGrpcServerFactory; import org.springframework.grpc.server.NettyGrpcServerFactory; import org.springframework.grpc.server.ServerBuilderCustomizer; +import org.springframework.grpc.server.ServerServiceDefinitionFilter; import org.springframework.grpc.server.ShadedNettyGrpcServerFactory; import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; import org.springframework.grpc.server.service.GrpcServiceConfigurer; @@ -62,7 +64,8 @@ static class ShadedNettyServerFactoryConfiguration { @Bean ShadedNettyGrpcServerFactory shadedNettyGrpcServerFactory(GrpcServerProperties properties, GrpcServiceDiscoverer serviceDiscoverer, GrpcServiceConfigurer serviceConfigurer, - ServerBuilderCustomizers serverBuilderCustomizers, SslBundles bundles) { + ServerBuilderCustomizers serverBuilderCustomizers, SslBundles bundles, + Optional serviceFilter) { ShadedNettyServerFactoryPropertyMapper mapper = new ShadedNettyServerFactoryPropertyMapper(properties); List> builderCustomizers = List .of(mapper::customizeServerBuilder, serverBuilderCustomizers::customize); @@ -75,7 +78,8 @@ ShadedNettyGrpcServerFactory shadedNettyGrpcServerFactory(GrpcServerProperties p : io.grpc.netty.shaded.io.netty.handler.ssl.util.InsecureTrustManagerFactory.INSTANCE; } ShadedNettyGrpcServerFactory factory = new ShadedNettyGrpcServerFactory(properties.getAddress(), - builderCustomizers, keyManager, trustManager, properties.getSsl().getClientAuth()); + builderCustomizers, keyManager, trustManager, properties.getSsl().getClientAuth(), + serviceFilter.orElse(null)); serviceDiscoverer.findServices().stream().map(serviceConfigurer::configure).forEach(factory::addService); return factory; } @@ -101,7 +105,8 @@ static class NettyServerFactoryConfiguration { @Bean NettyGrpcServerFactory nettyGrpcServerFactory(GrpcServerProperties properties, GrpcServiceDiscoverer serviceDiscoverer, GrpcServiceConfigurer serviceConfigurer, - ServerBuilderCustomizers serverBuilderCustomizers, SslBundles bundles) { + ServerBuilderCustomizers serverBuilderCustomizers, SslBundles bundles, + Optional serviceFilter) { NettyServerFactoryPropertyMapper mapper = new NettyServerFactoryPropertyMapper(properties); List> builderCustomizers = List .of(mapper::customizeServerBuilder, serverBuilderCustomizers::customize); @@ -114,7 +119,7 @@ NettyGrpcServerFactory nettyGrpcServerFactory(GrpcServerProperties properties, : InsecureTrustManagerFactory.INSTANCE; } NettyGrpcServerFactory factory = new NettyGrpcServerFactory(properties.getAddress(), builderCustomizers, - keyManager, trustManager, properties.getSsl().getClientAuth()); + keyManager, trustManager, properties.getSsl().getClientAuth(), serviceFilter.orElse(null)); serviceDiscoverer.findServices().stream().map(serviceConfigurer::configure).forEach(factory::addService); return factory; } diff --git a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java index 7249654c..2e329c6f 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java +++ b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java @@ -29,6 +29,7 @@ import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.mockito.InOrder; import org.mockito.MockedStatic; @@ -44,10 +45,12 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; +import org.springframework.grpc.server.DefaultGrpcServerFactory; import org.springframework.grpc.server.GrpcServerFactory; import org.springframework.grpc.server.InProcessGrpcServerFactory; import org.springframework.grpc.server.NettyGrpcServerFactory; import org.springframework.grpc.server.ServerBuilderCustomizer; +import org.springframework.grpc.server.ServerServiceDefinitionFilter; import org.springframework.grpc.server.ShadedNettyGrpcServerFactory; import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; import org.springframework.grpc.server.service.DefaultGrpcServiceConfigurer; @@ -66,6 +69,7 @@ * Tests for {@link GrpcServerAutoConfiguration}. * * @author Chris Bono + * @author Andrey Litvitski */ class GrpcServerAutoConfigurationTests { @@ -407,6 +411,47 @@ void nettyServerFactoryAutoConfiguredWithSsl() { NettyGrpcServerFactory.class, "myhost:6160", "nettyGrpcServerLifecycle"); } + @Nested + class WithFilter { + + @Test + void whenFilterExcludesOneServiceThenServerListGetAllowedOnes() { + ServerServiceDefinition serviceDefinitionA = mock(); + GrpcServerAutoConfigurationTests.this.contextRunner() + .withBean(ServerServiceDefinitionFilter.class, + () -> (serviceDefinition, ___) -> serviceDefinition.equals(serviceDefinitionA)) + .run((context) -> { + DefaultGrpcServerFactory defaultGrpcServerFactory = (DefaultGrpcServerFactory) context + .getBean(GrpcServerFactory.class); + ServerServiceDefinition serviceDefinitionB = mock(); + defaultGrpcServerFactory.addService(serviceDefinitionA); + defaultGrpcServerFactory.addService(serviceDefinitionB); + assertThat(context).getBean(GrpcServerFactory.class) + .extracting("serviceList", InstanceOfAssertFactories.list(ServerServiceDefinition.class)) + .contains(serviceDefinitionA) + .doesNotContain(serviceDefinitionB); + }); + } + + @Test + void whenFilterIncludesAllServicesThenServerListGetAllowedOnes() { + GrpcServerAutoConfigurationTests.this.contextRunner() + .withBean(ServerServiceDefinitionFilter.class, () -> (__, ___) -> true) + .run((context) -> { + DefaultGrpcServerFactory defaultGrpcServerFactory = (DefaultGrpcServerFactory) context + .getBean(GrpcServerFactory.class); + ServerServiceDefinition serviceDefinitionA = mock(); + ServerServiceDefinition serviceDefinitionB = mock(); + defaultGrpcServerFactory.addService(serviceDefinitionA); + defaultGrpcServerFactory.addService(serviceDefinitionB); + assertThat(context).getBean(GrpcServerFactory.class) + .extracting("serviceList", InstanceOfAssertFactories.list(ServerServiceDefinition.class)) + .contains(serviceDefinitionA, serviceDefinitionB); + }); + } + + } + @Configuration(proxyBeanMethods = false) static class ServerBuilderCustomizersConfig { From 6147d231002ac9685882e928fade83ca645f8381 Mon Sep 17 00:00:00 2001 From: Andrey Litvitski Date: Tue, 1 Jul 2025 16:26:58 +0300 Subject: [PATCH 2/3] remove `serverFactory` from `ServerServiceDefinitionFilter` Signed-off-by: Andrey Litvitski --- .../grpc/server/DefaultGrpcServerFactory.java | 5 +++-- .../grpc/server/NettyGrpcServerFactory.java | 5 ++++- .../grpc/server/ServerServiceDefinitionFilter.java | 7 +++---- .../grpc/server/ShadedNettyGrpcServerFactory.java | 5 ++++- .../server/GrpcServerFactoryConfigurations.java | 11 +++++------ .../server/GrpcServerAutoConfigurationTests.java | 4 ++-- 6 files changed, 21 insertions(+), 16 deletions(-) diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java index 4e96cb8a..a8557bc7 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java @@ -28,6 +28,7 @@ import org.apache.commons.logging.LogFactory; import org.springframework.grpc.internal.GrpcUtils; +import org.springframework.lang.Nullable; import com.google.common.collect.Lists; import io.grpc.Grpc; @@ -77,7 +78,7 @@ public DefaultGrpcServerFactory(String address, List> public DefaultGrpcServerFactory(String address, List> serverBuilderCustomizers, KeyManagerFactory keyManager, TrustManagerFactory trustManager, ClientAuth clientAuth, - ServerServiceDefinitionFilter serviceFilter) { + @Nullable ServerServiceDefinitionFilter serviceFilter) { this(address, serverBuilderCustomizers); this.keyManager = keyManager; this.trustManager = trustManager; @@ -98,7 +99,7 @@ public Server createServer() { @Override public void addService(ServerServiceDefinition service) { - if (this.serviceFilter == null || this.serviceFilter.filter(service, this)) { + if (this.serviceFilter == null || this.serviceFilter.filter(service)) { this.serviceList.add(service); } } diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java index f02b7c00..a76f1cf8 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java @@ -21,6 +21,8 @@ import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.TrustManagerFactory; +import org.springframework.lang.Nullable; + import io.grpc.TlsServerCredentials.ClientAuth; import io.grpc.netty.NettyServerBuilder; import io.netty.channel.epoll.EpollEventLoopGroup; @@ -37,7 +39,8 @@ public class NettyGrpcServerFactory extends DefaultGrpcServerFactory> serverBuilderCustomizers, KeyManagerFactory keyManager, - TrustManagerFactory trustManager, ClientAuth clientAuth, ServerServiceDefinitionFilter serviceFilter) { + TrustManagerFactory trustManager, ClientAuth clientAuth, + @Nullable ServerServiceDefinitionFilter serviceFilter) { super(address, serverBuilderCustomizers, keyManager, trustManager, clientAuth, serviceFilter); } diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/ServerServiceDefinitionFilter.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/ServerServiceDefinitionFilter.java index afeeb4e6..bf294471 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/ServerServiceDefinitionFilter.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/ServerServiceDefinitionFilter.java @@ -19,7 +19,7 @@ /** * Strategy to determine whether a {@link ServerServiceDefinition} should be included for - * a given {@link GrpcServerFactory server factory}. + * the {@link GrpcServerFactory server factory}. * * @author Andrey Litvitski */ @@ -28,11 +28,10 @@ public interface ServerServiceDefinitionFilter { /** * Determine whether the given {@link ServerServiceDefinition} should be included for - * the provided {@link GrpcServerFactory server factory}. + * the {@link GrpcServerFactory server factory}. * @param serviceDefinition the gRPC service definition under consideration. - * @param serverFactory the server factory in use. * @return {@code true} if the service should be included; {@code false} otherwise. */ - boolean filter(ServerServiceDefinition serviceDefinition, GrpcServerFactory serverFactory); + boolean filter(ServerServiceDefinition serviceDefinition); } diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java index 4700d4c6..c30c81b1 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java @@ -21,6 +21,8 @@ import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.TrustManagerFactory; +import org.springframework.lang.Nullable; + import io.grpc.TlsServerCredentials.ClientAuth; import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder; import io.grpc.netty.shaded.io.netty.channel.epoll.EpollEventLoopGroup; @@ -37,7 +39,8 @@ public class ShadedNettyGrpcServerFactory extends DefaultGrpcServerFactory> serverBuilderCustomizers, KeyManagerFactory keyManager, - TrustManagerFactory trustManager, ClientAuth clientAuth, ServerServiceDefinitionFilter serviceFilter) { + TrustManagerFactory trustManager, ClientAuth clientAuth, + @Nullable ServerServiceDefinitionFilter serviceFilter) { super(address, serverBuilderCustomizers, keyManager, trustManager, clientAuth, serviceFilter); } diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java index edb5fd92..c8ce96b9 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java @@ -17,7 +17,6 @@ package org.springframework.grpc.autoconfigure.server; import java.util.List; -import java.util.Optional; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.TrustManagerFactory; @@ -31,6 +30,7 @@ import org.springframework.boot.ssl.SslBundles; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Bean; +import org.springframework.lang.Nullable; import org.springframework.context.annotation.Configuration; import org.springframework.grpc.server.GrpcServerFactory; import org.springframework.grpc.server.InProcessGrpcServerFactory; @@ -65,7 +65,7 @@ static class ShadedNettyServerFactoryConfiguration { ShadedNettyGrpcServerFactory shadedNettyGrpcServerFactory(GrpcServerProperties properties, GrpcServiceDiscoverer serviceDiscoverer, GrpcServiceConfigurer serviceConfigurer, ServerBuilderCustomizers serverBuilderCustomizers, SslBundles bundles, - Optional serviceFilter) { + @Nullable ServerServiceDefinitionFilter serviceFilter) { ShadedNettyServerFactoryPropertyMapper mapper = new ShadedNettyServerFactoryPropertyMapper(properties); List> builderCustomizers = List .of(mapper::customizeServerBuilder, serverBuilderCustomizers::customize); @@ -78,8 +78,7 @@ ShadedNettyGrpcServerFactory shadedNettyGrpcServerFactory(GrpcServerProperties p : io.grpc.netty.shaded.io.netty.handler.ssl.util.InsecureTrustManagerFactory.INSTANCE; } ShadedNettyGrpcServerFactory factory = new ShadedNettyGrpcServerFactory(properties.getAddress(), - builderCustomizers, keyManager, trustManager, properties.getSsl().getClientAuth(), - serviceFilter.orElse(null)); + builderCustomizers, keyManager, trustManager, properties.getSsl().getClientAuth(), serviceFilter); serviceDiscoverer.findServices().stream().map(serviceConfigurer::configure).forEach(factory::addService); return factory; } @@ -106,7 +105,7 @@ static class NettyServerFactoryConfiguration { NettyGrpcServerFactory nettyGrpcServerFactory(GrpcServerProperties properties, GrpcServiceDiscoverer serviceDiscoverer, GrpcServiceConfigurer serviceConfigurer, ServerBuilderCustomizers serverBuilderCustomizers, SslBundles bundles, - Optional serviceFilter) { + @Nullable ServerServiceDefinitionFilter serviceFilter) { NettyServerFactoryPropertyMapper mapper = new NettyServerFactoryPropertyMapper(properties); List> builderCustomizers = List .of(mapper::customizeServerBuilder, serverBuilderCustomizers::customize); @@ -119,7 +118,7 @@ NettyGrpcServerFactory nettyGrpcServerFactory(GrpcServerProperties properties, : InsecureTrustManagerFactory.INSTANCE; } NettyGrpcServerFactory factory = new NettyGrpcServerFactory(properties.getAddress(), builderCustomizers, - keyManager, trustManager, properties.getSsl().getClientAuth(), serviceFilter.orElse(null)); + keyManager, trustManager, properties.getSsl().getClientAuth(), serviceFilter); serviceDiscoverer.findServices().stream().map(serviceConfigurer::configure).forEach(factory::addService); return factory; } diff --git a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java index 2e329c6f..84d73db0 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java +++ b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java @@ -419,7 +419,7 @@ void whenFilterExcludesOneServiceThenServerListGetAllowedOnes() { ServerServiceDefinition serviceDefinitionA = mock(); GrpcServerAutoConfigurationTests.this.contextRunner() .withBean(ServerServiceDefinitionFilter.class, - () -> (serviceDefinition, ___) -> serviceDefinition.equals(serviceDefinitionA)) + () -> (serviceDefinition) -> serviceDefinition.equals(serviceDefinitionA)) .run((context) -> { DefaultGrpcServerFactory defaultGrpcServerFactory = (DefaultGrpcServerFactory) context .getBean(GrpcServerFactory.class); @@ -436,7 +436,7 @@ void whenFilterExcludesOneServiceThenServerListGetAllowedOnes() { @Test void whenFilterIncludesAllServicesThenServerListGetAllowedOnes() { GrpcServerAutoConfigurationTests.this.contextRunner() - .withBean(ServerServiceDefinitionFilter.class, () -> (__, ___) -> true) + .withBean(ServerServiceDefinitionFilter.class, () -> (__) -> true) .run((context) -> { DefaultGrpcServerFactory defaultGrpcServerFactory = (DefaultGrpcServerFactory) context .getBean(GrpcServerFactory.class); From d3b58de16f64bd47163adb57475f52cfdeb55ab2 Mon Sep 17 00:00:00 2001 From: Andrey Litvitski Date: Tue, 1 Jul 2025 21:31:04 +0300 Subject: [PATCH 3/3] throw the filter to `InProcessGrpcServerFactory` and `TestInProcessGrpcServerFactory` Signed-off-by: Andrey Litvitski --- .../grpc/server/DefaultGrpcServerFactory.java | 3 ++- .../grpc/server/InProcessGrpcServerFactory.java | 8 ++++++-- .../grpc/server/NettyGrpcServerFactory.java | 3 ++- .../grpc/server/ShadedNettyGrpcServerFactory.java | 3 ++- .../server/GrpcServerFactoryConfigurations.java | 5 +++-- .../grpc/test/InProcessTestAutoConfiguration.java | 13 ++++++++----- 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java index a8557bc7..982f15f5 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/DefaultGrpcServerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2024-2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,6 +50,7 @@ * @param the type of server builder * @author David Syer * @author Chris Bono + * @author Andrey Litvitski * @see ServerProvider#provider() */ public class DefaultGrpcServerFactory> implements GrpcServerFactory { diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/InProcessGrpcServerFactory.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/InProcessGrpcServerFactory.java index cdbc4ca1..259a2e5a 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/InProcessGrpcServerFactory.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/InProcessGrpcServerFactory.java @@ -17,18 +17,22 @@ import java.util.List; +import org.springframework.lang.Nullable; + import io.grpc.inprocess.InProcessServerBuilder; /** * {@link GrpcServerFactory} that can be used to create an in-process gRPC server. * * @author Chris Bono + * @author Andrey Litvitski */ public class InProcessGrpcServerFactory extends DefaultGrpcServerFactory { public InProcessGrpcServerFactory(String address, - List> serverBuilderCustomizers) { - super(address, serverBuilderCustomizers); + List> serverBuilderCustomizers, + @Nullable ServerServiceDefinitionFilter serviceFilter) { + super(address, serverBuilderCustomizers, null, null, null, serviceFilter); } @Override diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java index a76f1cf8..67aefcbd 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/NettyGrpcServerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2024-2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ * * @author David Syer * @author Chris Bono + * @author Andrey Litvitski */ public class NettyGrpcServerFactory extends DefaultGrpcServerFactory { diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java index c30c81b1..ac14202a 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/ShadedNettyGrpcServerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2024-2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,7 @@ * * @author David Syer * @author Chris Bono + * @author Andrey Litvitski */ public class ShadedNettyGrpcServerFactory extends DefaultGrpcServerFactory { diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java index c8ce96b9..60112ed0 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java @@ -143,12 +143,13 @@ static class InProcessServerFactoryConfiguration { @Bean InProcessGrpcServerFactory inProcessGrpcServerFactory(GrpcServerProperties properties, GrpcServiceDiscoverer serviceDiscoverer, GrpcServiceConfigurer serviceConfigurer, - ServerBuilderCustomizers serverBuilderCustomizers) { + ServerBuilderCustomizers serverBuilderCustomizers, + @Nullable ServerServiceDefinitionFilter serviceFilter) { var mapper = new InProcessServerFactoryPropertyMapper(properties); List> builderCustomizers = List .of(mapper::customizeServerBuilder, serverBuilderCustomizers::customize); InProcessGrpcServerFactory factory = new InProcessGrpcServerFactory(properties.getInprocess().getName(), - builderCustomizers); + builderCustomizers, serviceFilter); serviceDiscoverer.findServices().stream().map(serviceConfigurer::configure).forEach(factory::addService); return factory; } diff --git a/spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessTestAutoConfiguration.java b/spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessTestAutoConfiguration.java index 359dd796..cbff197b 100644 --- a/spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessTestAutoConfiguration.java +++ b/spring-grpc-test/src/main/java/org/springframework/grpc/test/InProcessTestAutoConfiguration.java @@ -34,9 +34,11 @@ import org.springframework.grpc.client.InProcessGrpcChannelFactory; import org.springframework.grpc.server.InProcessGrpcServerFactory; import org.springframework.grpc.server.ServerBuilderCustomizer; +import org.springframework.grpc.server.ServerServiceDefinitionFilter; import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; import org.springframework.grpc.server.service.GrpcServiceConfigurer; import org.springframework.grpc.server.service.GrpcServiceDiscoverer; +import org.springframework.lang.Nullable; import io.grpc.BindableService; import io.grpc.ChannelCredentials; @@ -55,9 +57,9 @@ public class InProcessTestAutoConfiguration { @ConditionalOnBean(BindableService.class) @Order(Ordered.HIGHEST_PRECEDENCE) TestInProcessGrpcServerFactory testInProcessGrpcServerFactory(GrpcServiceDiscoverer serviceDiscoverer, - GrpcServiceConfigurer serviceConfigurer, - List> customizers) { - var factory = new TestInProcessGrpcServerFactory(address, customizers); + GrpcServiceConfigurer serviceConfigurer, List> customizers, + @Nullable ServerServiceDefinitionFilter serviceFilter) { + var factory = new TestInProcessGrpcServerFactory(address, customizers, serviceFilter); serviceDiscoverer.findServices().stream().map(serviceConfigurer::configure).forEach(factory::addService); return factory; } @@ -83,8 +85,9 @@ GrpcServerLifecycle inProcessGrpcServerLifecycle(InProcessGrpcServerFactory fact public static class TestInProcessGrpcServerFactory extends InProcessGrpcServerFactory { public TestInProcessGrpcServerFactory(String address, - List> serverBuilderCustomizers) { - super(address, serverBuilderCustomizers); + List> serverBuilderCustomizers, + @Nullable ServerServiceDefinitionFilter serviceFilter) { + super(address, serverBuilderCustomizers, serviceFilter); } }