---
schema_version: "1"
id: f2bc0e10d53c3d02dce9613ee57228f0f52b73bac2a201110fec0c333c191b3d
title: 認証ハンドラー
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/mule-sdk/1.1/authentication-handler.md
canonical_url: https://docs.mulesoft.com/jp/mule-sdk/1.1/authentication-handler
source_site: docs.mulesoft.com
content_type: documentation
content_hash: c8652d323fcc11259df5a3410578f223f870d9a9708e6e924953373b92038ab3
description: MuleSoft Documentation Site
release: "1.1"
latest: false
---
# 認証ハンドラー

このハンドラーでは、特定の要求に基づいて、暗号化および着信認証に使用される現在のコンテキストの認証を設定できます。

## [](#認証ハンドラーの使用方法)認証ハンドラーの使用方法

操作に ​`AuthenticationHandler`​ 型のパラメーターを追加すると、現在のコンテキストの認証ハンドラーインスタンスが実行時に挿入されます。このインスタンスを使用して現在の認証 (存在する場合) を確認し、新しい認証を設定できます。

認証ハンドラーインターフェースには次のメソッドがあります。

- `createCredentialsBuilder`​: `Credentials`​ インスタンスの作成に使用されるビルダーを返す。
- `createAuthentication`​: `Credentials`​ のインスタンスが指定されたときに、​`Authentication`​ インスタンスを作成する。
- `setAuthentication`​: `Authentication`​ が指定されたときに、その値を現在のコンテキストの認証として設定する。必要に応じて、​`SecurityManager`​ に追加されるセキュリティプロバイダー (名前) のリストを指定することもできます。
- `getAuthentication`​: 現在のコンテキストから ​`Authentication`​ を返す。現在のコンテキストに ​`Authentication`​ がない場合もあります。

## [](#使用例)使用例

HTTP 要求を受信した後に、現在のコンテキストの認証ハンドラーでログイン情報を認証して保存するとします。このユースケースでは、HTTP Connector の ​`basic-security-filter`​ 操作を使用できます。

次の簡略化された例は、​`basic-security-filter`​ の実装方法を示しています。

```java
public class HttpOperations {

  /**
   * Authenticates received HTTP requests. Must be used after a listener component.
   */
  @Throws(BasicSecurityErrorTypeProvider.class)
  public void basicSecurityFilter(@ParameterGroup(name = "Security Filter") HttpBasicAuthenticationFilter filter,
                                  AuthenticationHandler authenticationHandler) { (1)
    try {
      filter.authenticate(authenticationHandler);
    } catch (BasicUnauthorisedException e) {
      throw new ModuleException(BASIC_AUTHENTICATION, e);
    } catch (SecurityProviderNotFoundException | SecurityException | UnknownAuthenticationTypeException e) {
      throw new ModuleException(SERVER_SECURITY, e);
    }
  }
 // ...
}
```

<table><tbody><tr><td><i class="conum" data-value="1"></i><b>1</b></td><td>この操作には、実行時に挿入される ​<code>AuthenticationHandler</code>​ パラメーターがあります。</td></tr></tbody></table>

```java
public class HttpBasicAuthenticationFilter {

  @Parameter
  private String realm;

  @Parameter
  @Optional
  @NullSafe
  private List<String> securityProviders;

  @Parameter
  @Optional(defaultValue = "#[attributes]")
  HttpRequestAttributes attributes; (1)

  public void authenticate(AuthenticationHandler authenticationHandler)
      throws SecurityException, SecurityProviderNotFoundException, UnknownAuthenticationTypeException {
    String header = attributes.getHeaders().get("Authorization".toLowerCase());

    if ((header != null) && header.startsWith("Basic ")) {

      String username = retrieveUsername(header);
      String password = retrievePassword(header);


      Credentials credentials = authenticationHandler.createCredentialsBuilder() (2)
          .withUsername(username)
          .withPassword(password.toCharArray())
          .build();

      try {
        Authentication authentication = authenticationHandler.createAuthentication(credentials); (3)
        authenticationHandler.setAuthentication(securityProviders, authentication); (4)
      } catch (UnauthorisedException e) { (5)
        throw new BasicUnauthorisedException(authFailedForUser(username), e);
      }

    } else if (header == null) {
      throw new BasicUnauthorisedException(null, "HTTP basic authentication", "HTTP listener");
    } else {
      throw new UnsupportedAuthenticationSchemeException("Http Basic filter doesn't know how to handle header " + header));
    }
  }

}
```

<table><tbody><tr><td><i class="conum" data-value="1"></i><b>1</b></td><td>ユーザー名とパスワードが取得される場所の ​<code>HttpRequestAttributes</code>​。</td></tr><tr><td><i class="conum" data-value="2"></i><b>2</b></td><td><code>Credentials</code>​ は、要求から取得した情報を使用して作成されます。</td></tr><tr><td><i class="conum" data-value="3"></i><b>3</b></td><td><code>Authentication</code>​ は ​<code>Credentials</code>​ に基づいて作成されます。</td></tr><tr><td><i class="conum" data-value="4"></i><b>4</b></td><td>認証情報が ​<code>securityProviders</code>​ に対してチェックされ、ハンドラーに 設定されます。</td></tr><tr><td><i class="conum" data-value="5"></i><b>5</b></td><td>認証情報が無効な場合、例外がスローされます。</td></tr></tbody></table>

認証ハンドラーをパラメーターとして持つフロー内に別の操作がある場合、挿入されるインスタンスは、​`basic-authentication-filter`​ 操作で処理されるインスタンスと同じになります。これにより、​`getAuthentication`​ メソッドを使用して、HTTP 要求からログイン情報と共に ​`Authentication`​ を取得します。