5.4. リアクティブ Spring Boot WebFlux アプリケーションで Basic 認証の使用

Spring Security および WebFlux スターターを使用して、リアクティブ Hello World HTTP Web サービスを作成し、基本的なフォームベースの認証を作成します。

前提条件

手順

  1. vertx-spring-boot-starter-http および spring-boot-starter-security をプロジェクトの pom.xml ファイルに依存関係として追加します。

    pom.xml

    <project>
    ...
      <dependencies>
      ...
        <dependency>
          <groupId>dev.snowdrop</groupId>
          <artifactId>vertx-spring-boot-starter-http</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
      ...
      <dependencies>
    ...
    </project>

  2. アプリケーションのエンドポイントコントローラークラスを作成します。

    HelloController.java

    package dev.snowdrop.vertx.sample.http.security;
    
    import java.security.Principal;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import reactor.core.publisher.Mono;
    
    @RestController
    public class HelloController {
    
        @GetMapping("/")
        public Mono<String> hello(Mono<Principal> principal) {
            return principal
                .map(Principal::getName)
                .map(this::helloMessage);
        }
    
        private String helloMessage(String username) {
            return "Hello, " + username + "!";
        }
    }

  3. アプリケーションのメインクラスを作成します。

    HttpSecuritySampleApplication.java

    package dev.snowdrop.vertx.sample.http.security;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class HttpSecuritySampleApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(HttpSecuritySampleApplication.class, args);
        }
    }

  4. /hello エンドポイントにアクセスするためのユーザー認証情報を格納する SecurityConfiguration クラスを作成します。

    SecurityConfiguration.java

    package dev.snowdrop.vertx.sample.http.security;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
    import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
    import org.springframework.security.core.userdetails.User;
    import org.springframework.security.core.userdetails.UserDetails;
    
    @EnableWebFluxSecurity
    public class SecurityConfiguration {
    
        @Bean
        public MapReactiveUserDetailsService userDetailsService() {
            UserDetails user = User.withDefaultPasswordEncoder()
                .username("user")
                .password("user")
                .roles("USER")
                .build();
    
            return new MapReactiveUserDetailsService(user);
        }
    }

  5. オプション: アプリケーションをローカルで実行し、テストします。

    1. Maven プロジェクトのルートディレクトリーへ移動します。

      $ cd myApp
    2. アプリケーションをパッケージ化します。

      $ mvn clean package
    3. コマンドラインからアプリケーションを起動します。

      $ java -jar target/vertx-spring-boot-sample-http-security.jar
    4. ブラウザーを使用して http://localhost:8080 に移動し、ログイン画面にアクセスします。
    5. 以下の認証情報を使用してログインします。

      • username: user
      • password: user

      以下のログイン時に、カスタマイズされた挨拶文を受け取ります。

      Hello, user!
    6. Web ブラウザーを使用して http://localhost:8080/logout に移動し、Log out ボタンを使用してアプリケーションからログアウトします。
    7. または、ターミナルを使用して localhost:8080 で認証されていない HTTP 要求を実行します。アプリケーションから HTTP 401 Unauthorized 応答を受信します。

      $ curl -I http://localhost:8080
      HTTP/1.1 401 Unauthorized
      WWW-Authenticate: Basic realm="Realm"
      Cache-Control: no-cache, no-store, max-age=0, must-revalidate
      Pragma: no-cache
      Expires: 0
      X-Content-Type-Options: nosniff
      X-Frame-Options: DENY
      X-XSS-Protection: 1 ; mode=block
      Referrer-Policy: no-referrer
    8. サンプルユーザー認証情報を使用して認証された要求を発行します。パーソナルな応答を受け取ります。

      $ curl -u user:user http://localhost:8080
      Hello, user!

その他のリソース