The English version of quarkus.io is the official project site. Translated sites are community supported on a best-effort basis.

クロスオリジンリソース共有

クロスオリジンリソース共有 (CORS) は、HTTPヘッダーベースのメカニズムで、ブラウザがリソースの読み込みを許可すべきオリジンを、サーバーが自分以外のオリジンに示すことを可能にします。

これらのオリジンは1つのドメイン、スキーム、ポートで構成されています。 完全なオリジンの定義については、 ウェブオリジンのコンセプト のページを参照してください。

CORS フィルター

Quarkusには、 jakarta.servlet.Filter インタフェースを実装し、すべての受信HTTPリクエストをインターセプトするCORSフィルタがあります。 Quarkusの設定ファイル src/main/resources/application.properties で有効にできます:

quarkus.http.cors=true

フィルタが有効で、HTTPリクエストがクロスオリジンであると識別されると、CORSポリシーが適用されます。 また、サーブレット、Jakarta REST リソース、または他のエンドポイントのような意図された宛先にリクエストを転送する前に、以下のプロパティで構成されたヘッダを追加します。

ビルド時に固定される設定プロパティ - その他の設定プロパティは実行時にオーバーライド可能です。

Configuration property

デフォルト

Origins allowed for CORS Comma separated list of valid URLs, e.g.: http://www.quarkus.io,http://localhost:3000 In case an entry of the list is surrounded by forward slashes, it is interpreted as a regular expression.

Environment variable: QUARKUS_HTTP_CORS_ORIGINS

Show more

list of string

HTTP methods allowed for CORS Comma separated list of valid methods. ex: GET,PUT,POST The filter allows any method if this is not set. default: returns any requested method as valid

Environment variable: QUARKUS_HTTP_CORS_METHODS

Show more

list of string

HTTP headers allowed for CORS Comma separated list of valid headers. ex: X-Custom,Content-Disposition The filter allows any header if this is not set. default: returns any requested header as valid

Environment variable: QUARKUS_HTTP_CORS_HEADERS

Show more

list of string

HTTP headers exposed in CORS Comma separated list of valid headers. ex: X-Custom,Content-Disposition default: empty

Environment variable: QUARKUS_HTTP_CORS_EXPOSED_HEADERS

Show more

list of string

The Access-Control-Max-Age response header value indicating how long the results of a pre-flight request can be cached.

Environment variable: QUARKUS_HTTP_CORS_ACCESS_CONTROL_MAX_AGE

Show more

Duration

The Access-Control-Allow-Credentials header is used to tell the browsers to expose the response to front-end JavaScript code when the request’s credentials mode Request.credentials is “include”. The value of this header will default to true if quarkus.http.cors.origins property is set and there is a match with the precise Origin header.

Environment variable: QUARKUS_HTTP_CORS_ACCESS_CONTROL_ALLOW_CREDENTIALS

Show more

boolean

期間フォーマットについて

To write duration values, use the standard java.time.Duration format. See the Duration#parse() Java API documentation for more information.

数字で始まる簡略化した書式を使うこともできます:

  • 数値のみの場合は、秒単位の時間を表します。

  • 数値の後に ms が続く場合は、ミリ秒単位の時間を表します。

その他の場合は、簡略化されたフォーマットが解析のために java.time.Duration フォーマットに変換されます:

  • 数値の後に hms が続く場合は、その前に PT が付けられます。

  • 数値の後に d が続く場合は、その前に P が付けられます。

  1. 許可されたオリジンを定義す る 正規表現を含む完全な CORS フ ィ ル タ ー設定の例

quarkus.http.cors=true
quarkus.http.cors.origins=http://foo.com,http://www.bar.io,/https://([a-z0-9\\-_]+)\\\\.app\\\\.mydomain\\\\.com/
quarkus.http.cors.methods=GET,PUT,POST
quarkus.http.cors.headers=X-Custom
quarkus.http.cors.exposed-headers=Content-Disposition
quarkus.http.cors.access-control-max-age=24H
quarkus.http.cors.access-control-allow-credentials=true

/https://([a-z0-9\\-_]+)\\\\.app\\\\.mydomain\\\\.com/ はスラッシュで囲まれているため、正規表現として扱われます。

application.properties ファイルで正規表現を使用する場合、4つのバックスラッシュを使用して、 . およびその他の正規表現メタデータ文字を通常の文字として表すようにしてください。たとえば、 \\\\.. 文字を表し、 \\. は任意の文字を許可するメタデータ文字を表します。

開発モードですべてのオリジンをサポート

CORSのサポートが必要なQuarkusアプリケーションを開発する際に、必要なオリジンを設定するのは難しい場合があります。 そのような場合は、まず実際の開発に集中するために、devモードでのみすべてのoriginを許可することを検討してください:

quarkus.http.cors=true
%dev.quarkus.http.cors.origins=/.*/

devプロファイルに対してのみ、すべてのoriginを有効にします。 本番環境ですべてのオリジンを許可することは、重大なセキュリティリスクにつながる可能性があるため、お勧めできません。

関連コンテンツ