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,viewer
Spring Security でアプリケーションを保護する方法をセットアップするには、security config クラスを定義する必要があります。
@EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(authorize -> authorize .anyRequest().authenticated() ) .formLogin(withDefaults()) .httpBasic(withDefaults()) .csrf(csrf -> csrf .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .csrfTokenRequestHandler(new SpaCsrfTokenRequestHandler()) ) .addFilterAfter(new CsrfCookieFilter(), BasicAuthenticationFilter.class); return http.build(); } }
注記CsrfAuthenticationStrategy
とCsrfLogoutHandler
によって以前のトークンが消去されるため、認証成功およびログアウト成功後にトークンを更新する必要があります。クライアントアプリケーションは、新しいトークンを取得せずに、POST などの安全でない HTTP 要求を実行できません。
以下に例を示します。
作業例は、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 インテグレーション - Spring Boot を参照してください。