Skip to content

Allow filtering of global server interceptors #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.
* 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.Server;
import io.grpc.ServerServiceDefinition;

/**
* Marker interface for {@link GrpcServerFactory} that is to be handled by the servlet
* container.
*
* @author Chris Bono
*/
public class ServletGrpcServerFactory implements GrpcServerFactory {

/**
* Default instance of marker interface.
*/
public static ServletGrpcServerFactory INSTANCE = new ServletGrpcServerFactory();

@Override
public Server createServer() {
throw new UnsupportedOperationException("Marker interface only");
}

@Override
public void addService(ServerServiceDefinition service) {
throw new UnsupportedOperationException("Marker interface only");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import org.springframework.context.ApplicationContext;
import org.springframework.grpc.internal.ApplicationContextBeanLookupUtils;
import org.springframework.grpc.server.GlobalServerInterceptor;
import org.springframework.grpc.server.GrpcServerFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import io.grpc.BindableService;
import io.grpc.ServerInterceptor;
Expand All @@ -42,8 +44,12 @@ public class DefaultGrpcServiceConfigurer implements GrpcServiceConfigurer, Init

private List<ServerInterceptor> globalInterceptors;

public DefaultGrpcServiceConfigurer(ApplicationContext applicationContext) {
private ServerInterceptorFilter interceptorFilter;

public DefaultGrpcServiceConfigurer(ApplicationContext applicationContext,
@Nullable ServerInterceptorFilter interceptorFilter) {
this.applicationContext = applicationContext;
this.interceptorFilter = interceptorFilter;
}

@Override
Expand All @@ -52,8 +58,10 @@ public void afterPropertiesSet() {
}

@Override
public ServerServiceDefinition configure(ServerServiceDefinitionSpec serviceDefinitionSpec) {
return bindInterceptors(serviceDefinitionSpec.service(), serviceDefinitionSpec.serviceInfo());
public ServerServiceDefinition configure(ServerServiceDefinitionSpec serviceSpec, GrpcServerFactory serverFactory) {
Assert.notNull(serviceSpec, () -> "serviceSpec must not be null");
Assert.notNull(serverFactory, () -> "serverFactory must not be null");
return bindInterceptors(serviceSpec.service(), serviceSpec.serviceInfo(), serverFactory);
}

private List<ServerInterceptor> findGlobalInterceptors() {
Expand All @@ -62,13 +70,18 @@ private List<ServerInterceptor> findGlobalInterceptors() {
}

private ServerServiceDefinition bindInterceptors(BindableService bindableService,
@Nullable GrpcServiceInfo serviceInfo) {
@Nullable GrpcServiceInfo serviceInfo, GrpcServerFactory serverFactory) {
var serviceDef = bindableService.bindService();

// Add and filter global interceptors first
List<ServerInterceptor> allInterceptors = new ArrayList<>(this.globalInterceptors);
if (this.interceptorFilter != null) {
allInterceptors
.removeIf(interceptor -> !this.interceptorFilter.filter(interceptor, serviceDef, serverFactory));
}
if (serviceInfo == null) {
return ServerInterceptors.interceptForward(serviceDef, this.globalInterceptors);
return ServerInterceptors.interceptForward(serviceDef, allInterceptors);
}
// Add global interceptors first
List<ServerInterceptor> allInterceptors = new ArrayList<>(this.globalInterceptors);
// Add interceptors by type
Arrays.stream(serviceInfo.interceptors())
.forEachOrdered(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.grpc.server.service;

import org.springframework.grpc.server.GrpcServerFactory;

import io.grpc.ServerServiceDefinition;

/**
Expand All @@ -29,12 +31,14 @@
public interface GrpcServiceConfigurer {

/**
* Configure and bind a gRPC service spec resulting in a service definition that can
* Configure and bind a gRPC server spec resulting in a service definition that can
* then be added to a gRPC server.
* @param serviceSpec the spec containing the info about the service
* @return bound and configured service definition that is ready to be added to the
* @param serverFactory the factory that provides the server that the service will be
* added to
* @return bound and configured service definition that is ready to be added to a
* server
*/
ServerServiceDefinition configure(ServerServiceDefinitionSpec serviceSpec);
ServerServiceDefinition configure(ServerServiceDefinitionSpec serviceSpec, GrpcServerFactory serverFactory);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.service;

import org.springframework.grpc.server.GrpcServerFactory;

import io.grpc.ServerInterceptor;
import io.grpc.ServerServiceDefinition;

/**
* Strategy to determine whether a global {@link ServerInterceptor server interceptor}
* should be applied to {@link ServerServiceDefinition gRPC service}.
*
* @author Chris Bono
*/
@FunctionalInterface
public interface ServerInterceptorFilter {

/**
* Determine whether an interceptor should be applied to a service when the service is
* running on a server provided by the given server factory.
* @param interceptor the server interceptor under consideration.
* @param service the service being added.
* @param serverFactory the server factory in use.
* @return {@code true} if the interceptor should be included; {@code false}
* otherwise.
*/
boolean filter(ServerInterceptor interceptor, ServerServiceDefinition service, GrpcServerFactory serverFactory);

}
Loading