ios - 应用程序重新启动后的 AWS ExpiredTokenException
<p><p>我正在构建一个 iOS (Swift) 应用程序,使用 AWS 作为后端并带有开发人员身份验证。一切正常,直到我关闭应用程序,离开一段时间然后重新启动。在这种情况下,我经常(但并非总是)在尝试从 AWS 检索数据时收到 ExpiredTokenException 错误。</p>
<p>这是我的代码:</p>
<pre><code>class DeveloperAuthenticatedIdentityProvider: AWSAbstractCognitoIdentityProvider {
var _token: String!
var _logins: [ NSObject : AnyObject ]!
override var token: String {
get {
return _token
}
}
override var logins: [ NSObject : AnyObject ]! {
get {
return _logins
}
set {
_logins = newValue
}
}
override func getIdentityId() -> AWSTask! {
if self.identityId != nil {
return AWSTask(result: self.identityId)
} else {
return AWSTask(result: nil).continueWithBlock({ (task) -> AnyObject! in
if self.identityId == nil {
return self.refresh()
}
return AWSTask(result: self.identityId)
})
}
}
override func refresh() -> AWSTask! {
let apiUrl = "https://url-goes-here" // call my server to retrieve an OpenIdToken
request.GET(apiUrl, parameters: nil, progress: nil,
success: {
(task: NSURLSessionDataTask, response: AnyObject?) -> Void in
let tmp = NSMutableDictionary()
tmp.setObject("temp", forKey: "ExampleApp")
self.logins = tmp as [ NSObject : AnyObject ]
let jsonDictionary = response as! NSDictionary
self.identityId = jsonDictionary["identityId"] as! String
self._token = jsonDictionary["token"] as! String
awstask.setResult(response)
},
failure: {
(task: NSURLSessionDataTask?, error: NSError) -> Void in
awstask.setError(error)
}
)
return awstask.task
}
}
</code></pre>
<p>在 AppDelegate 中:</p>
<pre><code>func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -> Bool {
let identityProvider = DeveloperAuthenticatedIdentityProvider()
// set default service configuration
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: cognitoRegion, identityProvider: identityProvider, unauthRoleArn: unauthRole, authRoleArn: authRole)
let configuration = AWSServiceConfiguration(region: defaultServiceRegion, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
// set service configuration for S3 (my bucket is located in a different region to my Cognito and Lambda service)
let credentialsProviderForS3 = AWSCognitoCredentialsProvider(regionType: cognitoRegion, identityProvider: identityProvider, unauthRoleArn: unauthRole, authRoleArn: unauthRole)
let awsConfigurationForS3 = AWSServiceConfiguration(region: s3ServiceRegion, credentialsProvider: credentialsProviderForS3)
AWSS3TransferUtility.registerS3TransferUtilityWithConfiguration(awsConfigurationForS3, forKey: "S3")
return true
}
</code></pre>
<p>这个 <a href="http://direct.amorf.net/index.cgi/00/https/forums.aws.amazon.com/thread.jspa=3fmessageID=3d711931" rel="noreferrer noopener nofollow">post</a>表明 Cognitotoken 已过期,由开发人员手动刷新。这似乎过于复杂,因为它需要设置一个定期刷新的计时器,处理应用程序关闭和重新启动,以及处理刷新时发生的 AWS 请求。有没有更简单的方法?例如,是否可以让 AWS 开发工具包在尝试使用过期 token 查询服务器时自动调用刷新?</p>
<p>任何帮助将不胜感激。我正在使用适用于 iOS 的 AWS 开发工具包 2.3.5 版。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>适用于 iOS 2.4.x 的 AWS 移动开发工具包有一个名为 <a href="https://github.com/aws/aws-sdk-ios/blob/master/AWSCore/Authentication/AWSIdentityProvider.h#L66" rel="noreferrer noopener nofollow"><code>AWSIdentityProviderManager</code></a> 的新协议(protocol).它有以下方法:</p>
<pre><code>/**
* Each entry in logins represents a single login with an identity provider.
* The key is the domain of the login provider (e.g. 'graph.facebook.com') and the value is the
* OAuth/OpenId Connect token that results from an authentication with that login provider.
*/
- (AWSTask<NSDictionary<NSString *, NSString *> *> *)logins;
</code></pre>
<p>符合此协议(protocol)的对象的职责是在请求时返回有效的<code>logins</code> 字典。由于此方法是异步的,因此如果缓存的 token 过期,您可以在其中进行网络调用。实现取决于您,但在许多情况下,<code>AWSIdentityProviderManager</code> 管理多个 <a href="https://github.com/aws/aws-sdk-ios/blob/master/AWSCore/Authentication/AWSIdentityProvider.h#L52" rel="noreferrer noopener nofollow"><code>AWSIdentityProvider</code></a> s,聚合它们并返回 <code>logins</code> 字典。</p></p>
<p style="font-size: 20px;">关于ios - 应用程序重新启动后的 AWS ExpiredTokenException,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/37127831/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/37127831/
</a>
</p>
页:
[1]