为Amazon WAF移动 SDK 编写代码 - Amazon WAF、Amazon Firewall Manager 和 Amazon Shield Advanced
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

为Amazon 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
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();

如果setTokenCookieTRUE,则令牌提供商会在您向中指定的路径下的所有位置发出的 Web 请求中包含代币 cookietokenCookiePath。默认情况下,setTokenCookieTRUEtokenCookiePath/

例如,您可以通过指定令牌 cookie 路径来缩小包含令牌 cookie 的请求的范围/web/login。如果您这样做,请检查您的Amazon WAF规则是否检查您发送到其他路径的请求中是否有令牌。使用AWSManagedRulesATPRuleSet规则组时,需要配置登录路径,规则组会检查发送到该路径的请求中的令牌。有关更多信息,请参阅将 ATP 托管规则组添加到您的 Web ACL

iOS

setTokenCookie为时TRUE,令牌提供者将Amazon 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

如果setTokenCookieTRUE,则令牌提供者将Amazon WAF令牌存储在应用程序范围内共享的CookieHandler实例中。令牌提供商会自动将 cookie 包含在对您在中指定的域的请求中WAFConfiguration

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

如果您已经初始化了CookieHandler默认实例,则令牌提供者将使用它来管理 Cookie。否则,令牌提供者将使用该令Amazon WAF牌初始化一个新CookieManager实例,CookiePolicy.ACCEPT_ORIGINAL_SERVER然后将此新实例设置为中的默认实例CookieHandler

以下代码显示了 Cookie 管理器和 Cookie 处理器在您的应用程序中不可用时,SDK 如何对其进行初始化。

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

如果您设置为FALSE,则需要在setTokenCookie向受保护终端节点发送的请求中手动提供令牌 cookie,作为 Cookie HTTP 请求标头。以下代码演示了如何执行该操作。

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
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();