这是indexloc提供的服务,不要输入任何密码
Skip to content
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
1 change: 1 addition & 0 deletions spring-boot-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
<module>spring-boot-openapi</module>
<module>spring-boot-brave</module>
<module>spring-boot-simple</module>
<module>spring-boot-http2</module>
</modules>

<build>
Expand Down
53 changes: 53 additions & 0 deletions spring-boot-modules/spring-boot-http2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-http2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-http2</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.baeldung.spring-boot-modules</groupId>
<artifactId>spring-boot-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<spring-boot.version>3.4.3</spring-boot.version>
<logback.version>1.5.17</logback.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.http2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Http2Application {

public static void main(String[] args) {
SpringApplication.run(Http2Application.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baeldung.http2.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http2.Http2Protocol;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@Profile("config-class")
public class Http2Config {

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> getWebServerFactoryCustomizer() {
return factory -> {
Connector httpConnector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
httpConnector.setPort(8080);
factory.addConnectorCustomizers(connector -> connector.addUpgradeProtocol(new Http2Protocol()));
factory.addAdditionalTomcatConnectors(httpConnector);
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.http2.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Http2Controller {

@GetMapping("/http2/echo")
public String getEcho(@RequestParam String message) {
return message;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server:
http2:
enabled: false

port: 8443

ssl:
enabled: true
key-store: classpath:keystore.p12
key-store-password: abcd1234
key-store-type: PKCS12
key-alias: http2-alias
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server:
http2:
enabled: true

port: 8443

ssl:
enabled: true
key-store: classpath:keystore.p12
key-store-password: abcd1234
key-store-type: PKCS12
key-alias: http2-alias
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.http2;

import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;

public class AllCertsTrustManager implements X509TrustManager {

public X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(X509Certificate[] certs, String authType) {
}

public void checkServerTrusted(X509Certificate[] certs, String authType) {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.baeldung.http2;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import static org.assertj.core.api.Assertions.assertThat;

// Integration test based on enabling HTTP/2 using the configuration in application.yml
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class Http2ApplicationIntegrationTest {

private static SSLContext sslContext;

@LocalServerPort
private int port;

private HttpClient httpClient;

@BeforeAll
static void setupOnce() throws NoSuchAlgorithmException, KeyManagementException {
// Get rid of cert validation due to the self-signed certificate
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null,
new TrustManager[] { new AllCertsTrustManager() },
new java.security.SecureRandom());
}

@BeforeEach
void setup() {
httpClient = HttpClient.newBuilder()
.sslContext(sslContext)
.version(HttpClient.Version.HTTP_2)
.build();
}

@Test
void whenSendHttp2Request_thenReturnHttp2Response() throws Exception {
// when
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(String.format("https://localhost:%s/http2/echo?message=test", port)))
.GET()
.build();
HttpResponse<?> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

// then
assertThat(response.statusCode()).isEqualTo(200);
assertThat(response.body()).isEqualTo("test");
assertThat(response.version()).isEqualTo(HttpClient.Version.HTTP_2);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.http2;

import org.springframework.test.context.ActiveProfiles;

// Integration test based on enabling HTTP/2 using the configuration class Http2Config
@ActiveProfiles("config-class")
class Http2ConfigClassApplicationIntegrationTest extends Http2ApplicationIntegrationTest {
}