SpringBootActuatorですべてのエンドポイントを有効にする方法
1. 概要
このチュートリアルでは、Spring Bootアクチュエーターですべてのエンドポイントを有効にする方法を学習します。必要なMavenの依存関係から始めます。 そこから、プロパティファイルを介してエンドポイントを制御する方法を見ていきます。 最後に、エンドポイントを保護する方法の概要を説明します。
SpringBoot1.xとSpringBoot2.x の間で、アクチュエータのエンドポイントの構成方法に関していくつかの変更がありました。 それらが出てきたら、これらに注意します。
2. 設定
アクチュエーターを使用するには、Maven構成にspring-boot-starter-actuatorを含める必要があります。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.5.1</version>
</dependency>
さらに、Spring Boot 2.0以降、エンドポイントをHTTP 経由で公開する場合は、Webスターターを含める必要があります。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.1</version>
</dependency>
3. エンドポイントの有効化と公開
Spring Boot 2 から、エンドポイントを有効にして公開する必要があります。 デフォルトでは、 / shutdown を除くすべてのエンドポイントが有効になり、 /healthと/infoのみが公開されます。 アプリケーションに別のルートコンテキストを構成した場合でも、すべてのエンドポイントは/アクチュエータにあります。
つまり、Maven構成に適切なスターターを追加すると、 http:// localhost:8080/の/healthおよび/infoエンドポイントにアクセスできるようになります。アクチュエータ/ヘルスおよびhttp:// localhost:8080 / actuator /info。
http:// localhost:8080 / actuator に移動して、アクチュエータのエンドポイントがHATEOSに対応しているため、使用可能なエンドポイントのリストを表示してみましょう。 /healthと/infoが表示されます。
{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},
"health":{"href":"http://localhost:8080/actuator/health","templated":false},
"info":{"href":"http://localhost:8080/actuator/info","templated":false}}}
3.1. すべてのエンドポイントを公開する
次に、 application.properties ファイルを変更して、 /shutdownを除くすべてのエンドポイントを公開しましょう。
management.endpoints.web.exposure.include=*
サーバーを再起動して/アクチュエータエンドポイントに再度アクセスすると、 / shutdown:を除く他のエンドポイントが使用可能になります。
{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},
"beans":{"href":"http://localhost:8080/actuator/beans","templated":false},
"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},
"health":{"href":"http://localhost:8080/actuator/health","templated":false},
"info":{"href":"http://localhost:8080/actuator/info","templated":false},
"conditions":{"href":"http://localhost:8080/actuator/conditions","templated":false},
"configprops":{"href":"http://localhost:8080/actuator/configprops","templated":false},
"env":{"href":"http://localhost:8080/actuator/env","templated":false},
"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false},
"heapdump":{"href":"http://localhost:8080/actuator/heapdump","templated":false},
"threaddump":{"href":"http://localhost:8080/actuator/threaddump","templated":false},
"metrics":{"href":"http://localhost:8080/actuator/metrics","templated":false},
"scheduledtasks":{"href":"http://localhost:8080/actuator/scheduledtasks","templated":false},
"mappings":{"href":"http://localhost:8080/actuator/mappings","templated":false}}}
3.2. 特定のエンドポイントの公開
一部のエンドポイントは機密データを公開する可能性があるため、公開するエンドポイントについてより詳細に調べる方法を学びましょう。
management.endpoints.web.exposure.include プロパティは、エンドポイントのコンマ区切りリストを取得することもできます。 したがって、 /beansと/loggersのみを公開しましょう。
management.endpoints.web.exposure.include=beans, loggers
プロパティに特定のエンドポイントを含めることに加えて、エンドポイントを除外することもできます。 /threaddumpを除くすべてのエンドポイントを公開しましょう。
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=threaddump
includeプロパティとexcludeプロパティの両方が、エンドポイントのリストを取得します。 excludeプロパティはincludeよりも優先されます。
3.3. 特定のエンドポイントの有効化
次に、有効にしたエンドポイントについてよりきめ細かくする方法を学びましょう。
まず、すべてのエンドポイントを有効にするデフォルトをオフにする必要があります。
management.endpoints.enabled-by-default=false
次に、 /healthエンドポイントのみを有効にして公開しましょう。
management.endpoint.health.enabled=true
management.endpoints.web.exposure.include=health
この構成では、 /healthエンドポイントにのみアクセスできます。
3.4. シャットダウンの有効化
その機密性のため、/shutdownエンドポイントはデフォルトで無効になっています。
application.properties ファイルに行を追加して、これを有効にしましょう。
management.endpoint.shutdown.enabled=true
ここで、 /アクチュエータエンドポイントを照会すると、リストされているはずです。 / shutdownエンドポイントはPOSTリクエストのみを受け入れるので、アプリケーションを適切にシャットダウンしましょう。
curl -X POST http://localhost:8080/actuator/shutdown
4. エンドポイントの保護
実際のアプリケーションでは、アプリケーションにセキュリティが設定されている可能性があります。 それを念頭に置いて、アクチュエータのエンドポイントを保護しましょう。
まず、セキュリティスターターMaven依存関係を追加して、アプリケーションにセキュリティを追加しましょう。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>2.5.1</version>
</dependency>
最も基本的なセキュリティについては、それが私たちがしなければならないすべてです。 セキュリティスターターを追加するだけで、 /infoと/healthを除くすべての公開されたエンドポイントに基本認証が自動的に適用されます。
次に、セキュリティをカスタマイズして、 /actuatorエンドポイントをADMINロールに制限しましょう。
デフォルトのセキュリティ構成を除外することから始めましょう。
@SpringBootApplication(exclude = {
SecurityAutoConfiguration.class,
ManagementWebSecurityAutoConfiguration.class
})
ManagementWebSecurityAutoConfiguration.class に注意してください。これにより、独自のセキュリティ構成を/アクチュエータに適用できるようになります。
構成クラスで、いくつかのユーザーとロールを構成して、ADMINロールを操作できるようにします。
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
auth
.inMemoryAuthentication()
.withUser("user")
.password(encoder.encode("password"))
.roles("USER")
.and()
.withUser("admin")
.password(encoder.encode("admin"))
.roles("USER", "ADMIN");
}
SpringBootは、アクチュエータエンドポイントに使用する便利なリクエストマッチャーを提供します。
これを使用して、/アクチュエータをADMINロールのみにロックダウンしましょう。
http.requestMatcher(EndpointRequest.toAnyEndpoint())
.authorizeRequests((requests) -> requests.anyRequest().hasRole("ADMIN"));
5. 結論
このチュートリアルでは、SpringBootがデフォルトでアクチュエータを構成する方法を学びました。 その後、 application.properties ファイルで有効、無効、公開するエンドポイントをカスタマイズしました。 SpringBootはデフォルトで/shutdown エンドポイントを異なる方法で構成するため、個別に有効にする方法を学びました。
基本を学んだ後、アクチュエータのセキュリティを設定する方法を学びました。
いつものように、サンプルコードはGitHubでから入手できます。