4.3. Spring Boot
標準の JAAS 認証に加えて、Spring Boot の HawtIO は Spring Security または Keycloak を使用して保護できます。Spring Boot の HawtIO 認証を無効にする場合は、次の設定を application.properties に追加します。
hawtio.authenticationEnabled = false
4.3.1. Spring Security リンクのコピーリンクがクリップボードにコピーされました!
HawtIO で Spring Security を使用するには、以下の手順を実行します。
org.springframework.boot:spring-boot-starter-securityをpom.xmlの依存関係に追加します。<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>src/main/resources/application.propertiesの Spring Security 設定は次のようになります。spring.security.user.name = hawtio spring.security.user.password = s3cr3t! spring.security.user.roles = admin,viewerSpring Security でアプリケーションを保護する方法をセットアップするには、security config クラスを定義する必要があります。
@EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.authorizeRequests().anyRequest().authenticated() .and() .formLogin() .and() .httpBasic() .and() .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()); return http.build(); } }
以下に例を示します。
実際に動作する例については、springboot-security example を参照してください。
4.3.1.1. Spring Security を使用したリモートアプリケーションへの接続 リンクのコピーリンクがクリップボードにコピーされました!
Spring Security を有効にしてリモート Spring Boot アプリケーションに接続しようとする場合は、Spring Security 設定で HawtIO コンソールからのアクセスが許可されていることを確認してください。デフォルトの CSRF 保護により Jolokia エンドポイントへのリモートアクセスが禁止されている可能性が高く、その場合は HawtIO コンソールで認証エラーが発生します。
アプリケーションが CSRF 攻撃のリスクにさらされることに注意してください。
最も簡単な解決策は、次のようにリモートアプリケーションで Jolokia エンドポイントの CSRF 保護を無効にすることです。
import org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaEndpoint; import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest; @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { ... // Disable CSRF protection for the Jolokia endpoint http.csrf().ignoringRequestMatchers(EndpointRequest.to(JolokiaEndpoint.class)); return http.build(); } }Spring Security の CSRF 保護を使用せずに Jolokia エンドポイントを保護するには、以下のような
jolokia-access.xmlファイル (スニペット) をsrc/main/resources/に配置する必要があります。これにより、信頼されたノードのみがファイルにアクセスできるようになります。<restrict> ... <cors> <allow-origin>http*://localhost:*</allow-origin> <allow-origin>http*://127.0.0.1:*</allow-origin> <allow-origin>http*://*.example.com</allow-origin> <allow-origin>http*://*.example.com:*</allow-origin> <strict-checking /> </cors> </restrict>
4.3.2. Keycloak を使用した Spring Boot リンクのコピーリンクがクリップボードにコピーされました!
Keycloak Integration - Spring Boot を参照してください。