Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门。本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
为Amazon WAFMobile S
本部分提供使用移动 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();
允许软件开发工具包在你的 HTTP 请求中提供令牌 cookie
如果setTokenCookie是TRUE,令牌提供商在您向指定路径下的所有位置发出的网络请求中包含您的令牌 cookietokenCookiePath. 默认情况下,setTokenCookie是TRUE和tokenCookiePath是/.
您可以通过指定令牌 cookie 路径来缩小包含令牌 cookie 的请求的范围,例如,/web/login. 如果您这样做,请检查您的Amazon WAF规则不会检查您发送到其他路径的请求中是否有令牌。当您使用以下应用程序时:AWSManagedRulesATPRuleSet规则组,您可以配置登录路径,规则组检查发送到该路径的请求中是否有令牌。有关更多信息,请参阅 使用 ATP 托管规则组。
- 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
-
当setTokenCookie是TRUE,代币提供商存储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。否则,令牌提供者将初始化一个新的CookieManager实例使用Amazon WAF令牌和CookiePolicy.ACCEPT_ORIGINAL_SERVER然后将这个新实例设置为中的默认实例CookieHandler.
以下代码显示了 SDK 如何在 Cookie 管理器和 Cookie 处理程序在您的应用程序中不可用时对其进行初始化。
CookieManager cookieManager = (CookieManager) CookieHandler.getDefault();
if (cookieManager == null) {
// Cookie manager is initialized with CookiePolicy.ACCEPT_ORIGINAL_SERVER
cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
}
在您的 HTTP 请求中手动提供令牌 cookie
如果你设置setTokenCookie到FALSE,那么您需要在向受保护终端节点发出的请求中手动提供令牌 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();