The English version of quarkus.io is the official project site. Translated sites are community supported on a best-effort basis.
このページを編集

OpenID Connect クライアントとトークン伝播のクイックスタート

OpenID Connect (OIDC) および OAuth2 クライアントをフィルターとともに使用して、アプリケーションでアクセストークンを取得、更新、伝播する方法を学びます。

Quarkus での OIDC Client および Token Propagation のサポートに関する詳細は、OpenID Connect (OIDC) および OAuth2 クライアントとフィルターのリファレンスガイド を参照してください。

ベアラートークン認可を使用してアプリケーションを保護する方法については、OpenID Connect (OIDC) ベアラー認証 ガイドを参照してください。

前提条件

このガイドを完成させるには、以下が必要です:

  • 約15分

  • IDE

  • JDK 17+がインストールされ、 JAVA_HOME が適切に設定されていること

  • Apache Maven 3.9.15

  • 動作するコンテナランタイム(Docker, Podman)

  • 使用したい場合は、 Quarkus CLI

  • ネイティブ実行可能ファイルをビルドしたい場合、MandrelまたはGraalVM(あるいはネイティブなコンテナビルドを使用する場合はDocker)をインストールし、 適切に設定していること

  • jq tool

アーキテクチャー

In this example, an application is built with two Jakarta REST resources, FrontendResource and ProtectedResource.

  • FrontendResource uses one of three methods to propagate access tokens to 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

  • When either /frontend/user-name-with-oidc-client-token or /frontend/admin-name-with-oidc-client-token endpoint is called, FrontendResource uses a REST client with an OIDC client filter to get and propagate an access token to ProtectedResource .

  • When either /frontend/user-name-with-oidc-client-token-header-param or /frontend/admin-name-with-oidc-client-token-header-param endpoint is called, FrontendResource uses a programmatically created OIDC client to get and propagate an access token to ProtectedResource by passing it to a REST client method as an HTTP Authorization header value.

  • When either /frontend/user-name-with-propagated-token or /frontend/admin-name-with-propagated-token endpoint is called, FrontendResource uses a REST client with OIDC Token Propagation Filter to propagate the current incoming access token to ProtectedResource.

  • ProtectedResource には2つのエンドポイントがあります。

    • /protected/user-name

    • /protected/admin-name

      どちらのエンドポイントも、FrontendResource から ProtectedResource に伝播された受信アクセストークンから抽出したユーザー名を返します。 これらのエンドポイントの唯一の違いは、/protected/user-name の呼び出しは、現在のアクセストークンに user ロールがある場合にのみ許可され、/protected/admin-name の呼び出しは、現在のアクセストークンに admin ロールがある場合にのみ許可されることです。

ソリューション

次のセクションの指示に従って、アプリケーションを段階的に作成することをお勧めします。 ただし、完成した例に直接進むこともできます。

Git リポジトリーをクローンします: git clone https://github.com/quarkusio/quarkus-quickstarts.git 、または アーカイブ をダウンロードします。

ソリューションは security-openid-connect-client-quickstart ディレクトリー にあります。

Maven プロジェクトの作成

以下のコマンドで新規プロジェクトを作成します。

コマンドラインインタフェース
quarkus create app org.acme:security-openid-connect-client-quickstart \
    --extension='oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest' \
    --no-code
cd security-openid-connect-client-quickstart

Gradleプロジェクトを作成するには、 --gradle または --gradle-kotlin-dsl オプションを追加します。

Quarkus CLIのインストールと使用方法の詳細については、 Quarkus CLI ガイドを参照してください。

Maven
mvn io.quarkus.platform:quarkus-maven-plugin:3.35.2:create \
    -DprojectGroupId=org.acme \
    -DprojectArtifactId=security-openid-connect-client-quickstart \
    -Dextensions='oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest' \
    -DnoCode
cd security-openid-connect-client-quickstart

Gradleプロジェクトを作成するには、 -DbuildTool=gradle または -DbuildTool=gradle-kotlin-dsl オプションを追加します。

Windowsユーザーの場合:

  • cmdを使用する場合、(バックスラッシュ \ を使用せず、すべてを同じ行に書かないでください)。

  • Powershellを使用する場合は、 -D パラメータを二重引用符で囲んでください。例: "-DprojectArtifactId=security-openid-connect-client-quickstart"

Maven プロジェクトが生成され、oidcrest-client-oidc-filterrest-client-oidc-token-propagation、および rest エクステンションがインポートされます。

すでに Quarkus プロジェクトが設定されている場合は、プロジェクトのベースディレクトリーで以下のコマンドを実行することで、これらのエクステンションをプロジェクトに追加できます。

コマンドラインインタフェース
quarkus extension add oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest
Maven
./mvnw quarkus:add-extension -Dextensions='oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest'
Gradle
./gradlew addExtension --extensions='oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest'

ビルドファイルに次のエクステンションが追加されます。

pom.xml
<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>
build.gradle
implementation("io.quarkus:quarkus-oidc,rest-client-oidc-filter,rest-client-oidc-token-propagation,rest")

アプリケーションの記述

  1. Implement 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());
        }
    }

    ProtectedResourceuserName()adminName() の両方のメソッドから名前を返します。 この名前は現在の JsonWebToken から抽出されます。

  2. Add the following REST clients:

    1. RestClientWithOidcClientFilter: quarkus-rest-client-oidc-filter エクステンションによって提供される OIDC クライアントフィルターを使用して、アクセストークンを取得および伝播します。

    2. RestClientWithTokenHeaderParam: これは、プログラムで作成された OidcClient によってすでに取得されたトークンを HTTP Authorization ヘッダー値として受け入れます。

    3. RestClientWithTokenPropagationFilter: これは、quarkus-rest-client-oidc-token-propagation エクステンションによって提供される OIDC トークン伝播フィルターを使用して、アクセストークンを取得および伝播します。

  3. 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 クライアントフィルターを登録して、トークンを取得および伝播します。
  4. 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 ヘッダー値として渡されることを想定しています。
  5. 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 トークン伝播フィルターを登録して、既存の受信トークンを伝播します。

    Do not use the RestClientWithOidcClientFilter and RestClientWithTokenPropagationFilter interfaces in the same REST client because they can conflict, leading to issues.

    For example, the OIDC client filter can override the token from the OIDC token propagation filter, or the propagation filter might not work correctly if it attempts to propagate a token when none is available, expecting the OIDC client filter to obtain a new token instead.

  6. Add OidcClientCreator to create an OIDC client programmatically at startup. OidcClientCreator supports RestClientWithTokenHeaderParam REST client calls:

    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 クライアントを作成できます。
  7. Finish creating the application by adding 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(); (3)
    
        @Inject
        @RestClient
        RestClientWithTokenHeaderParam restClientWithTokenHeaderParam; (4)
    
        @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() { (4)
            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() { (4)
            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() { (5)
            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() { (5)
            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 io.quarkus.oidc.client.runtime.TokensHelper is useful when the OIDC client is used directly, without the OIDC client filter. Pass the OIDC client to TokensHelper to get the tokens. TokensHelper acquires the tokens and refreshes them if necessary in a thread-safe way.
    4 FrontendResource uses the programmatically created OIDC client to get and propagate an access token to ProtectedResource by passing it directly to the injected RestClientWithTokenHeaderParam REST client’s method as an HTTP Authorization header value when either /frontend/user-name-with-oidc-client-token-header-param or /frontend/admin-name-with-oidc-client-token-header-param is called.
    5 Sometimes, an application needs to acquire tokens in a blocking manner before propagating them with the REST client. This example shows how to acquire the tokens in such cases.
  8. Add a 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();
    	}
    
    }

    This exception mapper is only added to verify during the tests that ProtectedResource returns 403 when the token has no expected role.

    Without this mapper, Quarkus REST (formerly RESTEasy Reactive) would correctly convert the exceptions that escape from REST client calls to 500 to avoid leaking the information from the downstream resources such as ProtectedResource. However, in the tests, it would not be possible to assert that 500 is caused by an authorization exception instead of some internal error.

アプリケーションの設定

コードを準備したら、アプリケーションを設定します。

# 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 アドレスを指しています。

Adding a %prod. profile prefix to quarkus.oidc.auth-server-url ensures that Dev Services for Keycloak launches a container for you when the application is run in dev or test modes.

For more information, see the Running the application in dev mode section.

Keycloak サーバーの起動と設定

Do not start the Keycloak server when you run the application in dev or test modes; Dev Services for Keycloak launches a container.

For more information, see the Running the application in dev mode section.

Ensure you put the realm configuration file on the classpath, in the target/classes directory. This placement ensures that the file is automatically imported in dev mode. However, if you have already built a complete solution, you do not need to add the realm file to the classpath because the build process has already done so.

  1. Start a Keycloak Server by using Docker:

    docker run --name keycloak -e KC_BOOTSTRAP_ADMIN_USERNAME=admin -e KC_BOOTSTRAP_ADMIN_PASSWORD=admin -p 8180:8080 quay.io/keycloak/keycloak:26.5.7 start-dev

    Keycloak サーバーには localhost:8180 からアクセスできます。

  2. Keycloak 管理コンソールにアクセスするには、 admin ユーザーとしてログインします。パスワードは admin です。

  3. Import the realm configuration file to create a new realm.

    詳細は、 新規レルムの作成 方法に関する Keycloak ドキュメントを参照してください。

    この quarkus レルムファイルでは、 frontend クライアントと、 alice および admin ユーザーが追加されます。 aliceuser ロールを持ち、 adminuseradmin の両方のロールを持ちます。

開発モードでのアプリケーションの実行

  1. Run the application in a dev mode:

    コマンドラインインタフェース
    quarkus dev
    Maven
    ./mvnw quarkus:dev
    Gradle
    ./gradlew --console=plain quarkusDev

    Keycloak の Dev Services は Keycloak コンテナーを起動し、 quarkus-realm.json をインポートします。

  2. /q/dev-ui で入手可能な Dev UI を開き、OpenID Connect Dev UI カードの Keycloak provider リンクをクリックします。

  3. When asked, log in to a Single Page Application provided by the OpenID Connect Dev UI, log in as admin, with the password, admin.

    This user has both admin and user roles.

    1. /frontend/user-name-with-propagated-token にアクセスすると、 200 が返されます。

    2. /frontend/admin-name-with-propagated-token にアクセスすると、 200 が返されます。

  4. Log out and back in as alice with the password, alice.

    This user has a user role.

    1. /frontend/user-name-with-propagated-token にアクセスすると、 200 が返されます。

    2. /frontend/admin-name-with-propagated-token にアクセスすると、 403 が返されます。

      これで、 FrontendResource が OpenID Connect Dev UI からアクセストークンを伝播できることをテストできました。

JVM モードでのアプリケーションの実行

  1. After exploring the application in dev mode, run it as a standard Java application by compiling it:

    コマンドラインインタフェース
    quarkus build
    Maven
    ./mvnw install
    Gradle
    ./gradlew build
  2. Run it:

    java -jar target/quarkus-app/quarkus-run.jar

ネイティブモードでのアプリケーションの実行

このデモはネイティブコードにコンパイルできます。変更は必要ありません。

これは、生成されたバイナリーにランタイムテクノロジーが含まれ、最小限のリソースで実行するように最適化されているため、実稼働環境に JVM をインストールする必要がなくなることを意味します。

コンパイルには時間がかかるため、この手順はデフォルトでオフになっています。再度ビルドするには、 native プロファイルを有効にします。

コマンドラインインタフェース
quarkus build --native
Maven
./mvnw install -Dnative
Gradle
./gradlew build -Dquarkus.native.enabled=true

しばらくしてビルドが完了すると、ネイティブバイナリーを直接実行できます。

./target/security-openid-connect-quickstart-1.0.0-SNAPSHOT-runner

アプリケーションのテスト

開発モードでのアプリケーションのテストの詳細は、前述のRunning the application in dev mode セクションを参照してください。

curl を使用して、JVM またはネイティブモードで起動したアプリケーションをテストできます。

  1. 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' \
     )
  2. このトークンを使用して、 /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
  3. 同じトークンを使用して /frontend/admin-name-with-propagated-token を呼び出します。前のコマンドとは対照的に、このコマンドは aliceuser ロールしかないため 403 を返します。

    curl -i -X GET \
      http://localhost:8080/frontend/admin-name-with-propagated-token \
      -H "Authorization: Bearer "$access_token
  4. Obtain an access token for 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' \
     )
  5. このトークンを使用して /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
  6. 同じトークンを使用して、 /frontend/admin-name-with-propagated-token を呼び出します。このコマンドは、 adminuseradmin の両方のロールがあるため、 200 ステータスコードと名前 admin も返します。

    curl -i -X GET \
      http://localhost:8080/frontend/admin-name-with-propagated-token \
      -H "Authorization: Bearer "$access_token
  7. Check the FrontendResource methods, which do not propagate the existing tokens but use OidcClient to get and propagate the tokens.

    As already shown, OidcClient is configured to get the tokens for the alice user.

    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 ステータスコードを返します。

  8. Test that the programmatically created OIDC client correctly acquires and propagates the token with RestClientWithTokenHeaderParam both in reactive and imperative (blocking) modes.

    1. /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
    2. /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
  9. Test the endpoints that use OIDC client in the blocking mode.

    1. /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
    2. /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

関連コンテンツ