Skip to content

Commit 1d30a47

Browse files
committed
Merge branch '2.4.x'
Closes gh-24708
2 parents 3b8663c + b8a1869 commit 1d30a47

File tree

6 files changed

+182
-62
lines changed

6 files changed

+182
-62
lines changed

spring-boot-project/spring-boot-docs/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ dependencies {
7272
implementation("com.zaxxer:HikariCP")
7373
implementation("io.micrometer:micrometer-core")
7474
implementation("io.projectreactor.netty:reactor-netty-http")
75+
implementation("io.undertow:undertow-core")
7576
implementation("jakarta.servlet:jakarta.servlet-api")
7677
implementation("org.apache.commons:commons-dbcp2")
7778
implementation("org.apache.kafka:kafka-streams")

spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc

+11-48
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,11 @@ The example below is for Tomcat with the `spring-boot-starter-web` (Servlet stac
734734
}
735735
----
736736

737+
NOTE: Spring Boot uses that infrastructure internally to auto-configure the server.
738+
Auto-configured `WebServerFactoryCustomizer` beans have an order of `0` and will be processed before any user-defined customizers, unless it has an explicit order that states otherwise.
739+
740+
Once you've got access to a `WebServerFactory` using the customizer, you can use it to configure specific parts, like connectors, server resources, or the server itself - all using server-specific APIs.
741+
737742
In addition Spring Boot provides:
738743

739744
[[howto-configure-webserver-customizers]]
@@ -758,10 +763,8 @@ In addition Spring Boot provides:
758763
| `NettyReactiveWebServerFactory`
759764
|===
760765

761-
Once you've got access to a `WebServerFactory`, you can often add customizers to it to configure specific parts, like connectors, server resources, or the server itself - all using server-specific APIs.
762-
763-
As a last resort, you can also declare your own `WebServerFactory` component, which will override the one provided by Spring Boot.
764-
In this case, you can't rely on configuration properties in the `server` namespace anymore.
766+
As a last resort, you can also declare your own `WebServerFactory` bean, which will override the one provided by Spring Boot.
767+
When you do so, auto-configured customizers are still applied on your custom factory, so use that option carefully.
765768

766769

767770

@@ -918,7 +921,7 @@ You can customize the valve's configuration by adding an entry to `application.p
918921

919922
NOTE: You can trust all proxies by setting the `internal-proxies` to empty (but do not do so in production).
920923

921-
You can take complete control of the configuration of Tomcat's `RemoteIpValve` by switching the automatic one off (to do so, set `server.forward-headers-strategy=NONE`) and adding a new valve instance in a `TomcatServletWebServerFactory` bean.
924+
You can take complete control of the configuration of Tomcat's `RemoteIpValve` by switching the automatic one off (to do so, set `server.forward-headers-strategy=NONE`) and adding a new valve instance using a `WebServerFactoryCustomizer` bean.
922925

923926

924927

@@ -928,35 +931,7 @@ You can add an `org.apache.catalina.connector.Connector` to the `TomcatServletWe
928931

929932
[source,java,indent=0,subs="verbatim,quotes,attributes"]
930933
----
931-
@Bean
932-
public ServletWebServerFactory servletContainer() {
933-
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
934-
tomcat.addAdditionalTomcatConnectors(createSslConnector());
935-
return tomcat;
936-
}
937-
938-
private Connector createSslConnector() {
939-
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
940-
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
941-
try {
942-
File keystore = new ClassPathResource("keystore").getFile();
943-
File truststore = new ClassPathResource("keystore").getFile();
944-
connector.setScheme("https");
945-
connector.setSecure(true);
946-
connector.setPort(8443);
947-
protocol.setSSLEnabled(true);
948-
protocol.setKeystoreFile(keystore.getAbsolutePath());
949-
protocol.setKeystorePass("changeit");
950-
protocol.setTruststoreFile(truststore.getAbsolutePath());
951-
protocol.setTruststorePass("changeit");
952-
protocol.setKeyAlias("apitester");
953-
return connector;
954-
}
955-
catch (IOException ex) {
956-
throw new IllegalStateException("can't access keystore: [" + keystore
957-
+ "] or truststore: [" + truststore + "]", ex);
958-
}
959-
}
934+
include::{code-examples}/context/embedded/TomcatMultipleConnectorsExample.java[tag=configuration]
960935
----
961936

962937

@@ -1003,19 +978,7 @@ Add an `UndertowBuilderCustomizer` to the `UndertowServletWebServerFactory` and
1003978

1004979
[source,java,indent=0,subs="verbatim,quotes,attributes"]
1005980
----
1006-
@Bean
1007-
public UndertowServletWebServerFactory servletWebServerFactory() {
1008-
UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
1009-
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
1010-
1011-
@Override
1012-
public void customize(Builder builder) {
1013-
builder.addHttpListener(8080, "0.0.0.0");
1014-
}
1015-
1016-
});
1017-
return factory;
1018-
}
981+
include::{code-examples}/context/embedded/UndertowMultipleListenersExample.java[tag=configuration]
1019982
----
1020983

1021984

@@ -2432,7 +2395,7 @@ You can switch on the valve by adding some entries to `application.properties`,
24322395
----
24332396

24342397
(The presence of either of those properties switches on the valve.
2435-
Alternatively, you can add the `RemoteIpValve` by adding a `TomcatServletWebServerFactory` bean.)
2398+
Alternatively, you can add the `RemoteIpValve` by customizing the `TomcatServletWebServerFactory` using a `WebServerFactoryCustomizer` bean.)
24362399

24372400
To configure Spring Security to require a secure channel for all (or some) requests, consider adding your own `SecurityFilterChain` bean that adds the following `HttpSecurity` configuration:
24382401

spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc

+10-14
Original file line numberDiff line numberDiff line change
@@ -3462,30 +3462,26 @@ The following example shows programmatically setting the port:
34623462
}
34633463
----
34643464

3465-
NOTE: `TomcatServletWebServerFactory`, `JettyServletWebServerFactory` and `UndertowServletWebServerFactory` are dedicated variants of `ConfigurableServletWebServerFactory` that have additional customization setter methods for Tomcat, Jetty and Undertow respectively.
3465+
`TomcatServletWebServerFactory`, `JettyServletWebServerFactory` and `UndertowServletWebServerFactory` are dedicated variants of `ConfigurableServletWebServerFactory` that have additional customization setter methods for Tomcat, Jetty and Undertow respectively.
3466+
The following example shows how to customize `TomcatServletWebServerFactory` that provides access to Tomcat-specific configuration options:
3467+
3468+
[source,java,indent=0,subs="verbatim,quotes,attributes"]
3469+
----
3470+
include::{code-examples}/context/embedded/TomcatServerCustomizerExample.java[tag=configuration]
3471+
----
34663472

34673473

34683474

34693475
[[boot-features-customizing-configurableservletwebserverfactory-directly]]
34703476
===== Customizing ConfigurableServletWebServerFactory Directly
3471-
If the preceding customization techniques are too limited, you can register the `TomcatServletWebServerFactory`, `JettyServletWebServerFactory`, or `UndertowServletWebServerFactory` bean yourself.
3472-
3473-
[source,java,indent=0]
3474-
----
3475-
@Bean
3476-
public ConfigurableServletWebServerFactory webServerFactory() {
3477-
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
3478-
factory.setPort(9000);
3479-
factory.setSessionTimeout(10, TimeUnit.MINUTES);
3480-
factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"));
3481-
return factory;
3482-
}
3483-
----
3477+
For more advanced use cases that require you to extend from `ServletWebServerFactory`, you can expose a bean of such type yourself.
34843478

34853479
Setters are provided for many configuration options.
34863480
Several protected method "`hooks`" are also provided should you need to do something more exotic.
34873481
See the {spring-boot-module-api}/web/servlet/server/ConfigurableServletWebServerFactory.html[source code documentation] for details.
34883482

3483+
NOTE: Auto-configured customizers are still applied on your custom factory, so use that option carefully.
3484+
34893485

34903486

34913487
[[boot-features-jsp-limitations]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.context.embedded;
18+
19+
import java.io.IOException;
20+
import java.net.URL;
21+
22+
import org.apache.catalina.connector.Connector;
23+
import org.apache.coyote.http11.Http11NioProtocol;
24+
25+
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
26+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.util.ResourceUtils;
30+
31+
/**
32+
* Example configuration for configuring Tomcat with an additional {@link Connector}.
33+
*
34+
* @author Stephane Nicoll
35+
*/
36+
@Configuration(proxyBeanMethods = false)
37+
public class TomcatMultipleConnectorsExample {
38+
39+
// tag::configuration[]
40+
@Bean
41+
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> sslConnectorCustomizer() {
42+
return (tomcat) -> tomcat.addAdditionalTomcatConnectors(createSslConnector());
43+
}
44+
45+
private Connector createSslConnector() {
46+
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
47+
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
48+
try {
49+
URL keystore = ResourceUtils.getURL("keystore");
50+
URL truststore = ResourceUtils.getURL("truststore");
51+
connector.setScheme("https");
52+
connector.setSecure(true);
53+
connector.setPort(8443);
54+
protocol.setSSLEnabled(true);
55+
protocol.setKeystoreFile(keystore.toString());
56+
protocol.setKeystorePass("changeit");
57+
protocol.setTruststoreFile(truststore.toString());
58+
protocol.setTruststorePass("changeit");
59+
protocol.setKeyAlias("apitester");
60+
return connector;
61+
}
62+
catch (IOException ex) {
63+
throw new IllegalStateException("Fail to create ssl connector", ex);
64+
}
65+
}
66+
// end::configuration[]
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.context.embedded;
18+
19+
import java.time.Duration;
20+
21+
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
22+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
23+
import org.springframework.stereotype.Component;
24+
25+
/**
26+
* Example of a {@link WebServerFactoryCustomizer} that uses a more narrowed server type.
27+
*
28+
* @author Stephane Nicoll
29+
*/
30+
// tag::configuration[]
31+
@Component
32+
public class TomcatServerCustomizerExample implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
33+
34+
@Override
35+
public void customize(TomcatServletWebServerFactory server) {
36+
server.addConnectorCustomizers(
37+
(tomcatConnector) -> tomcatConnector.setAsyncTimeout(Duration.ofSeconds(20).toMillis()));
38+
}
39+
40+
}
41+
// end::configuration[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.context.embedded;
18+
19+
import io.undertow.Undertow.Builder;
20+
21+
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
22+
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
23+
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
27+
/**
28+
* Example configuration for configuring Undertow with an additional listener.
29+
*
30+
* @author Stephane Nicoll
31+
*/
32+
@Configuration(proxyBeanMethods = false)
33+
public class UndertowMultipleListenersExample {
34+
35+
// tag::configuration[]
36+
@Bean
37+
public WebServerFactoryCustomizer<UndertowServletWebServerFactory> undertowListenerCustomizer() {
38+
return (factory) -> {
39+
factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
40+
41+
@Override
42+
public void customize(Builder builder) {
43+
builder.addHttpListener(8080, "0.0.0.0");
44+
}
45+
46+
});
47+
};
48+
}
49+
// end::configuration[]
50+
51+
}

0 commit comments

Comments
 (0)