---
schema_version: "1"
id: d63f1a7a8c711e59750ecf444fcae7c9231e8a4aab391fa5df6fdfa48c1b3e96
title: 基本認証とレート制限を使用した API の保護
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/gateway/1.9/flex-local-secure-api-with-basic-auth-policy.md
canonical_url: https://docs.mulesoft.com/jp/gateway/1.9/flex-local-secure-api-with-basic-auth-policy
source_site: docs.mulesoft.com
content_type: documentation
content_hash: b160a5dee363c33e98317b5db0a5c2e9ee4440ef1ccb5caa257422ed45c7fcf3
description: MuleSoft Documentation Site
release: "1.9"
latest: false
---
# 基本認証とレート制限を使用した API の保護

ローカルモードで Flex Gateway の背後で実行されている API をパブリッシュすることで、YAML 設定データを変更する方法について説明します。

- Linux および Docker: `.yaml`​ ファイル

下記の手順は、​[基本認証](https://docs.mulesoft.com/jp/gateway/1.9/policies-included-basic-auth-simple)​と​[レート制限](https://docs.mulesoft.com/jp/gateway/1.9/policies-included-rate-limiting)​によって保護されている複数のアップストリームサービスを使用した API のシンプルな YAML 設定を適用する方法を示しています。

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><span class="image"><a class="image" href="#publish-an-api-running-behind-flex-gateway-on-linux"><img src="https://docs.mulesoft.com/jp/gateway/1.9/_images/install-linux-logo.png" alt="20%" width="20%"></a></span></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><span class="image"><a class="image" href="#publish-an-api-running-behind-flex-gateway-in-a-docker-container"><img src="https://docs.mulesoft.com/jp/gateway/1.9/_images/install-docker-logo.png" alt="25%" width="25%"></a></span></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#publish-an-api-running-behind-flex-gateway-on-linux">Linux で Flex Gateway の背後で実行されている API のパブリッシュ</a>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="#publish-an-api-running-behind-flex-gateway-in-a-docker-container">Docker コンテナで Flex Gateway の背後で実行されている API のパブリッシュ</a>​</p></td>
</tr>
</tbody>
</table>

## [](#linux-で-flex-gateway-の背後で実行されている-api-のパブリッシュ)Linux で Flex Gateway の背後で実行されている API のパブリッシュ

### [](#始める前に)始める前に

始める前に、次の準備が整っていることを確認します。

- Flex Gateway がインストールされていてローカルモードで実行されている。ゲートウェイのインストールと実行についての詳細は、[Flex Gateway のダウンロード](https://docs.mulesoft.com/jp/gateway/1.9/flex-install)を参照してください。
- アップストリームサービスの URL が用意されている。次の例では架空の ​`products-api`​ と ​`users-api`​ というサービスを使用していますが、​`metadata.name`​ には実際の API 名、​`spec.services`​ にはサービスの詳細を指定することができます。

### [](#api-のパブリッシュ)API のパブリッシュ

1. `.yaml`​ ファイル拡張子で設定ファイルを作成します。
    
    1. ファイルにカスタム名を付けます。
    2. ファイルを Flex Gateway の設定ディレクトリ ​`/etc/mulesoft/flex-gateway/conf.d/custom`​ に保存します。このディレクトリには複数の設定ファイルを保存できます。
2. 次の YAML スニペットをコピーしてファイルに貼り付け、必要な箇所を適切な値に変更します。
    
    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: ApiInstance
    metadata:
      name: products-users-api
    spec:
      address: http://0.0.0.0:8080
      services:
        products:
          address: https://<your products URL>:<your port>/
          routes:
            - rules:
                - path: /products(/.\*)
                - path: /products-featured(/.\*)
              config:
                destinationPath: /api
        users:
          address: https://<your users URL>:<your port>/
          routes:
            - rules:
                - path: /api/users(/.\*)
      policies:
        - policyRef:
            name: http-basic-authentication-flex
          config:
            username: chris
            password: admin
        - policyRef:
            name: rate-limiting-flex
          config:
            exposeHeaders: true
            rateLimits:
              - maximumRequests: 3
                timePeriodInMilliseconds: 6000
            keySelector: "#\[attributes.queryParams\['identifier'\]\]"
3. ファイルを保存します。ゲートウェイによって設定が自動的に更新されます。
4. 次のコマンドを実行してログを表示します。
    
    journalctl -u flex-gateway-\*
    
    応答は次のようになります。
    
    \[agent\]\[info\] Generating config
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding ApiInstance default/products-users-api http://0.0.0.0:8080
    \[agent\]\[info\] Gateway default/4191c977e1ce: Adding Policy default/products-users-api-users-http-basic-authentication-flex-1
    \[agent\]\[info\] Gateway default/4191c977e1ce: Adding Policy default/products-users-api-users-rate-limiting-flex-2
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/products(/.\*) methods: headerConditions:\[\] profile:0xc0030529f0} => {Kind:Service Name:products-users-api-products Namespace:default}
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/users(/.\*) methods: headerConditions:\[\] profile:0xc0030529f0} => {Kind:Service Name:products-users-api-users Namespace:default}
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Policy default/envoy.filters.http.router
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Service default/monitoring\_metrics http://0.0.0.0:9881
    \[agent\]\[debug\] generating service monitoring\_metrics.default.svc hostname: 0.0.0.0 port: 9881
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Service default/products-users-api-products https://<your products URL>:<your port>/
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Service default/products-users-api-users https://<your users URL>:<your port>/
    \[agent\]\[debug\] generating service products-users-api-products.default.svc hostname: <your products URL> port: <your port>
    \[agent\]\[debug\] generating service products-users-api-users.default.svc hostname: <your users URL> port: <your port>
    \[agent\]\[info\] Writing envoy bootstrap configuration to /tmp/envoy.json
    \[envoy\]\[info\] cds: add 2 cluster(s), remove 2 cluster(s)
    \[envoy\]\[info\] cds: added/updated 1 cluster(s), skipped 1 unmodified cluster(s)

基本認証とレート制限によって保護された API が Flex Gateway の背後で実行されています。

## [](#docker-コンテナで-flex-gateway-の背後で実行されている-api-のパブリッシュ)Docker コンテナで Flex Gateway の背後で実行されている API のパブリッシュ

### [](#始める前に-2)始める前に

始める前に、次の準備が整っていることを確認します。

- Flex Gateway がインストールされている。詳細は、[Flex Gateway のダウンロード](https://docs.mulesoft.com/jp/gateway/1.9/flex-install)を参照してください。
- Flex Gateway がローカルモードで登録されて実行されている。詳細は​[「ローカルモードでの登録と実行」](https://docs.mulesoft.com/jp/gateway/1.9/flex-local-reg-run)​を参照してください。
- アップストリームサービスの URL が用意されている。次の例では架空の ​`products-api`​ と ​`users-api`​ というサービスを使用していますが、​`metadata.name`​ には実際の API 名、​`spec.services`​ にはサービスの詳細を指定することができます。

### [](#api-のパブリッシュ-2)API のパブリッシュ

1. ターミナルを開いて、Flex Gateway 設定ファイルが保存されているディレクトリに移動します。このディレクトリは、Flex Gateway を起動したときに指定されています。
2. `.yaml`​ ファイル拡張子で設定ファイルを作成します。
    
    1. ファイルにカスタム名を付けます。
    2. ファイルを保存します。
3. 次の YAML スニペットをコピーしてファイルに貼り付け、必要な箇所を適切な値に変更します。
    
    apiVersion: gateway.mulesoft.com/v1alpha1
    kind: ApiInstance
    metadata:
      name: products-users-api
    spec:
      address: http://0.0.0.0:8080
      services:
        products:
          address: https://<your products URL>:<your port>/
          routes:
            - rules:
                - path: /api/products(/.\*)
                - path: /api/products-featured(/.\*)
              config:
                destinationPath: /api
        users:
          address: https://<your users URL>:<your port>/
          routes:
            - rules:
                - path: /api/users(/.\*)
      policies:
        - policyRef:
            name: http-basic-authentication-flex
          config:
            username: chris
            password: admin
        - policyRef:
            name: rate-limiting-flex
          config:
            exposeHeaders: true
            rateLimits:
              - maximumRequests: 3
                timePeriodInMilliseconds: 6000
            keySelector: "#\[attributes.queryParams\['identifier'\]\]"
4. ファイルを保存します。ゲートウェイによって設定が自動的に更新されます。
5. Docker コンテナログを表示します。内容は次のようになっています。
    
    \[agent\]\[info\] Generating config
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding ApiInstance default/products-users-api http://0.0.0.0:8080
    \[agent\]\[info\] Gateway default/4191c977e1ce: Adding Policy default/products-users-api-users-http-basic-authentication-flex-1
    \[agent\]\[info\] Gateway default/4191c977e1ce: Adding Policy default/products-users-api-users-rate-limiting-flex-2
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/products(/.\*) methods: headerConditions:\[\] profile:0xc0030529f0} => {Kind:Service Name:products-users-api-products Namespace:default}
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Route: &{host: path:/api/users(/.\*) methods: headerConditions:\[\] profile:0xc0030529f0} => {Kind:Service Name:products-users-api-users Namespace:default}
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Policy default/envoy.filters.http.router
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Service default/monitoring\_metrics http://0.0.0.0:9881
    \[agent\]\[debug\] generating service monitoring\_metrics.default.svc hostname: 0.0.0.0 port: 9881
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Service default/products-users-api-products https://<your products URL>:<your port>/
    \[agent\]\[info\] Gateway default/18b4e890fe7d: Adding Service default/products-users-api-users https://<your users URL>:<your port>/
    \[agent\]\[debug\] generating service products-users-api-products.default.svc hostname: <your products URL> port: <your port>
    \[agent\]\[debug\] generating service products-users-api-users.default.svc hostname: <your users URL> port: <your port>
    \[agent\]\[info\] Writing envoy bootstrap configuration to /tmp/envoy.json
    \[envoy\]\[info\] cds: add 2 cluster(s), remove 2 cluster(s)
    \[envoy\]\[info\] cds: added/updated 1 cluster(s), skipped 1 unmodified cluster(s)

基本認証とレート制限によって保護された API が Flex Gateway の背後で実行されています。

## [](#関連情報)関連情報

- [インバウンドポリシーディレクトリ](https://docs.mulesoft.com/jp/gateway/1.9/policies-included-directory)