SpringBoot 2.0 系列007 --WEB容器详解
我们知道java常用的两大容器tomcat和jetty,其中SB默认内嵌了tomcat容器。那么SB都支持什么属性呢?
- 参阅ServerProperties.java
基本是通用的服务器配置,以及error、Compression、Http2、Servlet、Tomcat、Jetty、Undertow等配置。
可用配置
1. 通用服务配置
port
服务器端口号
- 使用方式
server.port=8080,默認端口号8080
address
服务绑定的网络地址
- 使用方式
server.address=127.0.0.1
useForwardHeaders
请求是否允许X-Forwarded-*
- 使用方式
server.use-forward-headers=false
serverHeader
服务响应头信息
- 使用方式
server.server-header=xxxx
maxHttpHeaderSize
最大头信息大小
- 使用方式
server.max-http-header-size=0
connectionTimeout
连接超时时间
- 使用方式
server.connection-timeout=-1
2. ssl
主要是ssl相关配置。具体参阅 org.springframework.boot.web.server.Ssl类
- 使用方式
server.ssl.xxx=xxx
server.ssl.ciphers= # Supported SSL ciphers. server.ssl.client-auth= # Whether client authentication is wanted ("want") or needed ("need"). Requires a trust store. server.ssl.enabled= # Enable SSL support. server.ssl.enabled-protocols= # Enabled SSL protocols. server.ssl.key-alias= # Alias that identifies the key in the key store. server.ssl.key-password= # Password used to access the key in the key store. server.ssl.key-store= # Path to the key store that holds the SSL certificate (typically a jks file). server.ssl.key-store-password= # Password used to access the key store. server.ssl.key-store-provider= # Provider for the key store. server.ssl.key-store-type= # Type of the key store. server.ssl.protocol=TLS # SSL protocol to use. server.ssl.trust-store= # Trust store that holds SSL certificates. server.ssl.trust-store-password= # Password used to access the trust store. server.ssl.trust-store-provider= # Provider for the trust store. server.ssl.trust-store-type= # Type of the trust store.
public class Ssl {
1/\*\* 2 \* Whether to enable SSL support. 3 \*/ 4private boolean enabled = true; 5 6/\*\* 7 \* Whether client authentication is wanted ("want") or needed ("need"). Requires a 8 \* trust store. 9 \*/ 10private ClientAuth clientAuth; 11 12/\*\* 13 \* Supported SSL ciphers. 14 \*/ 15private String\[\] ciphers; 16 17/\*\* 18 \* Enabled SSL protocols. 19 \*/ 20private String\[\] enabledProtocols; 21 22/\*\* 23 \* Alias that identifies the key in the key store. 24 \*/ 25private String keyAlias; 26 27/\*\* 28 \* Password used to access the key in the key store. 29 \*/ 30private String keyPassword; 31 32/\*\* 33 \* Path to the key store that holds the SSL certificate (typically a jks file). 34 \*/ 35private String keyStore; 36 37/\*\* 38 \* Password used to access the key store. 39 \*/ 40private String keyStorePassword; 41 42/\*\* 43 \* Type of the key store. 44 \*/ 45private String keyStoreType; 46 47/\*\* 48 \* Provider for the key store. 49 \*/ 50private String keyStoreProvider; 51 52/\*\* 53 \* Trust store that holds SSL certificates. 54 \*/ 55private String trustStore; 56 57/\*\* 58 \* Password used to access the trust store. 59 \*/ 60private String trustStorePassword; 61 62/\*\* 63 \* Type of the trust store. 64 \*/ 65private String trustStoreType; 66 67/\*\* 68 \* Provider for the trust store. 69 \*/ 70private String trustStoreProvider; 71 72/\*\* 73 \* SSL protocol to use. 74 \*/ 75private String protocol = "TLS"; 76 77public boolean isEnabled() { 78 return this.enabled; 79} 80 81public void setEnabled(boolean enabled) { 82 this.enabled = enabled; 83} 84 85public ClientAuth getClientAuth() { 86 return this.clientAuth; 87} 88 89public void setClientAuth(ClientAuth clientAuth) { 90 this.clientAuth = clientAuth; 91} 92 93public String\[\] getCiphers() { 94 return this.ciphers; 95} 96 97public void setCiphers(String\[\] ciphers) { 98 this.ciphers = ciphers; 99} 100 101public String getKeyAlias() { 102 return this.keyAlias; 103} 104 105public void setKeyAlias(String keyAlias) { 106 this.keyAlias = keyAlias; 107} 108 109public String getKeyPassword() { 110 return this.keyPassword; 111} 112 113public void setKeyPassword(String keyPassword) { 114 this.keyPassword = keyPassword; 115} 116 117public String getKeyStore() { 118 return this.keyStore; 119} 120 121public void setKeyStore(String keyStore) { 122 this.keyStore = keyStore; 123} 124 125public String getKeyStorePassword() { 126 return this.keyStorePassword; 127} 128 129public void setKeyStorePassword(String keyStorePassword) { 130 this.keyStorePassword = keyStorePassword; 131} 132 133public String getKeyStoreType() { 134 return this.keyStoreType; 135} 136 137public void setKeyStoreType(String keyStoreType) { 138 this.keyStoreType = keyStoreType; 139} 140 141public String getKeyStoreProvider() { 142 return this.keyStoreProvider; 143} 144 145public void setKeyStoreProvider(String keyStoreProvider) { 146 this.keyStoreProvider = keyStoreProvider; 147} 148 149public String\[\] getEnabledProtocols() { 150 return this.enabledProtocols; 151} 152 153public void setEnabledProtocols(String\[\] enabledProtocols) { 154 this.enabledProtocols = enabledProtocols; 155} 156 157public String getTrustStore() { 158 return this.trustStore; 159} 160 161public void setTrustStore(String trustStore) { 162 this.trustStore = trustStore; 163} 164 165public String getTrustStorePassword() { 166 return this.trustStorePassword; 167} 168 169public void setTrustStorePassword(String trustStorePassword) { 170 this.trustStorePassword = trustStorePassword; 171} 172 173public String getTrustStoreType() { 174 return this.trustStoreType; 175} 176 177public void setTrustStoreType(String trustStoreType) { 178 this.trustStoreType = trustStoreType; 179} 180 181public String getTrustStoreProvider() { 182 return this.trustStoreProvider; 183} 184 185public void setTrustStoreProvider(String trustStoreProvider) { 186 this.trustStoreProvider = trustStoreProvider; 187} 188 189public String getProtocol() { 190 return this.protocol; 191} 192 193public void setProtocol(String protocol) { 194 this.protocol = protocol; 195} 196 197/\*\* 198 \* Client authentication types. 199 \*/ 200public enum ClientAuth { 201 202 /\*\* 203 \* Client authentication is wanted but not mandatory. 204 \*/ 205 WANT, 206 207 /\*\* 208 \* Client authentication is needed and mandatory. 209 \*/ 210 NEED 211 212}
}
3. compression
简单的压缩配置,具体参阅org.springframework.boot.web.server.Compression
- 使用方式
server.compression.xx=xx
server.compression.enabled=false # Whether response compression is enabled. server.compression.excluded-user-agents= # List of user-agents to exclude from compression. server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript # Comma-separated list of MIME types that should be compressed. server.compression.min-response-size=2048 # Minimum "Content-Length" value that is required for compression to be performed.
- 附源码
public class Compression {
1/\*\* 2 \* Whether response compression is enabled. 3 \*/ 4private boolean enabled = false; 5 6/\*\* 7 \* Comma-separated list of MIME types that should be compressed. 8 \*/ 9private String\[\] mimeTypes = new String\[\] { "text/html", "text/xml", "text/plain", 10 "text/css", "text/javascript", "application/javascript", "application/json", 11 "application/xml" }; 12 13/\*\* 14 \* Comma-separated list of user agents for which responses should not be compressed. 15 \*/ 16private String\[\] excludedUserAgents = null; 17 18/\*\* 19 \* Minimum "Content-Length" value that is required for compression to be performed. 20 \*/ 21private int minResponseSize = 2048; 22 23public boolean getEnabled() { 24 return this.enabled; 25} 26 27public void setEnabled(boolean enabled) { 28 this.enabled = enabled; 29} 30 31public String\[\] getMimeTypes() { 32 return this.mimeTypes; 33} 34 35public void setMimeTypes(String\[\] mimeTypes) { 36 this.mimeTypes = mimeTypes; 37} 38 39public int getMinResponseSize() { 40 return this.minResponseSize; 41} 42 43public void setMinResponseSize(int minSize) { 44 this.minResponseSize = minSize; 45} 46 47public String\[\] getExcludedUserAgents() { 48 return this.excludedUserAgents; 49} 50 51public void setExcludedUserAgents(String\[\] excludedUserAgents) { 52 this.excludedUserAgents = excludedUserAgents; 53}
}
4. http2
主要是开启http2的支持,具体参阅org.springframework.boot.web.server.Http2
- 使用方式
server.http2.enabled=false
5. servlet
主要是servlet相关的配置,具体参阅org.springframework.boot.autoconfigure.web.ServerProperties.Servlet
- 使用方式
server.servlet.contextPath=/chapter01
server.servlet.context-parameters.*= # Servlet context init parameters. server.servlet.context-path= # Context path of the application. server.servlet.application-display-name=application # Display name of the application. server.servlet.jsp.class-name=org.apache.jasper.servlet.JspServlet # The class name of the JSP servlet. server.servlet.jsp.init-parameters.*= # Init parameters used to configure the JSP servlet. server.servlet.jsp.registered=true # Whether the JSP servlet is registered. server.servlet.path=/ # Path of the main dispatcher servlet. server.servlet.session.cookie.comment= # Comment for the session cookie. server.servlet.session.cookie.domain= # Domain for the session cookie. server.servlet.session.cookie.http-only= # "HttpOnly" flag for the session cookie. server.servlet.session.cookie.max-age= # Maximum age of the session cookie. If a duration suffix is not specified, seconds will be used. server.servlet.session.cookie.name= # Session cookie name. server.servlet.session.cookie.path= # Path of the session cookie. server.servlet.session.cookie.secure= # "Secure" flag for the session cookie. server.servlet.session.persistent=false # Whether to persist session data between restarts. server.servlet.session.store-dir= # Directory used to store session data. server.servlet.session.timeout= # Session timeout. If a duration suffix is not specified, seconds will be used. server.servlet.session.tracking-modes= # Session tracking modes (one or more of the following: "cookie", "url", "ssl").
6. tomcat
主要是tomcat相关配置,具体参阅org.springframework.boot.autoconfigure.web.ServerProperties.Tomcat
- 使用方式
server.tomcat.uri-encoding=UTF-8
server.tomcat.accept-count=0 # Maximum queue length for incoming connection requests when all possible request processing threads are in use. server.tomcat.accesslog.buffered=true # Whether to buffer output such that it is flushed only periodically. server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir. server.tomcat.accesslog.enabled=false # Enable access log. server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name. server.tomcat.accesslog.pattern=common # Format pattern for access logs. server.tomcat.accesslog.prefix=access_log # Log file name prefix. server.tomcat.accesslog.rename-on-rotate=false # Whether to defer inclusion of the date stamp in the file name until rotate time. server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for the IP address, Hostname, protocol, and port used for the request. server.tomcat.accesslog.rotate=true # Whether to enable access log rotation. server.tomcat.accesslog.suffix=.log # Log file name suffix. server.tomcat.additional-tld-skip-patterns= # Comma-separated list of additional patterns that match jars to ignore for TLD scanning. server.tomcat.background-processor-delay=30s # Delay between the invocation of backgroundProcess methods. If a duration suffix is not specified, seconds will be used. server.tomcat.basedir= # Tomcat base directory. If not specified, a temporary directory is used. server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\ 192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\ 169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\ 127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\ 172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\ 172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\ 172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # Regular expression matching trusted IP addresses. server.tomcat.max-connections=0 # Maximum number of connections that the server accepts and processes at any given time. server.tomcat.max-http-header-size=0 # Maximum size, in bytes, of the HTTP message header. server.tomcat.max-http-post-size=0 # Maximum size, in bytes, of the HTTP post content. server.tomcat.max-threads=0 # Maximum number of worker threads. server.tomcat.min-spare-threads=0 # Minimum number of worker threads. server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value. server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto". server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL. server.tomcat.redirect-context-root= # Whether requests to the context root should be redirected by appending a / to the path. server.tomcat.remote-ip-header= # Name of the HTTP header from which the remote IP is extracted. For instance, `X-FORWARDED-FOR`. server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache. server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI. server.tomcat.use-relative-redirects= # Whether HTTP 1.1 and later location headers generated by a call to sendRedirect will use relative or absolute redirects.
7. jetty
也是一款web服务器,这里主要是jetty相关的配置,具体参阅org.springframework.boot.autoconfigure.web.ServerProperties.Jetty
- 使用方式
server.jetty.selectors=1
server.jetty.acceptors= # Number of acceptor threads to use. server.jetty.accesslog.append=false # Append to log. server.jetty.accesslog.date-format=dd/MMM/yyyy:HH:mm:ss Z # Timestamp format of the request log. server.jetty.accesslog.enabled=false # Enable access log. server.jetty.accesslog.extended-format=false # Enable extended NCSA format. server.jetty.accesslog.file-date-format= # Date format to place in log file name. server.jetty.accesslog.filename= # Log filename. If not specified, logs redirect to "System.err". server.jetty.accesslog.locale= # Locale of the request log. server.jetty.accesslog.log-cookies=false # Enable logging of the request cookies. server.jetty.accesslog.log-latency=false # Enable logging of request processing time. server.jetty.accesslog.log-server=false # Enable logging of the request hostname. server.jetty.accesslog.retention-period=31 # Number of days before rotated log files are deleted. server.jetty.accesslog.time-zone=GMT # Timezone of the request log. server.jetty.max-http-post-size=0 # Maximum size, in bytes, of the HTTP post or put content. server.jetty.selectors= # Number of selector threads to use.
8. undertow
是红帽的一款开源web服务器,性能卓越。很多人推荐使用此款服务器。这里主要是undertow相关配置,具体参阅org.springframework.boot.autoconfigure.web.ServerProperties.Undertow
- 使用方式
server.undertow.enabled=false
server.undertow.accesslog.dir= # Undertow access log directory. server.undertow.accesslog.enabled=false # Whether to enable the access log. server.undertow.accesslog.pattern=common # Format pattern for access logs. server.undertow.accesslog.prefix=access_log. # Log file name prefix. server.undertow.accesslog.rotate=true # Whether to enable access log rotation. server.undertow.accesslog.suffix=log # Log file name suffix. server.undertow.buffer-size= # Size of each buffer, in bytes. server.undertow.direct-buffers= # Whether to allocate buffers outside the Java heap. server.undertow.io-threads= # Number of I/O threads to create for the worker. server.undertow.eager-filter-init=true # Whether servlet filters should be initialized on startup. server.undertow.max-http-post-size=0 # Maximum size, in bytes, of the HTTP post content. server.undertow.worker-threads= # Number of worker threads.
9. error
错误相关配置
server.error.include-exception=false # Include the "exception" attribute. server.error.include-stacktrace=never # When to include a "stacktrace" attribute. server.error.path=/error # Path of the error controller. server.error.whitelabel.enabled=true # Whether to enable the default error page displayed in browsers in case of a server error.
三大服务器整合使用
这里是接之前的教程,默认已经引入我们的父工程。自己测试的话引入SB提供的parent即可。
tomcat
tomcat服务器的相关配置
- 内置tomcat方式
- 外置tomcat
- application-tomcat.yml文件
server: tomcat: basedir: D:\work\ricky\tomcat
jetty
jetty相关配置
- 依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-jetty</artifactId> 4 </dependency>
- application-jetty.yml 示例
server: jetty: accesslog: enabled: true date-format: yyyy-MM-dd:HH:mm:ss filename: D:\work\ricky\jetty\access.log
undertow
undertow服务器整合
- 依赖
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-undertow</artifactId> 4 </dependency>
- application-undertow.yml
server: undertow: accesslog: dir: D:\work\ricky\undertow\ prefix: chapter05. suffix: log enabled: true
说明
从启动和响应来看,undertow是最快的,tomcat和jetty相差不大。
附源码
/* * Copyright 2012-2018 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 * * http://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.boot.autoconfigure.web;
import java.io.File; import java.net.InetAddress; import java.nio.charset.Charset; import java.time.Duration; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.TimeZone;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.boot.convert.DurationUnit; import org.springframework.boot.web.server.Compression; import org.springframework.boot.web.server.Http2; import org.springframework.boot.web.server.Ssl; import org.springframework.boot.web.servlet.server.Jsp; import org.springframework.boot.web.servlet.server.Session; import org.springframework.util.Assert; import org.springframework.util.StringUtils;
/** * {@link ConfigurationProperties} for a web server (e.g. port and path settings). * * @author Dave Syer * @author Stephane Nicoll * @author Andy Wilkinson * @author Ivan Sopov * @author Marcos Barbero * @author Eddú Meléndez * @author Quinten De Swaef * @author Venil Noronha * @author Aurélien Leboulanger * @author Brian Clozel * @author Olivier Lamy */ @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) public class ServerProperties {
1/\*\* 2 \* Server HTTP port. 3 \*/ 4private Integer port; 5 6/\*\* 7 \* Network address to which the server should bind. 8 \*/ 9private InetAddress address; 10 11@NestedConfigurationProperty 12private final ErrorProperties error = new ErrorProperties(); 13 14/\*\* 15 \* Whether X-Forwarded-\* headers should be applied to the HttpRequest. 16 \*/ 17private Boolean useForwardHeaders; 18 19/\*\* 20 \* Value to use for the Server response header (if empty, no header is sent). 21 \*/ 22private String serverHeader; 23 24/\*\* 25 \* Maximum size, in bytes, of the HTTP message header. 26 \*/ 27private int maxHttpHeaderSize = 0; // bytes 28 29/\*\* 30 \* Time that connectors wait for another HTTP request before closing the connection. 31 \* When not set, the connector's container-specific default is used. Use a value of -1 32 \* to indicate no (that is, an infinite) timeout. 33 \*/ 34private Duration connectionTimeout; 35 36@NestedConfigurationProperty 37private Ssl ssl; 38 39@NestedConfigurationProperty 40private final Compression compression = new Compression(); 41 42@NestedConfigurationProperty 43private final Http2 http2 = new Http2(); 44 45private final Servlet servlet = new Servlet(); 46 47private final Tomcat tomcat = new Tomcat(); 48 49private final Jetty jetty = new Jetty(); 50 51private final Undertow undertow = new Undertow(); 52 53public Integer getPort() { 54 return this.port; 55} 56 57public void setPort(Integer port) { 58 this.port = port; 59} 60 61public InetAddress getAddress() { 62 return this.address; 63} 64 65public void setAddress(InetAddress address) { 66 this.address = address; 67} 68 69public Boolean isUseForwardHeaders() { 70 return this.useForwardHeaders; 71} 72 73public void setUseForwardHeaders(Boolean useForwardHeaders) { 74 this.useForwardHeaders = useForwardHeaders; 75} 76 77public String getServerHeader() { 78 return this.serverHeader; 79} 80 81public void setServerHeader(String serverHeader) { 82 this.serverHeader = serverHeader; 83} 84 85public int getMaxHttpHeaderSize() { 86 return this.maxHttpHeaderSize; 87} 88 89public void setMaxHttpHeaderSize(int maxHttpHeaderSize) { 90 this.maxHttpHeaderSize = maxHttpHeaderSize; 91} 92 93public Duration getConnectionTimeout() { 94 return this.connectionTimeout; 95} 96 97public void setConnectionTimeout(Duration connectionTimeout) { 98 this.connectionTimeout = connectionTimeout; 99} 100 101public ErrorProperties getError() { 102 return this.error; 103} 104 105public Ssl getSsl() { 106 return this.ssl; 107} 108 109public void setSsl(Ssl ssl) { 110 this.ssl = ssl; 111} 112 113public Compression getCompression() { 114 return this.compression; 115} 116 117public Http2 getHttp2() { 118 return this.http2; 119} 120 121public Servlet getServlet() { 122 return this.servlet; 123} 124 125public Tomcat getTomcat() { 126 return this.tomcat; 127} 128 129public Jetty getJetty() { 130 return this.jetty; 131} 132 133public Undertow getUndertow() { 134 return this.undertow; 135} 136 137/\*\* 138 \* Servlet properties. 139 \*/ 140public static class Servlet { 141 142 /\*\* 143 \* Servlet context init parameters. 144 \*/ 145 private final Map<String, String> contextParameters = new HashMap<>(); 146 147 /\*\* 148 \* Context path of the application. 149 \*/ 150 private String contextPath; 151 152 /\*\* 153 \* Display name of the application. 154 \*/ 155 private String applicationDisplayName = "application"; 156 157 /\*\* 158 \* Path of the main dispatcher servlet. 159 \*/ 160 private String path = "/"; 161 162 @NestedConfigurationProperty 163 private final Jsp jsp = new Jsp(); 164 165 @NestedConfigurationProperty 166 private final Session session = new Session(); 167 168 public String getContextPath() { 169 return this.contextPath; 170 } 171 172 public void setContextPath(String contextPath) { 173 this.contextPath = cleanContextPath(contextPath); 174 } 175 176 private String cleanContextPath(String contextPath) { 177 if (StringUtils.hasText(contextPath) && contextPath.endsWith("/")) { 178 return contextPath.substring(0, contextPath.length() - 1); 179 } 180 return contextPath; 181 } 182 183 public String getApplicationDisplayName() { 184 return this.applicationDisplayName; 185 } 186 187 public void setApplicationDisplayName(String displayName) { 188 this.applicationDisplayName = displayName; 189 } 190 191 public String getPath() { 192 return this.path; 193 } 194 195 public void setPath(String path) { 196 Assert.notNull(path, "Path must not be null"); 197 this.path = path; 198 } 199 200 public Map<String, String> getContextParameters() { 201 return this.contextParameters; 202 } 203 204 public Jsp getJsp() { 205 return this.jsp; 206 } 207 208 public Session getSession() { 209 return this.session; 210 } 211 212 public String getServletMapping() { 213 if (this.path.equals("") || this.path.equals("/")) { 214 return "/"; 215 } 216 if (this.path.contains("\*")) { 217 return this.path; 218 } 219 if (this.path.endsWith("/")) { 220 return this.path + "\*"; 221 } 222 return this.path + "/\*"; 223 } 224 225 public String getPath(String path) { 226 String prefix = getServletPrefix(); 227 if (!path.startsWith("/")) { 228 path = "/" + path; 229 } 230 return prefix + path; 231 } 232 233 public String getServletPrefix() { 234 String result = this.path; 235 int index = result.indexOf('\*'); 236 if (index != -1) { 237 result = result.substring(0, index); 238 } 239 if (result.endsWith("/")) { 240 result = result.substring(0, result.length() - 1); 241 } 242 return result; 243 } 244 245 public String\[\] getPathsArray(Collection<String> paths) { 246 String\[\] result = new String\[paths.size()\]; 247 int i = 0; 248 for (String path : paths) { 249 result\[i++\] = getPath(path); 250 } 251 return result; 252 } 253 254 public String\[\] getPathsArray(String\[\] paths) { 255 String\[\] result = new String\[paths.length\]; 256 int i = 0; 257 for (String path : paths) { 258 result\[i++\] = getPath(path); 259 } 260 return result; 261 } 262 263} 264 265/\*\* 266 \* Tomcat properties. 267 \*/ 268public static class Tomcat { 269 270 /\*\* 271 \* Access log configuration. 272 \*/ 273 private final Accesslog accesslog = new Accesslog(); 274 275 /\*\* 276 \* Regular expression matching trusted IP addresses. 277 \*/ 278 private String internalProxies = "10\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}|" // 10/8 279 + "192\\\\.168\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}|" // 192.168/16 280 + "169\\\\.254\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}|" // 169.254/16 281 + "127\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}|" // 127/8 282 + "172\\\\.1\[6-9\]{1}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}|" // 172.16/12 283 + "172\\\\.2\[0-9\]{1}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}|" 284 + "172\\\\.3\[0-1\]{1}\\\\.\\\\d{1,3}\\\\.\\\\d{1,3}"; 285 286 /\*\* 287 \* Header that holds the incoming protocol, usually named "X-Forwarded-Proto". 288 \*/ 289 private String protocolHeader; 290 291 /\*\* 292 \* Value of the protocol header indicating whether the incoming request uses SSL. 293 \*/ 294 private String protocolHeaderHttpsValue = "https"; 295 296 /\*\* 297 \* Name of the HTTP header used to override the original port value. 298 \*/ 299 private String portHeader = "X-Forwarded-Port"; 300 301 /\*\* 302 \* Name of the HTTP header from which the remote IP is extracted. For instance, 303 \* \`X-FORWARDED-FOR\`. 304 \*/ 305 private String remoteIpHeader; 306 307 /\*\* 308 \* Tomcat base directory. If not specified, a temporary directory is used. 309 \*/ 310 private File basedir; 311 312 /\*\* 313 \* Delay between the invocation of backgroundProcess methods. If a duration suffix 314 \* is not specified, seconds will be used. 315 \*/ 316 @DurationUnit(ChronoUnit.SECONDS) 317 private Duration backgroundProcessorDelay = Duration.ofSeconds(30); 318 319 /\*\* 320 \* Maximum number of worker threads. 321 \*/ 322 private int maxThreads = 0; 323 324 /\*\* 325 \* Minimum number of worker threads. 326 \*/ 327 private int minSpareThreads = 0; 328 329 /\*\* 330 \* Maximum size, in bytes, of the HTTP post content. 331 \*/ 332 private int maxHttpPostSize = 0; 333 334 /\*\* 335 \* Maximum size, in bytes, of the HTTP message header. 336 \*/ 337 private int maxHttpHeaderSize = 0; 338 339 /\*\* 340 \* Whether requests to the context root should be redirected by appending a / to 341 \* the path. 342 \*/ 343 private Boolean redirectContextRoot; 344 345 /\*\* 346 \* Whether HTTP 1.1 and later location headers generated by a call to sendRedirect 347 \* will use relative or absolute redirects. 348 \*/ 349 private Boolean useRelativeRedirects; 350 351 /\*\* 352 \* Character encoding to use to decode the URI. 353 \*/ 354 private Charset uriEncoding; 355 356 /\*\* 357 \* Maximum number of connections that the server accepts and processes at any 358 \* given time. Once the limit has been reached, the operating system may still 359 \* accept connections based on the "acceptCount" property. 360 \*/ 361 private int maxConnections = 0; 362 363 /\*\* 364 \* Maximum queue length for incoming connection requests when all possible request 365 \* processing threads are in use. 366 \*/ 367 private int acceptCount = 0; 368 369 /\*\* 370 \* Comma-separated list of additional patterns that match jars to ignore for TLD 371 \* scanning. The special '?' and '\*' characters can be used in the pattern to 372 \* match one and only one character and zero or more characters respectively. 373 \*/ 374 private List<String> additionalTldSkipPatterns = new ArrayList<>(); 375 376 /\*\* 377 \* Static resource configuration. 378 \*/ 379 private final Resource resource = new Resource(); 380 381 public int getMaxThreads() { 382 return this.maxThreads; 383 } 384 385 public void setMaxThreads(int maxThreads) { 386 this.maxThreads = maxThreads; 387 } 388 389 public int getMinSpareThreads() { 390 return this.minSpareThreads; 391 } 392 393 public void setMinSpareThreads(int minSpareThreads) { 394 this.minSpareThreads = minSpareThreads; 395 } 396 397 public int getMaxHttpPostSize() { 398 return this.maxHttpPostSize; 399 } 400 401 public void setMaxHttpPostSize(int maxHttpPostSize) { 402 this.maxHttpPostSize = maxHttpPostSize; 403 } 404 405 public Accesslog getAccesslog() { 406 return this.accesslog; 407 } 408 409 public Duration getBackgroundProcessorDelay() { 410 return this.backgroundProcessorDelay; 411 } 412 413 public void setBackgroundProcessorDelay(Duration backgroundProcessorDelay) { 414 this.backgroundProcessorDelay = backgroundProcessorDelay; 415 } 416 417 public File getBasedir() { 418 return this.basedir; 419 } 420 421 public void setBasedir(File basedir) { 422 this.basedir = basedir; 423 } 424 425 public String getInternalProxies() { 426 return this.internalProxies; 427 } 428 429 public void setInternalProxies(String internalProxies) { 430 this.internalProxies = internalProxies; 431 } 432 433 public String getProtocolHeader() { 434 return this.protocolHeader; 435 } 436 437 public void setProtocolHeader(String protocolHeader) { 438 this.protocolHeader = protocolHeader; 439 } 440 441 public String getProtocolHeaderHttpsValue() { 442 return this.protocolHeaderHttpsValue; 443 } 444 445 public void setProtocolHeaderHttpsValue(String protocolHeaderHttpsValue) { 446 this.protocolHeaderHttpsValue = protocolHeaderHttpsValue; 447 } 448 449 public String getPortHeader() { 450 return this.portHeader; 451 } 452 453 public void setPortHeader(String portHeader) { 454 this.portHeader = portHeader; 455 } 456 457 public Boolean getRedirectContextRoot() { 458 return this.redirectContextRoot; 459 } 460 461 public void setRedirectContextRoot(Boolean redirectContextRoot) { 462 this.redirectContextRoot = redirectContextRoot; 463 } 464 465 public Boolean getUseRelativeRedirects() { 466 return this.useRelativeRedirects; 467 } 468 469 public void setUseRelativeRedirects(Boolean useRelativeRedirects) { 470 this.useRelativeRedirects = useRelativeRedirects; 471 } 472 473 public String getRemoteIpHeader() { 474 return this.remoteIpHeader; 475 } 476 477 public void setRemoteIpHeader(String remoteIpHeader) { 478 this.remoteIpHeader = remoteIpHeader; 479 } 480 481 public Charset getUriEncoding() { 482 return this.uriEncoding; 483 } 484 485 public void setUriEncoding(Charset uriEncoding) { 486 this.uriEncoding = uriEncoding; 487 } 488 489 public int getMaxConnections() { 490 return this.maxConnections; 491 } 492 493 public void setMaxConnections(int maxConnections) { 494 this.maxConnections = maxConnections; 495 } 496 497 public int getMaxHttpHeaderSize() { 498 return this.maxHttpHeaderSize; 499 } 500 501 public void setMaxHttpHeaderSize(int maxHttpHeaderSize) { 502 this.maxHttpHeaderSize = maxHttpHeaderSize; 503 } 504 505 public int getAcceptCount() { 506 return this.acceptCount; 507 } 508 509 public void setAcceptCount(int acceptCount) { 510 this.acceptCount = acceptCount; 511 } 512 513 public List<String> getAdditionalTldSkipPatterns() { 514 return this.additionalTldSkipPatterns; 515 } 516 517 public void setAdditionalTldSkipPatterns(List<String> additionalTldSkipPatterns) { 518 this.additionalTldSkipPatterns = additionalTldSkipPatterns; 519 } 520 521 public Resource getResource() { 522 return this.resource; 523 } 524 525 /\*\* 526 \* Tomcat access log properties. 527 \*/ 528 public static class Accesslog { 529 530 /\*\* 531 \* Enable access log. 532 \*/ 533 private boolean enabled = false; 534 535 /\*\* 536 \* Format pattern for access logs. 537 \*/ 538 private String pattern = "common"; 539 540 /\*\* 541 \* Directory in which log files are created. Can be absolute or relative to 542 \* the Tomcat base dir. 543 \*/ 544 private String directory = "logs"; 545 546 /\*\* 547 \* Log file name prefix. 548 \*/ 549 protected String prefix = "access\_log"; 550 551 /\*\* 552 \* Log file name suffix. 553 \*/ 554 private String suffix = ".log"; 555 556 /\*\* 557 \* Whether to enable access log rotation. 558 \*/ 559 private boolean rotate = true; 560 561 /\*\* 562 \* Whether to defer inclusion of the date stamp in the file name until rotate 563 \* time. 564 \*/ 565 private boolean renameOnRotate; 566 567 /\*\* 568 \* Date format to place in the log file name. 569 \*/ 570 private String fileDateFormat = ".yyyy-MM-dd"; 571 572 /\*\* 573 \* Set request attributes for the IP address, Hostname, protocol, and port 574 \* used for the request. 575 \*/ 576 private boolean requestAttributesEnabled; 577 578 /\*\* 579 \* Whether to buffer output such that it is flushed only periodically. 580 \*/ 581 private boolean buffered = true; 582 583 public boolean isEnabled() { 584 return this.enabled; 585 } 586 587 public void setEnabled(boolean enabled) { 588 this.enabled = enabled; 589 } 590 591 public String getPattern() { 592 return this.pattern; 593 } 594 595 public void setPattern(String pattern) { 596 this.pattern = pattern; 597 } 598 599 public String getDirectory() { 600 return this.directory; 601 } 602 603 public void setDirectory(String directory) { 604 this.directory = directory; 605 } 606 607 public String getPrefix() { 608 return this.prefix; 609 } 610 611 public void setPrefix(String prefix) { 612 this.prefix = prefix; 613 } 614 615 public String getSuffix() { 616 return this.suffix; 617 } 618 619 public void setSuffix(String suffix) { 620 this.suffix = suffix; 621 } 622 623 public boolean isRotate() { 624 return this.rotate; 625 } 626 627 public void setRotate(boolean rotate) { 628 this.rotate = rotate; 629 } 630 631 public boolean isRenameOnRotate() { 632 return this.renameOnRotate; 633 } 634 635 public void setRenameOnRotate(boolean renameOnRotate) { 636 this.renameOnRotate = renameOnRotate; 637 } 638 639 public String getFileDateFormat() { 640 return this.fileDateFormat; 641 } 642 643 public void setFileDateFormat(String fileDateFormat) { 644 this.fileDateFormat = fileDateFormat; 645 } 646 647 public boolean isRequestAttributesEnabled() { 648 return this.requestAttributesEnabled; 649 } 650 651 public void setRequestAttributesEnabled(boolean requestAttributesEnabled) { 652 this.requestAttributesEnabled = requestAttributesEnabled; 653 } 654 655 public boolean isBuffered() { 656 return this.buffered; 657 } 658 659 public void setBuffered(boolean buffered) { 660 this.buffered = buffered; 661 } 662 663 } 664 665 /\*\* 666 \* Tomcat static resource properties. 667 \*/ 668 public static class Resource { 669 670 /\*\* 671 \* Time-to-live of the static resource cache. 672 \*/ 673 private Duration cacheTtl; 674 675 public Duration getCacheTtl() { 676 return this.cacheTtl; 677 } 678 679 public void setCacheTtl(Duration cacheTtl) { 680 this.cacheTtl = cacheTtl; 681 } 682 683 } 684 685} 686 687/\*\* 688 \* Jetty properties. 689 \*/ 690public static class Jetty { 691 692 /\*\* 693 \* Access log configuration. 694 \*/ 695 private final Accesslog accesslog = new Accesslog(); 696 697 /\*\* 698 \* Maximum size, in bytes, of the HTTP post or put content. 699 \*/ 700 private int maxHttpPostSize = 0; // bytes 701 702 /\*\* 703 \* Number of acceptor threads to use. 704 \*/ 705 private Integer acceptors; 706 707 /\*\* 708 \* Number of selector threads to use. 709 \*/ 710 private Integer selectors; 711 712 public Accesslog getAccesslog() { 713 return this.accesslog; 714 } 715 716 public int getMaxHttpPostSize() { 717 return this.maxHttpPostSize; 718 } 719 720 public void setMaxHttpPostSize(int maxHttpPostSize) { 721 this.maxHttpPostSize = maxHttpPostSize; 722 } 723 724 public Integer getAcceptors() { 725 return this.acceptors; 726 } 727 728 public void setAcceptors(Integer acceptors) { 729 this.acceptors = acceptors; 730 } 731 732 public Integer getSelectors() { 733 return this.selectors; 734 } 735 736 public void setSelectors(Integer selectors) { 737 this.selectors = selectors; 738 } 739 740 /\*\* 741 \* Jetty access log properties. 742 \*/ 743 public static class Accesslog { 744 745 /\*\* 746 \* Enable access log. 747 \*/ 748 private boolean enabled = false; 749 750 /\*\* 751 \* Log filename. If not specified, logs redirect to "System.err". 752 \*/ 753 private String filename; 754 755 /\*\* 756 \* Date format to place in log file name. 757 \*/ 758 private String fileDateFormat; 759 760 /\*\* 761 \* Number of days before rotated log files are deleted. 762 \*/ 763 private int retentionPeriod = 31; // no days 764 765 /\*\* 766 \* Append to log. 767 \*/ 768 private boolean append; 769 770 /\*\* 771 \* Enable extended NCSA format. 772 \*/ 773 private boolean extendedFormat; 774 775 /\*\* 776 \* Timestamp format of the request log. 777 \*/ 778 private String dateFormat = "dd/MMM/yyyy:HH:mm:ss Z"; 779 780 /\*\* 781 \* Locale of the request log. 782 \*/ 783 private Locale locale; 784 785 /\*\* 786 \* Timezone of the request log. 787 \*/ 788 private TimeZone timeZone = TimeZone.getTimeZone("GMT"); 789 790 /\*\* 791 \* Enable logging of the request cookies. 792 \*/ 793 private boolean logCookies; 794 795 /\*\* 796 \* Enable logging of the request hostname. 797 \*/ 798 private boolean logServer; 799 800 /\*\* 801 \* Enable logging of request processing time. 802 \*/ 803 private boolean logLatency; 804 805 public boolean isEnabled() { 806 return this.enabled; 807 } 808 809 public void setEnabled(boolean enabled) { 810 this.enabled = enabled; 811 } 812 813 public String getFilename() { 814 return this.filename; 815 } 816 817 public void setFilename(String filename) { 818 this.filename = filename; 819 } 820 821 public String getFileDateFormat() { 822 return this.fileDateFormat; 823 } 824 825 public void setFileDateFormat(String fileDateFormat) { 826 this.fileDateFormat = fileDateFormat; 827 } 828 829 public int getRetentionPeriod() { 830 return this.retentionPeriod; 831 } 832 833 public void setRetentionPeriod(int retentionPeriod) { 834 this.retentionPeriod = retentionPeriod; 835 } 836 837 public boolean isAppend() { 838 return this.append; 839 } 840 841 public void setAppend(boolean append) { 842 this.append = append; 843 } 844 845 public boolean isExtendedFormat() { 846 return this.extendedFormat; 847 } 848 849 public void setExtendedFormat(boolean extendedFormat) { 850 this.extendedFormat = extendedFormat; 851 } 852 853 public String getDateFormat() { 854 return this.dateFormat; 855 } 856 857 public void setDateFormat(String dateFormat) { 858 this.dateFormat = dateFormat; 859 } 860 861 public Locale getLocale() { 862 return this.locale; 863 } 864 865 public void setLocale(Locale locale) { 866 this.locale = locale; 867 } 868 869 public TimeZone getTimeZone() { 870 return this.timeZone; 871 } 872 873 public void setTimeZone(TimeZone timeZone) { 874 this.timeZone = timeZone; 875 } 876 877 public boolean isLogCookies() { 878 return this.logCookies; 879 } 880 881 public void setLogCookies(boolean logCookies) { 882 this.logCookies = logCookies; 883 } 884 885 public boolean isLogServer() { 886 return this.logServer; 887 } 888 889 public void setLogServer(boolean logServer) { 890 this.logServer = logServer; 891 } 892 893 public boolean isLogLatency() { 894 return this.logLatency; 895 } 896 897 public void setLogLatency(boolean logLatency) { 898 this.logLatency = logLatency; 899 } 900 } 901 902} 903 904/\*\* 905 \* Undertow properties. 906 \*/ 907public static class Undertow { 908 909 /\*\* 910 \* Maximum size, in bytes, of the HTTP post content. 911 \*/ 912 private long maxHttpPostSize = 0; // bytes 913 914 /\*\* 915 \* Size of each buffer, in bytes. 916 \*/ 917 private Integer bufferSize; 918 919 /\*\* 920 \* Number of I/O threads to create for the worker. 921 \*/ 922 private Integer ioThreads; 923 924 /\*\* 925 \* Number of worker threads. 926 \*/ 927 private Integer workerThreads; 928 929 /\*\* 930 \* Whether to allocate buffers outside the Java heap. 931 \*/ 932 private Boolean directBuffers; 933 934 /\*\* 935 \* Whether servlet filters should be initialized on startup. 936 \*/ 937 private boolean eagerFilterInit = true; 938 939 private final Accesslog accesslog = new Accesslog(); 940 941 public long getMaxHttpPostSize() { 942 return this.maxHttpPostSize; 943 } 944 945 public void setMaxHttpPostSize(long maxHttpPostSize) { 946 this.maxHttpPostSize = maxHttpPostSize; 947 } 948 949 public Integer getBufferSize() { 950 return this.bufferSize; 951 } 952 953 public void setBufferSize(Integer bufferSize) { 954 this.bufferSize = bufferSize; 955 } 956 957 public Integer getIoThreads() { 958 return this.ioThreads; 959 } 960 961 public void setIoThreads(Integer ioThreads) { 962 this.ioThreads = ioThreads; 963 } 964 965 public Integer getWorkerThreads() { 966 return this.workerThreads; 967 } 968 969 public void setWorkerThreads(Integer workerThreads) { 970 this.workerThreads = workerThreads; 971 } 972 973 public Boolean getDirectBuffers() { 974 return this.directBuffers; 975 } 976 977 public void setDirectBuffers(Boolean directBuffers) { 978 this.directBuffers = directBuffers; 979 } 980 981 public boolean isEagerFilterInit() { 982 return this.eagerFilterInit; 983 } 984 985 public void setEagerFilterInit(boolean eagerFilterInit) { 986 this.eagerFilterInit = eagerFilterInit; 987 } 988 989 public Accesslog getAccesslog() { 990 return this.accesslog; 991 } 992 993 /\*\* 994 \* Undertow access log properties. 995 \*/ 996 public static class Accesslog { 997 998 /\*\* 999 \* Whether to enable the access log. 1000 \*/ 1001 private boolean enabled = false; 1002 1003 /\*\* 1004 \* Format pattern for access logs. 1005 \*/ 1006 private String pattern = "common"; 1007 1008 /\*\* 1009 \* Log file name prefix. 1010 \*/ 1011 protected String prefix = "access\_log."; 1012 1013 /\*\* 1014 \* Log file name suffix. 1015 \*/ 1016 private String suffix = "log"; 1017 1018 /\*\* 1019 \* Undertow access log directory. 1020 \*/ 1021 private File dir = new File("logs"); 1022 1023 /\*\* 1024 \* Whether to enable access log rotation. 1025 \*/ 1026 private boolean rotate = true; 1027 1028 public boolean isEnabled() { 1029 return this.enabled; 1030 } 1031 1032 public void setEnabled(boolean enabled) { 1033 this.enabled = enabled; 1034 } 1035 1036 public String getPattern() { 1037 return this.pattern; 1038 } 1039 1040 public void setPattern(String pattern) { 1041 this.pattern = pattern; 1042 } 1043 1044 public String getPrefix() { 1045 return this.prefix; 1046 } 1047 1048 public void setPrefix(String prefix) { 1049 this.prefix = prefix; 1050 } 1051 1052 public String getSuffix() { 1053 return this.suffix; 1054 } 1055 1056 public void setSuffix(String suffix) { 1057 this.suffix = suffix; 1058 } 1059 1060 public File getDir() { 1061 return this.dir; 1062 } 1063 1064 public void setDir(File dir) { 1065 this.dir = dir; 1066 } 1067 1068 public boolean isRotate() { 1069 return this.rotate; 1070 } 1071 1072 public void setRotate(boolean rotate) { 1073 this.rotate = rotate; 1074 } 1075 1076 } 1077 1078}
}
- @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
表示配置属性是server开头 忽略不知道的属性