6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring Boot の Jetty がバージョン情報を晒しているので止めさせたい

Posted at

意外と面倒だったのでメモ。

Spring Boot のサーブレットコンテナに Jetty を使っていると、レスポンスヘッダにバッチリ Jetty のバージョンが載ってくる:

$ curl -o/dev/null -sv http://localhost:8080/foo 2>&1 | grep '^< Server:'
< Server: Jetty(9.2.11.v20150529)

これを止めさせるには自動で Jetty サーバを作ってるロジックに、下記のような手を加える必要がある:

Config.java
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = Config.class)
public class Config {
    /**
     * レスポンスヘッダ Server 付与する機能を潰す設定
     */
    @Bean
    public JettyServerCustomizer jettyServerNameSuppresser() {
        return server -> {
            Stream.of(server.getConnectors())
                    .flatMap(c -> c.getConnectionFactories().stream())
                    .flatMap(f -> (f instanceof HttpConnectionFactory) ? Stream.of((HttpConnectionFactory) f)
                            : Stream.empty())
                    .forEach(f -> f.getHttpConfiguration().setSendServerVersion(false));
        };
    }

    /**
     * デフォルトの Jetty サーバ作成クラスから、customizers でサーバに手を加える
     */
    @Bean
    public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory(
            JettyServerCustomizer[] customizers) {
        JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory();
        factory.addServerCustomizers(customizers);
        return factory;
    }
}

起動してアクセスしてみる:

$ curl -o/dev/null -sv http://localhost:8080/foo 2>&1 | grep '^< Server:'
$ echo $?
1

Server ヘッダが消えているのが分かる。

「設定ファイルでちょっと設定すればOK!!」だと思っていたのにあてが外れて、思いの外苦労した。

参考: 64. Embedded servlet containers

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?