OpenID Connect クライアントとトークンの伝播クイックスタート
フィルター付きの OpenID Connect (OIDC) および OAuth2 クライアントを使用して、アプリケーションでアクセストークンを取得、更新、および伝播する方法を学習します。
Quarkus での OIDC Client
と Token Propagation
のサポートの詳細は、OpenID Connect (OIDC) と OAuth2 クライアントおよびフィルターのリファレンスガイド を参照してください。
Bearer Token Authorization を使用してアプリケーションを保護するには、OpenID Connect (OIDC) ベアラートークン認証 ガイドを参照してください。
要件
このガイドを完成させるには、以下が必要です:
-
約15分
-
IDE
-
JDK 17+がインストールされ、
JAVA_HOME
が適切に設定されていること -
Apache Maven 3.9.9
-
動作するコンテナランタイム(Docker, Podman)
-
使用したい場合は、 Quarkus CLI
-
ネイティブ実行可能ファイルをビルドしたい場合、MandrelまたはGraalVM(あるいはネイティブなコンテナビルドを使用する場合はDocker)をインストールし、 適切に設定していること
アーキテクチャー
この例では、アプリケーションは 2 つの Jakarta REST リソース FrontendResource と ProtectedResource を使用して構築されます。
ここで、 FrontendResource
は 3 つの方法のいずれかを使用してアクセストークンを ProtectedResource
に伝播します。
-
トークンを伝播する前に、OIDC クライアントフィルターを使用してトークンを取得できます。
-
プログラムで作成された OIDC クライアントを使用してトークンを取得し、それを HTTP
Authorization
ヘッダー値として REST クライアントメソッドに渡すことで伝播できます。 -
OIDC トークン伝播フィルターを使用して、受信したアクセストークンを伝播できます。
FrontendResource
には 8 つのエンドポイントがあります。
-
/frontend/user-name-with-oidc-client-token
-
/frontend/admin-name-with-oidc-client-token
-
/frontend/user-name-with-oidc-client-token-header-param
-
/frontend/admin-name-with-oidc-client-token-header-param
-
/frontend/user-name-with-oidc-client-token-header-param-blocking
-
/frontend/admin-name-with-oidc-client-token-header-param-blocking
-
/frontend/user-name-with-propagated-token
-
/frontend/admin-name-with-propagated-token
/frontend/user-name-with-oidc-client-token
または /frontend/admin-name-with-oidc-client-token
エンドポイントのいずれかが呼び出されると、 FrontendResource
は OIDC クライアントフィルターを備えた REST クライアントを使用してアクセストークンを取得し、 ProtectedResource
に伝播します。
/frontend/user-name-with-oidc-client-token-header-param
または /frontend/admin-name-with-oidc-client-token-header-param
エンドポイントのいずれかが呼び出されると、 FrontendResource
はプログラムで作成された OIDC クライアントを使用してアクセストークンを取得し、それを HTTP Authorization
ヘッダー値として REST クライアントメソッドに渡すことで ProtectedResource
に伝播します。
/frontend/user-name-with-propagated-token
または /frontend/admin-name-with-propagated-token
エンドポイントのいずれかが呼び出されると、 FrontendResource
は OIDC Token Propagation Filter
を備えた REST クライアントを使用して、現在の受信アクセストークンを ProtectedResource
に伝播します。
ProtectedResource
には 2 つのエンドポイントがあります。
-
/protected/user-name
-
/protected/admin-name
どちらのエンドポイントも、 FrontendResource
から ProtectedResource
に伝搬された受信アクセストークンから抽出したユーザー名を返します。これらのエンドポイントの唯一の違いは、 /protected/user-name
の呼び出しは、現在のアクセストークンに user
ロールがある場合にのみ、 /protected/admin-name
の呼び出しは、現在のアクセストークンに admin
ロールがある場合のみ許可されることです。
ソリューション
次の章で紹介する手順に沿って、ステップを踏んでアプリケーションを作成することを推奨します。 ただし、完成した例にそのまま進むこともできます。
git clone https://github.com/quarkusio/quarkus-quickstarts.git
で Git リポジトリーをクローンします。または、https://github.com/quarkusio/quarkus-quickstarts/archive/main.zip[アーカイブ] をダウンロードします。
解決策は ` ディレクトリー にあります。
Maven プロジェクトの作成
まず、新しいプロジェクトが必要です。 以下のコマンドで新規プロジェクトを作成します。
Windowsユーザーの場合:
-
cmdを使用する場合、(バックスラッシュ
\
を使用せず、すべてを同じ行に書かないでください)。 -
Powershellを使用する場合は、
-D
パラメータを二重引用符で囲んでください。例:"-DprojectArtifactId=security-openid-connect-client-quickstart"
Maven プロジェクトが生成され、 oidc
、 rest-client-oidc-filter
、 rest-client-oidc-token-propagation
、および rest
エクステンションがインポートされます。
すでに Quarkus プロジェクトが設定されている場合は、プロジェクトのベースディレクトリーで以下のコマンドを実行することで、プロジェクトにこれらのエクステンションを追加できます。
quarkus extension add oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest
./mvnw quarkus:add-extension -Dextensions='oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest'
./gradlew addExtension --extensions='oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest'
ビルドファイルに次のエクステンションが追加されます。
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-oidc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-oidc-filter</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-oidc-token-propagation</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest</artifactId>
</dependency>
implementation("io.quarkus:quarkus-oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest")
アプリケーションの記述
まず、 ProtectedResource
を実装します。
package org.acme.security.openid.connect.client;
import jakarta.annotation.security.RolesAllowed;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import io.quarkus.security.Authenticated;
import io.smallrye.mutiny.Uni;
import org.eclipse.microprofile.jwt.JsonWebToken;
@Path("/protected")
@Authenticated
public class ProtectedResource {
@Inject
JsonWebToken principal;
@GET
@RolesAllowed("user")
@Produces("text/plain")
@Path("userName")
public Uni<String> userName() {
return Uni.createFrom().item(principal.getName());
}
@GET
@RolesAllowed("admin")
@Produces("text/plain")
@Path("adminName")
public Uni<String> adminName() {
return Uni.createFrom().item(principal.getName());
}
}
ProtectedResource
は userName()
と adminName()
の両方のメソッドから名前を返します。この名前は、現在の JsonWebToken
から抽出されたものです。
次に、以下の REST クライアントを追加します。
-
RestClientWithOidcClientFilter
: これは、quarkus-rest-client-oidc-filter
エクステンションによって提供される OIDC クライアントフィルターを使用して、アクセストークンを取得および伝播します。 -
RestClientWithTokenHeaderParam
: これは、プログラムで作成された OidcClient によってすでに取得されたトークンを HTTPAuthorization
ヘッダー値として受け入れます。 -
RestClientWithTokenPropagationFilter
: これは、quarkus-rest-client-oidc-token-propagation
エクステンションによって提供される OIDC トークン伝播フィルターを使用して、アクセストークンを取得および伝播します。
RestClientWithOidcClientFilter
REST クライアントを追加します。
package org.acme.security.openid.connect.client;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import io.quarkus.oidc.client.filter.OidcClientFilter;
import io.smallrye.mutiny.Uni;
@RegisterRestClient
@OidcClientFilter (1)
@Path("/")
public interface RestClientWithOidcClientFilter {
@GET
@Produces("text/plain")
@Path("userName")
Uni<String> getUserName();
@GET
@Produces("text/plain")
@Path("adminName")
Uni<String> getAdminName();
}
1 | REST クライアントに OIDC クライアントフィルターを登録して、トークンを取得および伝播します。 |
RestClientWithTokenHeaderParam
REST クライアントを追加します。
package org.acme.security.openid.connect.client;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import io.smallrye.mutiny.Uni;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.HeaderParam;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
@RegisterRestClient
@Path("/")
public interface RestClientWithTokenHeaderParam {
@GET
@Produces("text/plain")
@Path("userName")
Uni<String> getUserName(@HeaderParam("Authorization") String authorization); (1)
@GET
@Produces("text/plain")
@Path("adminName")
Uni<String> getAdminName(@HeaderParam("Authorization") String authorization); (1)
}
1 | RestClientWithTokenHeaderParam REST クライアントは、トークンが HTTP Authorization ヘッダー値として渡されることを要求します。 |
RestClientWithTokenPropagationFilter
REST クライアントを追加します。
package org.acme.security.openid.connect.client;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import io.quarkus.oidc.token.propagation.common.AccessToken;
import io.smallrye.mutiny.Uni;
@RegisterRestClient
@AccessToken (1)
@Path("/")
public interface RestClientWithTokenPropagationFilter {
@GET
@Produces("text/plain")
@Path("userName")
Uni<String> getUserName();
@GET
@Produces("text/plain")
@Path("adminName")
Uni<String> getAdminName();
}
1 | REST クライアントに OIDC トークン伝播フィルターを登録して、既存の受信トークンを伝播します。 |
RestClientWithOidcClientFilter インターフェイスと RestClientWithTokenPropagationFilter インターフェイスを同じ REST クライアントで使用しないでください。競合して問題が発生する可能性があります。
たとえば、OIDC クライアントフィルターは、OIDC トークン伝播フィルターからのトークンをオーバーライドできます。また、利用可能なトークンがない場合に伝播フィルターがトークンを伝播しようとして、代わりに OIDC クライアントフィルターが新しいトークンを取得するように要求し、伝播フィルターが正しく機能しない可能性があります。
|
また、起動時にプログラムで OIDC クライアントを作成する OidcClientCreator
を追加します。 OidcClientCreator
は、 RestClientWithTokenHeaderParam
REST クライアント呼び出しをサポートします。
package org.acme.security.openid.connect.client;
import java.util.Map;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import io.quarkus.oidc.client.OidcClient;
import io.quarkus.oidc.client.OidcClients;
import io.quarkus.oidc.client.runtime.OidcClientConfig;
import io.quarkus.oidc.client.runtime.OidcClientConfig.Grant.Type;
import io.quarkus.runtime.StartupEvent;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
@ApplicationScoped
public class OidcClientCreator {
@Inject
OidcClients oidcClients; (1)
@ConfigProperty(name = "quarkus.oidc.auth-server-url")
String oidcProviderAddress;
private volatile OidcClient oidcClient;
public void startup(@Observes StartupEvent event) {
createOidcClient().subscribe().with(client -> {oidcClient = client;});
}
public OidcClient getOidcClient() {
return oidcClient;
}
private Uni<OidcClient> createOidcClient() {
OidcClientConfig cfg = OidcClientConfig
.authServerUrl(oidcProviderAddress)
.id("myclient")
.clientId("backend-service")
.credentials("secret")
.grant(Type.PASSWORD)
.grantOptions("password", Map.of("username", "alice", "password", "alice"))
.build();
return oidcClients.newClient(cfg);
}
}
1 | OidcClients を使用して、すでに初期化され、名前が付けられた OIDC クライアントを取得できるほか、必要に応じて新しい OIDC クライアントを作成できます。 |
次に、 FrontendResource
を追加してアプリケーションの作成を完了します。
package org.acme.security.openid.connect.client;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import io.quarkus.oidc.client.Tokens;
import io.quarkus.oidc.client.runtime.TokensHelper;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import io.smallrye.mutiny.Uni;
@Path("/frontend")
public class FrontendResource {
@Inject
@RestClient
RestClientWithOidcClientFilter restClientWithOidcClientFilter; (1)
@Inject
@RestClient
RestClientWithTokenPropagationFilter restClientWithTokenPropagationFilter; (2)
@Inject
OidcClientCreator oidcClientCreator;
TokensHelper tokenHelper = new TokensHelper(); (5)
@Inject
@RestClient
RestClientWithTokenHeaderParam restClientWithTokenHeaderParam; (3)
@GET
@Path("user-name-with-oidc-client-token")
@Produces("text/plain")
public Uni<String> getUserNameWithOidcClientToken() { (1)
return restClientWithOidcClientFilter.getUserName();
}
@GET
@Path("admin-name-with-oidc-client-token")
@Produces("text/plain")
public Uni<String> getAdminNameWithOidcClientToken() { (1)
return restClientWithOidcClientFilter.getAdminName();
}
@GET
@Path("user-name-with-propagated-token")
@Produces("text/plain")
public Uni<String> getUserNameWithPropagatedToken() { (2)
return restClientWithTokenPropagationFilter.getUserName();
}
@GET
@Path("admin-name-with-propagated-token")
@Produces("text/plain")
public Uni<String> getAdminNameWithPropagatedToken() { (2)
return restClientWithTokenPropagationFilter.getAdminName();
}
@GET
@Path("user-name-with-oidc-client-token-header-param")
@Produces("text/plain")
public Uni<String> getUserNameWithOidcClientTokenHeaderParam() { (3)
return tokenHelper.getTokens(oidcClientCreator.getOidcClient()).onItem()
.transformToUni(tokens -> restClientWithTokenHeaderParam.getUserName("Bearer " + tokens.getAccessToken()));
}
@GET
@Path("admin-name-with-oidc-client-token-header-param")
@Produces("text/plain")
public Uni<String> getAdminNameWithOidcClientTokenHeaderParam() { (3)
return tokenHelper.getTokens(oidcClientCreator.getOidcClient()).onItem()
.transformToUni(tokens -> restClientWithTokenHeaderParam.getAdminName("Bearer " + tokens.getAccessToken()));
}
@GET
@Path("user-name-with-oidc-client-token-header-param-blocking")
@Produces("text/plain")
public String getUserNameWithOidcClientTokenHeaderParamBlocking() { (4)
Tokens tokens = tokenHelper.getTokens(oidcClientCreator.getOidcClient()).await().indefinitely();
return restClientWithTokenHeaderParam.getUserName("Bearer " + tokens.getAccessToken()).await().indefinitely();
}
@GET
@Path("admin-name-with-oidc-client-token-header-param-blocking")
@Produces("text/plain")
public String getAdminNameWithOidcClientTokenHeaderParamBlocking() { (4)
Tokens tokens = tokenHelper.getTokens(oidcClientCreator.getOidcClient()).await().indefinitely();
return restClientWithTokenHeaderParam.getAdminName("Bearer " + tokens.getAccessToken()).await().indefinitely();
}
}
1 | FrontendResource は、 /frontend/user-name-with-oidc-client-token または /frontend/admin-name-with-oidc-client-token のいずれかが呼び出されたときに、注入された RestClientWithOidcClientFilter REST クライアントと OIDC クライアントフィルターを使用し、アクセストークンを取得して ProtectedResource に伝播します。 |
2 | FrontendResource は、 /frontend/user-name-with-propagated-token または /frontend/admin-name-with-propagated-token のいずれかが呼び出されたときに、注入された RestClientWithTokenPropagationFilter REST クライアントと OIDC トークン伝播フィルターを使用して、現在の受信アクセストークンを ProtectedResource に伝播します。 |
3 | FrontendResource は、 /frontend/user-name-with-oidc-client-token-header-param または /frontend/admin-name-with-oidc-client-token-header-param のいずれかが呼び出されたときに、プログラムで作成された OIDC クライアントを使用してアクセストークンを取得し、注入された RestClientWithTokenHeaderParam REST クライアントのメソッドに HTTP Authorization ヘッダー値として直接アクセストークンを渡すことで ProtectedResource に伝播します。 |
4 | 場合によっては、REST クライアントでトークンを伝播する前に、ブロック方式でトークンを取得する必要があります。この例では、そのような場合にトークンを取得する方法を示します。 |
5 | io.quarkus.oidc.client.runtime.TokensHelper は、OIDC クライアントフィルターを使用せずに OIDC クライアントを直接使用する場合に便利なツールです。 TokensHelper を使用するには、OIDC クライアントを渡してトークンを取得します。 TokensHelper はトークンを取得し、必要に応じてスレッドセーフな方法でトークンを更新します。 |
最後に、Jakarta REST ExceptionMapper
を追加します。
package org.acme.security.openid.connect.client;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;
import jakarta.ws.rs.ext.Provider;
import org.jboss.resteasy.reactive.ClientWebApplicationException;
@Provider
public class FrontendExceptionMapper implements ExceptionMapper<ClientWebApplicationException> {
@Override
public Response toResponse(ClientWebApplicationException t) {
return Response.status(t.getResponse().getStatus()).build();
}
}
この例外マッパーは、トークンに想定するロールがない場合に ProtectedResource
が 403 を返すことをテスト中に検証するためにのみ追加されます。このマッパーがないと、Quarkus REST (旧 RESTEasy Reactive) は、REST クライアント呼び出しからエスケープされる例外を 500
に正しく変換して、 ProtectedResource
などのダウンストリームリソースからの情報漏えいを回避します。
ただし、テストでは 500
が実際は内部エラーではなく、認可例外が原因であることをアサートできません。
アプリケーションの設定
コードを準備したら、アプリケーションを設定します。
# Configure OIDC
%prod.quarkus.oidc.auth-server-url=http://localhost:8180/realms/quarkus
quarkus.oidc.client-id=backend-service
quarkus.oidc.credentials.secret=secret
# Tell Dev Services for Keycloak to import the realm file
# This property is ineffective when running the application in JVM or Native modes but only in dev and test modes.
quarkus.keycloak.devservices.realm-path=quarkus-realm.json
# Configure OIDC Client
quarkus.oidc-client.auth-server-url=${quarkus.oidc.auth-server-url}
quarkus.oidc-client.client-id=${quarkus.oidc.client-id}
quarkus.oidc-client.credentials.secret=${quarkus.oidc.credentials.secret}
quarkus.oidc-client.grant.type=password
quarkus.oidc-client.grant-options.password.username=alice
quarkus.oidc-client.grant-options.password.password=alice
# Configure REST clients
%prod.port=8080
%dev.port=8080
%test.port=8081
org.acme.security.openid.connect.client.RestClientWithOidcClientFilter/mp-rest/url=http://localhost:${port}/protected
org.acme.security.openid.connect.client.RestClientWithTokenHeaderParam/mp-rest/url=http://localhost:${port}/protected
org.acme.security.openid.connect.client.RestClientWithTokenPropagationFilter/mp-rest/url=http://localhost:${port}/protected
上記の設定は Keycloak を参照します。Keycloak は、受信アクセストークンを検証するために ProtectedResource
によって使用され、 password
グラントを使用してユーザー alice
のトークンを取得するために OidcClient
によって使用されます。どちらの REST クライアントも ProtectedResource
の HTTP アドレスを指しています。
quarkus.oidc.auth-server-url に %prod. プロファイル接頭辞を追加すると、アプリケーションが開発モードまたはテストモードで実行されているときに、 Dev Services for Keycloak がコンテナーを起動するようになります。
詳細は、Running the application in dev mode セクションを参照してください。
|
Keycloak サーバーの起動と設定
アプリケーションを開発モードまたはテストモードで実行するときは、Keycloak サーバーを起動しないでください。 Dev Services for Keycloak がコンテナーを起動します。
詳細は、Running the application in dev mode セクションを参照してください。
必ず、 target/classes ディレクトリーのクラスパス上に レルム設定ファイル を挿入してください。
この配置により、ファイルが開発モードで自動的にインポートされるようになります。
ただし、すでに 全ソリューション を構築している場合は、ビルドプロセスによってすでに実行されているため、レルムファイルをクラスパスに追加する必要はありません。
|
Docker を使用して次のコマンドを実行するだけで、Keycloak サーバーを起動できます。
docker run --name keycloak -e KC_BOOTSTRAP_ADMIN_USERNAME=admin -e KC_BOOTSTRAP_ADMIN_PASSWORD=admin -p 8180:8080 quay.io/keycloak/keycloak:{keycloak.version} start-dev
{keycloak.version}
を 26.1.3
以降に設定してください。
Keycloak サーバーには localhost:8180 からアクセスできます。
Keycloak 管理コンソールにアクセスするには、 admin
ユーザーとしてログインします。
パスワードは admin
です。
新しいレルムを作成するには、レルム設定ファイル をインポートします。 新しいレルムを作成する 方法は Keycloakのドキュメントを参照してください。
この quarkus
レルムファイルでは、 frontend
クライアントと、 alice
および admin
ユーザーが追加されます。
alice
は user
ロールを持ち、
admin
は user
と admin
の両方のロールを持ちます。
開発モードでのアプリケーションの実行
アプリケーションを開発モードで実行するには、次を使用します。
quarkus dev
./mvnw quarkus:dev
./gradlew --console=plain quarkusDev
Keycloak の Dev Services は Keycloak コンテナーを起動し、 quarkus-realm.json
をインポートします。
求められたら、OpenID Connect Dev UI によって提供される Single Page Application
にログインします。
-
パスワード
admin
を使用して、admin
としてログインします。このユーザーには、admin
とuser
の両方の役割があります。-
/frontend/user-name-with-propagated-token
にアクセスすると、200
が返されます。 -
/frontend/admin-name-with-propagated-token
にアクセスすると、200
が返されます。
-
-
ログアウトし、パスワード
alice
を使用してalice
として再度ログインします。 このユーザーにはuser
ロールがあります。-
/frontend/user-name-with-propagated-token
にアクセスすると、200
が返されます。 -
/frontend/admin-name-with-propagated-token
にアクセスすると、403
が返されます。
-
これで、 FrontendResource
が OpenID Connect Dev UI からアクセストークンを伝播できることをテストできました。
JVM モードでのアプリケーションの実行
開発モードでアプリケーションを試した後、標準の Java アプリケーションとして実行できます。
まず、コンパイルします:
quarkus build
./mvnw install
./gradlew build
そして、実行してみてください:
java -jar target/quarkus-app/quarkus-run.jar
ネイティブモードでアプリケーションの実行
このデモはネイティブコードにコンパイルできます。変更は必要ありません。
これは、生成されたバイナリーにランタイムテクノロジーが含まれ、 最小限のリソースで実行するように最適化されているため、 実稼働環境に JVM をインストールする必要がなくなることを意味します。
コンパイルには時間がかかるため、この手順はデフォルトでオフになっています。
再度ビルドするには、 native
プロファイルを有効にします。
quarkus build --native
./mvnw install -Dnative
./gradlew build -Dquarkus.native.enabled=true
しばらくしてビルドが完了すると、ネイティブバイナリーを直接実行できます。
./target/security-openid-connect-quickstart-1.0.0-SNAPSHOT-runner
アプリケーションのテスト
開発モードでのアプリケーションのテストの詳細は、前述のRunning the application in dev mode セクションを参照してください。
curl
を使用して、JVM またはネイティブモードで起動したアプリケーションをテストできます。
alice
のアクセストークンを取得します:
export access_token=$(\
curl --insecure -X POST http://localhost:8180/realms/quarkus/protocol/openid-connect/token \
--user backend-service:secret \
-H 'content-type: application/x-www-form-urlencoded' \
-d 'username=alice&password=alice&grant_type=password' | jq --raw-output '.access_token' \
)
このトークンを使用して、 /frontend/user-name-with-propagated-token
を呼び出します。このコマンドは、 200
ステータスコードと名前 alice
を返します。
curl -i -X GET \
http://localhost:8080/frontend/user-name-with-propagated-token \
-H "Authorization: Bearer "$access_token
同じトークンを使用して /frontend/admin-name-with-propagated-token
を呼び出します。前のコマンドとは対照的に、このコマンドは alice
に user
ロールしかないため 403
を返します。
curl -i -X GET \
http://localhost:8080/frontend/admin-name-with-propagated-token \
-H "Authorization: Bearer "$access_token
次に admin
用のアクセストークンを取得します。
export access_token=$(\
curl --insecure -X POST http://localhost:8180/realms/quarkus/protocol/openid-connect/token \
--user backend-service:secret \
-H 'content-type: application/x-www-form-urlencoded' \
-d 'username=admin&password=admin&grant_type=password' | jq --raw-output '.access_token' \
)
このトークンを使用して /frontend/user-name-with-propagated-token
を呼び出します。このコマンドは 200
ステータスコードと名前 admin
を返します。
curl -i -X GET \
http://localhost:8080/frontend/user-name-with-propagated-token \
-H "Authorization: Bearer "$access_token
同じトークンを使用して、 /frontend/admin-name-with-propagated-token
を呼び出します。このコマンドは、 admin
に user
と admin
の両方のロールがあるため、 200
ステータスコードと名前 admin
も返します。
curl -i -X GET \
http://localhost:8080/frontend/admin-name-with-propagated-token \
-H "Authorization: Bearer "$access_token
次に、既存のトークンを伝播せず、 OidcClient
を使用してトークンを取得および伝播する FrontendResource
メソッドを確認します。
すでに示したように、 OidcClient
は alice
ユーザーのトークンを取得するように設定されています。
curl -i -X GET \
http://localhost:8080/frontend/user-name-with-oidc-client-token
このコマンドは、ステータスコード 200
と名前 alice
を返します。
curl -i -X GET \
http://localhost:8080/frontend/admin-name-with-oidc-client-token
前のコマンドとは対照的に、このコマンドは 403
ステータスコードを返します。
次に、プログラムで作成された OIDC クライアントが、リアクティブモードと命令型 (ブロッキング) モードの両方で、 RestClientWithTokenHeaderParam
を使用してトークンを正しく取得して伝播することをテストします。
/user-name-with-oidc-client-token-header-param
を呼び出します。このコマンドは、 200
ステータスコードと名前 alice
を返します。
curl -i -X GET \
http://localhost:8080/frontend/user-name-with-oidc-client-token-header-param
/admin-name-with-oidc-client-token-header-param
を呼び出します。前のコマンドとは対照的に、このコマンドは 403
ステータスコードを返します。
curl -i -X GET \
http://localhost:8080/frontend/admin-name-with-oidc-client-token-header-param
次に、ブロッキングモードで OIDC クライアントを使用するエンドポイントをテストします。
/user-name-with-oidc-client-token-header-param-blocking
を呼び出します。このコマンドは、 200
ステータスコードと名前 alice
を返します。
curl -i -X GET \
http://localhost:8080/frontend/user-name-with-oidc-client-token-header-param-blocking
/admin-name-with-oidc-client-token-header-param-blocking
を呼び出します。前のコマンドとは対照的に、このコマンドは 403
ステータスコードを返します。
curl -i -X GET \
http://localhost:8080/frontend/admin-name-with-oidc-client-token-header-param-blocking