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 を使用するには、以下の手順を実行します。

  1. org.springframework.boot:spring-boot-starter-securitypom.xml の依存関係に追加します。

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  2. src/main/resources/application.properties の Spring Security 設定は次のようになります。

    spring.security.user.name = hawtio
    spring.security.user.password = s3cr3t!
    spring.security.user.roles = admin,viewer
  3. 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();
        }
    }
    注記

    CsrfAuthenticationStrategyCsrfLogoutHandler によって以前のトークンが消去されるため、認証成功およびログアウト成功後にトークンを更新する必要があります。クライアントアプリケーションは、新しいトークンを取得せずに、POST などの安全でない HTTP 要求を実行できません。

以下に例を示します。

作業例は、springboot-security example を参照してください。

4.3.1.1. Spring Security を使用したリモートアプリケーションへの接続

Spring Security を有効にしてリモート Spring Boot アプリケーションに接続しようとする場合は、Spring Security 設定で HawtIO コンソールからのアクセスが許可されていることを確認してください。デフォルトの CSRF 保護により Jolokia エンドポイントへのリモートアクセスが禁止されている可能性が高く、その場合は HawtIO コンソールで認証エラーが発生します。

警告

アプリケーションが CSRF 攻撃のリスクにさらされることに注意してください。

  1. 最も簡単な解決策は、次のようにリモートアプリケーションで 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();
        }
    
    }
  2. 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 を参照してください。

Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.