翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
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)
}
}
}
SDK による HTTP リクエストでのトークン cookie の提供の許可
setTokenCookie
が TRUE
である場合、トークンプロバイダーは、tokenCookiePath
で指定されたパスの下のすべての場所に対するウェブリクエストにトークン cookie を含めます。デフォルトでは、setTokenCookie
は TRUE
、tokenCookiePath
は /
です。
トークン cookie のパスを指定することで、トークン cookie を含むリクエストの範囲を絞り込むことができます (例: /web/login
)。これを行う場合は、AWS WAF ルールが他のパスに送信するリクエストのトークンを検査しないことを確認してください。AWSManagedRulesACFPRuleSet
ルールグループを使用する場合、アカウントの登録パスと作成パスを設定すると、ルールグループはそれらのパスに送信されるリクエスト内のトークンをチェックします。詳細については、「ACFP マネージドルールグループをウェブ ACL に追加」を参照してください。同様に、AWSManagedRulesATPRuleSet
ルールグループを使用する場合は、ログインパスを設定し、ルールグループはそのパスに送信されるリクエストのトークンをチェックします。詳細については、「ATP マネージドルールグループをウェブ ACL に追加」を参照してください。
- iOS
-
setTokenCookie
が TRUE
である場合、トークンプロバイダーは AWS WAF トークンを HTTPCookieStorage.shared
に格納し、WAFConfiguration
で指定したドメインに対するリクエストに cookie を自動的に含めます。
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
である場合、トークンプロバイダーは、アプリケーション全体で共有される CookieHandler
インスタンスに AWS WAF トークンを格納します。トークンプロバイダーは、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 を管理します。初期化されていない場合、トークンプロバイダーは AWS WAF トークンおよび CookiePolicy.ACCEPT_ORIGINAL_SERVER
を使用して新しい CookieManager
インスタンスを初期化し、この新しいインスタンスを 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)
}
HTTP リクエストにおけるトークン cookie の手動による提供
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