のコード例 AWS WAF モバイル SDK - AWS WAF, AWS Firewall Managerおよび AWS Shield Advanced

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

のコード例 AWS WAF モバイル SDK

このセクションでは、モバイル を使用するためのコード例を示しますSDK。

トークンプロバイダーの初期化とトークンの取得

設定オブジェクトを使用して、トークンプロバイダーインスタンスを開始します。その後、使用可能なオペレーションを使用してトークンを取得できます。必要なコードの基本コンポーネントを次に示します。

iOS
let url: URL = URL(string: "Web ACL integration URL")! let configuration = WAFConfiguration(applicationIntegrationUrl: url, domainName: "Domain name") let tokenProvider = WAFTokenProvider(configuration) //onTokenReady can be add as an observer for UIApplication.willEnterForegroundNotification self.tokenProvider.onTokenReady() { token, error in if let token = token { //token available } if let error = error { //error occurred after exhausting all retries } } //getToken() let token = tokenProvider.getToken()
Android

Java の例:

String applicationIntegrationURL = "Web ACL integration URL"; //Or URL applicationIntegrationURL = new URL("Web ACL integration URL"); String domainName = "Domain name"; WAFConfiguration configuration = WAFConfiguration.builder().applicationIntegrationURL(applicationIntegrationURL).domainName(domainName).build(); WAFTokenProvider tokenProvider = new WAFTokenProvider(Application context, configuration); // implement a token result callback WAFTokenResultCallback callback = (wafToken, error) -> { if (wafToken != null) { // token available } else { // error occurred in token refresh } }; // Add this callback to application creation or activity creation where token will be used tokenProvider.onTokenReady(callback); // Once you have token in token result callback // if background refresh is enabled you can call getToken() from same tokenprovider object // if background refresh is disabled you can directly call getToken()(blocking call) for new token WAFToken token = tokenProvider.getToken();

Kotlin の例:

import com.amazonaws.waf.mobilesdk.token.WAFConfiguration import com.amazonaws.waf.mobilesdk.token.WAFTokenProvider private lateinit var wafConfiguration: WAFConfiguration private lateinit var wafTokenProvider: WAFTokenProvider private val WAF_INTEGRATION_URL = "Web ACL integration URL" private val WAF_DOMAIN_NAME = "Domain name" fun initWaf() { // Initialize the tokenprovider instance val applicationIntegrationURL = URL(WAF_INTEGRATION_URL) wafConfiguration = WAFConfiguration.builder().applicationIntegrationURL(applicationIntegrationURL) .domainName(WAF_DOMAIN_NAME).backgroundRefreshEnabled(true).build() wafTokenProvider = WAFTokenProvider(getApplication(), wafConfiguration) // getToken from tokenprovider object println("WAF: "+ wafTokenProvider.token.value) // implement callback for where token will be used wafTokenProvider.onTokenReady { wafToken, sdkError -> run { println("WAF Token:" + wafToken.value) } } }

setTokenCookieTRUE である場合、トークンプロバイダーは、tokenCookiePath で指定されたパスの下のすべての場所に対するウェブリクエストにトークン cookie を含めます。デフォルトでは、setTokenCookieTRUEtokenCookiePath/ です。

トークン cookie のパスを指定することで、トークン cookie を含むリクエストの範囲を絞り込むことができます (例: /web/login)。これを行う場合は、 AWS WAF ルールは、他のパスに送信するリクエスト内のトークンを検査しません。AWSManagedRulesACFPRuleSet ルールグループを使用する場合、アカウントの登録パスと作成パスを設定すると、ルールグループはそれらのパスに送信されるリクエスト内のトークンをチェックします。詳細については、「ACFP マネージドルールグループをウェブに追加する ACL」を参照してください。同様に、AWSManagedRulesATPRuleSet ルールグループを使用する場合は、ログインパスを設定し、ルールグループはそのパスに送信されるリクエストのトークンをチェックします。詳細については、「ATP マネージドルールグループをウェブに追加する ACL」を参照してください。

iOS

setTokenCookie が の場合TRUE、トークンプロバイダーは を保存します。 AWS WAF の トークンHTTPCookieStorage.sharedと は、 で指定したドメインへのリクエストに Cookie を自動的に含めますWAFConfiguration

let request = URLRequest(url: URL(string: domainEndpointUrl)!) //The token cookie is set automatically as cookie header let task = URLSession.shared.dataTask(with: request) { data, urlResponse, error in }.resume()
Android

setTokenCookie が の場合TRUE、トークンプロバイダーは を保存します。 AWS WAF アプリケーション全体で共有されているCookieHandlerインスタンス内の トークン。トークンプロバイダーは、WAFConfiguration で指定したドメインへのリクエストに cookie を自動的に含めます。

Java の例:

URL url = new URL("Domain name"); //The token cookie is set automatically as cookie header HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.getResponseCode();

Kotlin の例:

val url = URL("Domain name") //The token cookie is set automatically as cookie header val connection = (url.openConnection() as HttpsURLConnection) connection.responseCode

CookieHandler デフォルトインスタンスが既に初期化されている場合、トークンプロバイダーは、それを使用して cookie を管理します。そうでない場合、トークンプロバイダーは を使用して新しいCookieManagerインスタンスを初期化します。 AWS WAF トークン と CookiePolicy.ACCEPT_ORIGINAL_SERVER を に設定し、この新しいインスタンスを のデフォルトインスタンスとして設定しますCookieHandler

次のコードは、 が Cookie マネージャーと Cookie ハンドラーSDKをアプリで使用できない場合に初期化する方法を示しています。

Java の例:

CookieManager cookieManager = (CookieManager) CookieHandler.getDefault(); if (cookieManager == null) { // Cookie manager is initialized with CookiePolicy.ACCEPT_ORIGINAL_SERVER cookieManager = new CookieManager(); CookieHandler.setDefault(cookieManager); }

Kotlin の例:

var cookieManager = CookieHandler.getDefault() as? CookieManager if (cookieManager == null) { // Cookie manager is initialized with CookiePolicy.ACCEPT_ORIGINAL_SERVER cookieManager = CookieManager() CookieHandler.setDefault(cookieManager) }

setTokenCookie を に設定する場合はFALSE、保護されたエンドポイントへのリクエストで、Cookie HTTPリクエストヘッダーとしてトークン Cookie を手動で指定する必要があります。次のコードは、これを実行する方法を説明しています。

iOS
var request = URLRequest(url: wafProtectedEndpoint) request.setValue("aws-waf-token=token from token provider", forHTTPHeaderField: "Cookie") request.httpShouldHandleCookies = true URLSession.shared.dataTask(with: request) { data, response, error in }
Android

Java の例:

URL url = new URL("Domain name"); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); String wafTokenCookie = "aws-waf-token=token from token provider"; connection.setRequestProperty("Cookie", wafTokenCookie); connection.getInputStream();

Kotlin の例:

val url = URL("Domain name") val connection = (url.openConnection() as HttpsURLConnection) val wafTokenCookie = "aws-waf-token=token from token provider" connection.setRequestProperty("Cookie", wafTokenCookie) connection.inputStream