すべてのプロパティ

spring_boot_starter_oauth2_client が読むすべてのプロパティです。登録は spring.security.oauth2.client.registration.<id>.*、プロバイダーは spring.security.oauth2.client.provider.<id>.* で、二つのキーが Feign クライアントのトークンを有効にします。スターターのメタデータ (lib/spring_gen/spring_metadata.json) にも同じ 26 個のキーが列挙されています。

使う理由

クライアントとプロバイダーは環境ごとに異なります。開発ではテスト用のレルム、後には本番の発行者、シークレットは環境からです。Spring Boot と同じくこれらはプロパティなので、一つのビルドがどこでも動き、Spring Boot の設定をそのまま写せます。

例

# lib/resources/application.yml
spring:
  security:
    oauth2:
      client:
        registration:
          keycloak:
            client-id: neon-web
            client-secret: ${KEYCLOAK_SECRET}
            scope: [openid, profile, email]
            client-name: Neon login
          partner:
            client-id: shop
            client-secret: ${PARTNER_SECRET}
            authorization-grant-type: client_credentials
            client-authentication-method: client_secret_post
            scope: orders.read
          apple:
            client-id: books.neon.web
            client-authentication-method: client_secret_post
            client-secret-jwt:
              issuer: ABCDE12345
              key-id: XYZ987KEY1
              private-key-location: file:/run/secrets/AuthKey_XYZ987KEY1.p8
              lifetime: 1h
        provider:
          keycloak:
            issuer-uri: https://login.example.com/realms/neon
          partner:
            token-uri: https://id.partner.example/oauth2/token
  feign:
    oauth2:
      enabled: true
      client-registration-id: partner

同じ内容を .properties で:

# lib/resources/application.properties
spring.security.oauth2.client.registration.keycloak.client-id=neon-web
spring.security.oauth2.client.registration.keycloak.client-secret=${KEYCLOAK_SECRET}
spring.security.oauth2.client.registration.keycloak.scope=openid,profile,email
spring.security.oauth2.client.registration.keycloak.client-name=Neon login
spring.security.oauth2.client.registration.partner.client-id=shop
spring.security.oauth2.client.registration.partner.client-secret=${PARTNER_SECRET}
spring.security.oauth2.client.registration.partner.authorization-grant-type=client_credentials
spring.security.oauth2.client.registration.partner.client-authentication-method=client_secret_post
spring.security.oauth2.client.registration.partner.scope=orders.read
spring.security.oauth2.client.registration.apple.client-id=books.neon.web
spring.security.oauth2.client.registration.apple.client-authentication-method=client_secret_post
spring.security.oauth2.client.registration.apple.client-secret-jwt.issuer=ABCDE12345
spring.security.oauth2.client.registration.apple.client-secret-jwt.key-id=XYZ987KEY1
spring.security.oauth2.client.registration.apple.client-secret-jwt.private-key-location=file:/run/secrets/AuthKey_XYZ987KEY1.p8
spring.security.oauth2.client.registration.apple.client-secret-jwt.lifetime=1h
spring.security.oauth2.client.provider.keycloak.issuer-uri=https://login.example.com/realms/neon
spring.security.oauth2.client.provider.partner.token-uri=https://id.partner.example/oauth2/token
spring.feign.oauth2.enabled=true
spring.feign.oauth2.client-registration-id=partner

シークレット (client-secret、client-secret-jwt.private-key) は環境変数かシークレットストアに置き、コミットされるファイルには決して置かないでください。どれもログに出ることはありません。

登録

各キーは spring.security.oauth2.client.registration.<id>.<key> です。登録の ID (keycloak、google) は、provider が別のものを指定しない限り、プロバイダーの ID でもあります。

キー 型 デフォルト 意味
client-id String 必須 プロバイダーでのクライアントの ID。
client-secret String なし クライアントのシークレット。アプリ (パブリッククライアント) にはない。クレデンシャルなので、決してログに出さない。
client-authentication-method client_secret_basic、client_secret_post、none authorization_code でシークレットがなければ none、それ以外は client_secret_basic クライアントがトークンエンドポイントで自分が誰かを証明する方法。
authorization-grant-type authorization_code、client_credentials issuer-uri か既知のプロバイダーがあれば authorization_code。それ以外は必須 ユーザーのログイン、またはクライアント自身のトークン (サーバーからサーバー)。
redirect-uri String {baseUrl}/login/oauth2/code/{registrationId} プロバイダーがブラウザーを戻す先。テンプレート、または neonbooks:/oauth2/callback のようなアプリの URI。プロバイダーが受け入れなければならない。
scope Set<String> なし。既知のプロバイダーではそのデフォルト 求めるスコープ。カンマ区切りのリストか YAML のリスト。openid があると OpenID Connect のログイン (チェックされる ID トークン) になる。
client-name String 発行者、または既知のプロバイダーの名前 ログインページでの名前。
provider String 登録の ID provider.* キーのプロバイダー、または既知のもの: google、github、facebook、okta、apple。
dpop auto、true、false auto DPoP (RFC 9449): トークンはクライアントの DPoPKey にバインドされ、すべてのリクエストが証明を運ぶ。auto は、プロバイダーが DPoP を告知し、鍵がプログラムより長く残る場合にバインドする。Spring Boot にはない。
response-mode query、form_post query。apple プロバイダーでは form_post プロバイダーが認可応答を返す方法。リダイレクト URI の中で、またはそこへのフォームの POST で (受け取れるのはサーバーの oauth2Login() だけで、HTTPS でのみ)。Spring Boot にはない。
native-sign-in none、apple apple プロバイダーでは apple、それ以外は none アプリ (spring_boot_starter_flutter_security): apple は、iOS と macOS でブラウザーの代わりに Sign in with Apple のシートでサインインする。コードはその後、登録の token-uri、つまりアプリのサーバーに送られる。Spring Boot にはない。

client-secret-jwt: JWT であるクライアントシークレット

Spring Boot にはありません。各キーは spring.security.oauth2.client.registration.<id>.client-secret-jwt.<key> です。

キー 型 デフォルト 意味
issuer String 必須 JWT の iss (Apple: チーム ID)。
key-id String 必須 JWT の kid (Apple: 鍵 ID)。
private-key-location String なし PKCS #8 の秘密鍵 (Apple の .p8)。file:/run/secrets/apple.p8 や resources:... など。最初のトークンリクエストで読まれ、ログに出ることはない。これか private-key のどちらかが必須。
private-key String なし 場所の代わりの PKCS #8 の PEM そのもの (環境変数から。\n のエスケープを使える)。クレデンシャルなので、決してログに出さない。
audience String プロバイダーの issuer-uri JWT の aud。
lifetime Duration 1h 各 JWT の有効期間。最短 1m、最長 180d。残りが 10 分の 1 になると作り直される。

プロバイダー

各キーは spring.security.oauth2.client.provider.<id>.<key> です。既知のプロバイダー (google、github、facebook、okta、apple) は、Okta の issuer-uri を除いて、どれも必要としません。

キー 型 デフォルト 意味
issuer-uri String なし プロバイダー。他のエンドポイントはその OpenID (または OAuth 2.0) のメタデータから来て、その issuer はこれと等しくなければならない。起動時ではなく最初の使用時に検出される。
authorization-uri String メタデータから プロバイダーのログインページ。
token-uri String メタデータから トークンエンドポイント。
user-info-uri String メタデータから ユーザー情報エンドポイント。
jwk-set-uri String メタデータから プロバイダーの公開鍵。サーバーはこれで ID トークンの署名をチェックする。
user-info-authentication-method header、form、query header アクセストークンをユーザー情報エンドポイントに送る方法。
user-name-attribute String issuer-uri があれば sub ユーザーを表す属性 (sub、login、email)。

Feign クライアント

キー 型 デフォルト 意味
spring.feign.oauth2.enabled bool false Feign クライアントが OAuth2 クライアントの登録のアクセストークンを送るかどうか。
spring.feign.oauth2.client-registration-id String なし Feign クライアントがそのトークンを送る登録。なければ Feign クライアントの名前が登録 ID になる。

値の書き方

Spring (Java) との対応

registration.* と provider.* のキーは Spring Boot のもので、意味も同じです。dpop、response-mode、native-sign-in、client-secret-jwt.* は追加されたものです。Feign のキーは、Spring Cloud OpenFeign のものから cloud.openfeign を除いたものです。

関連項目