Skip to content

Commit 9a82682

Browse files
committed
Configure Jetty to hide server version and fix remote IP logging when working behind nginx.
1 parent 050bbb3 commit 9a82682

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

src/main/config/nginx/mystamps.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ server {
1616

1717
location / {
1818
error_page 502 =503 @maintenance;
19+
proxy_set_header X-Forwarded-For $remote_addr;
1920
proxy_pass http://127.0.0.1:8080;
2021
}
2122

src/main/java/ru/mystamps/web/support/spring/boot/ApplicationBootstrap.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
H2Config.class,
4646
ThymeleafViewResolverInitializingBean.class,
4747
ResourceBundleMessageSourceInitializingBean.class,
48+
JettyServletContainerCustomizer.class,
4849
ErrorPagesServletContainerCustomizer.class
4950
})
5051
public class ApplicationBootstrap {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (C) 2009-2015 Slava Semushin <slava.semushin@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.support.spring.boot;
19+
20+
import org.eclipse.jetty.server.Connector;
21+
import org.eclipse.jetty.server.ForwardedRequestCustomizer;
22+
import org.eclipse.jetty.server.HttpConfiguration;
23+
import org.eclipse.jetty.server.HttpConnectionFactory;
24+
import org.eclipse.jetty.server.Server;
25+
import org.eclipse.jetty.server.ServerConnector;
26+
27+
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
28+
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
29+
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
30+
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
31+
import org.springframework.context.annotation.Configuration;
32+
33+
@Configuration
34+
public class JettyServletContainerCustomizer implements EmbeddedServletContainerCustomizer {
35+
36+
private static final JettyServerCustomizer JETTY_CUSTOMIZER = new JettyServerCustomizer() {
37+
@Override
38+
public void customize(Server server) {
39+
for (Connector connector : server.getConnectors()) {
40+
if (connector instanceof ServerConnector) {
41+
HttpConnectionFactory connectionFactory =
42+
connector.getConnectionFactory(HttpConnectionFactory.class);
43+
if (connectionFactory != null) {
44+
HttpConfiguration httpConfiguration =
45+
connectionFactory.getHttpConfiguration();
46+
if (httpConfiguration != null) {
47+
httpConfiguration.setSendServerVersion(false);
48+
httpConfiguration.addCustomizer(new ForwardedRequestCustomizer());
49+
}
50+
}
51+
}
52+
}
53+
}
54+
};
55+
56+
@Override
57+
public void customize(ConfigurableEmbeddedServletContainer container) {
58+
if (container instanceof JettyEmbeddedServletContainerFactory) {
59+
JettyEmbeddedServletContainerFactory jetty =
60+
(JettyEmbeddedServletContainerFactory)container;
61+
jetty.addServerCustomizers(JETTY_CUSTOMIZER);
62+
}
63+
}
64+
65+
}

0 commit comments

Comments
 (0)