5.5. リアクティブ Spring Boot アプリケーションで OAuth2 認証の使用。
リアクティブ Spring Boot アプリケーションの OAuth2 認証 を設定し、クライアント ID およびクライアントシークレットを使用して認証します。
前提条件
- JDK 8 または JDK 11 がインストールされている。
- GitHub アカウント
- Maven がインストールされている。
- Spring Boot を使用するよう設定されたMaven ベースのアプリケーションプロジェクト
手順
Github アカウントに 新規 OAuth 2 アプリケーションを登録 します。登録フォームに以下の値を指定するようにしてください。
-
ホームページ URL:
http://localhost:8080
承認コールバック URL:
http://localhost:8080/login/oauth2/code/github
登録が完了したら、クライアント ID とクライアントシークレットを保存します。
-
ホームページ URL:
プロジェクトの
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>
-
アプリケーションのエンドポイントコントローラークラスを作成します。
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") + "!"); } }
アプリケーションのメインクラスを作成します。
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); } }
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
オプション: アプリケーションをローカルで実行し、テストします。
Maven プロジェクトのルートディレクトリーへ移動します。
$ cd myApp
アプリケーションをパッケージ化します。
$ mvn clean package
コマンドラインからアプリケーションを起動します。
$ java -jar target/vertx-spring-boot-sample-http-oauth.jar
-
Web ブラウザーを使用して
http://localhost:8080
に移動します。GitHub の OAuth2 アプリケーション認可画面にリダイレクトされます。プロンプトが表示されたら、GitHub アカウントの認証情報を使用してログインします。 - Authorize をクリックして確定します。パーソナライズされたグリーティングメッセージを示す画面にリダイレクトされます。
その他のリソース
- Fabric8 Maven プラグインを使用して、アプリケーションを OpenShift クラスターにデプロイ できます。
- また、スタンドアロンの Red Hat Enterprise Linux でのデプロイメント 用にアプリケーションを設定することもできます。
- 詳細は、Spring コミュニティードキュメントの OAuth2 チュートリアル を参照してください。または、Spring Security の OAuth2 を使用するためのチュートリアルを参照してください。
- OAuth2 認証フレームワークの完全な仕様については、RFC-6749 を参照してください。