5.5. リアクティブ Spring Boot アプリケーションで OAuth2 認証の使用。

リアクティブ Spring Boot アプリケーションの OAuth2 認証 を設定し、クライアント ID およびクライアントシークレットを使用して認証します。

前提条件

  • JDK 8 または JDK 11 がインストールされている。
  • GitHub アカウント
  • Maven がインストールされている。
  • Spring Boot を使用するよう設定されたMaven ベースのアプリケーションプロジェクト

手順

  1. Github アカウントに 新規 OAuth 2 アプリケーションを登録 します。登録フォームに以下の値を指定するようにしてください。

  2. プロジェクトの pom.xml ファイルに以下の依存関係を追加します。

    • vertx-spring-boot-starter-http
    • spring-boot-starter-security
    • spring-boot-starter-oauth2-client
    • reactor-netty

      spring-boot-starter-oauth2-client が適切に動作するには、reactor-netty クライアントが必要です。

      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>
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
          </dependency>
          <!-- Spring OAuth2 client only works with Reactor Netty client -->
          <dependency>
            <groupId>io.projectreactor.netty</groupId>
            <artifactId>reactor-netty</artifactId>
          </dependency>
        ...
        <dependencies>
      ...
      </project>

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

    HelloController.java

    package dev.snowdrop.vertx.sample.http.oauth;
    
    import org.springframework.security.core.annotation.AuthenticationPrincipal;
    import org.springframework.security.oauth2.core.user.OAuth2User;
    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(@AuthenticationPrincipal OAuth2User oauth2User) {
            return Mono.just("Hello, " + oauth2User.getAttributes().get("name") + "!");
        }
    }

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

    OAuthSampleApplication.java

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

  5. YAML 設定ファイルを作成して、アプリケーション登録時に GitHub から受け取った OAuth2 クライアント ID およびクライアントシークレットを保存します。

    src/main/resources/application.yml

    spring:
      security:
        oauth2:
          client:
            registration:
              github:
                client-id: YOUR_GITHUB_CLIENT_ID
                client-secret: YOUR_GITHUB_CLIENT_SECRET

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

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

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

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

      $ java -jar target/vertx-spring-boot-sample-http-oauth.jar
    4. Web ブラウザーを使用して http://localhost:8080 に移動します。GitHub の OAuth2 アプリケーション認可画面にリダイレクトされます。プロンプトが表示されたら、GitHub アカウントの認証情報を使用してログインします。
    5. Authorize をクリックして確定します。パーソナライズされたグリーティングメッセージを示す画面にリダイレクトされます。

その他のリソース