From ebf9caa44dde53bc7ac4d54df31bb2a761961eb1 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 3 Mar 2025 09:26:06 +0000 Subject: [PATCH] Add config property for http2 by default --- .../src/main/resources/application.properties | 1 - .../src/main/resources/application.properties | 1 - .../ServletEnvironmentPostProcessor.java | 38 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 5 ++- 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/ServletEnvironmentPostProcessor.java diff --git a/samples/grpc-tomcat-secure/src/main/resources/application.properties b/samples/grpc-tomcat-secure/src/main/resources/application.properties index dc54139..21ac05f 100644 --- a/samples/grpc-tomcat-secure/src/main/resources/application.properties +++ b/samples/grpc-tomcat-secure/src/main/resources/application.properties @@ -1,5 +1,4 @@ spring.application.name=grpc-tomcat-secure server.port=9090 -server.http2.enabled=true spring.security.user.name=user spring.security.user.password=user \ No newline at end of file diff --git a/samples/grpc-tomcat/src/main/resources/application.properties b/samples/grpc-tomcat/src/main/resources/application.properties index 735467d..a2b23a9 100644 --- a/samples/grpc-tomcat/src/main/resources/application.properties +++ b/samples/grpc-tomcat/src/main/resources/application.properties @@ -1,6 +1,5 @@ spring.application.name=grpc-tomcat server.port=9090 -server.http2.enabled=true logging.pattern.level=%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}] logging.level.org.springframework.security=DEBUG management.endpoints.web.exposure.include=* diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/ServletEnvironmentPostProcessor.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/ServletEnvironmentPostProcessor.java new file mode 100644 index 0000000..3f3ba8e --- /dev/null +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/ServletEnvironmentPostProcessor.java @@ -0,0 +1,38 @@ +/* + * Copyright 2024-2024 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.autoconfigure.server; + +import java.util.Map; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.util.ClassUtils; + +public class ServletEnvironmentPostProcessor implements EnvironmentPostProcessor { + + private static final boolean SERVLET_AVAILABLE = ClassUtils.isPresent("io.grpc.servlet.jakarta.GrpcServlet", null); + + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + if (SERVLET_AVAILABLE) { + environment.getPropertySources() + .addFirst(new MapPropertySource("grpc-servlet", Map.of("server.http2.enabled", "true"))); + } + } + +} diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index a574d79..aa10c79 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -1,2 +1,5 @@ org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer=\ -org.springframework.grpc.autoconfigure.server.security.GrpcDisableCsrfHttpConfigurer \ No newline at end of file +org.springframework.grpc.autoconfigure.server.security.GrpcDisableCsrfHttpConfigurer + +org.springframework.boot.env.EnvironmentPostProcessor=\ +org.springframework.grpc.autoconfigure.server.ServletEnvironmentPostProcessor \ No newline at end of file