AuthUsingTempFederationTokenJava
Making Requests Using Federated User Temporary Credentials - AWS SDK for Java
You can provide temporary security credentials for your federated users and applications so that they can send authenticated requests to access your AWS resources. When requesting these temporary credentials, you must provide a user name and an IAM policy that describes the resource permissions that you want to grant. By default, the session duration is one hour. You can explicitly set a different duration value when requesting the temporary security credentials for federated users and applications.
Note
For added security when requesting temporary security credentials for federated users and applications, we recommend that you use a dedicated IAM user with only the necessary access permissions. The temporary user you create can never get more permissions than the IAM user who requested the temporary security credentials. For more information, see AWS Identity and Access Management FAQs .
To provide security credentials and send authenticated request to access resources, do the following:
- Create an instance of the
AWSSecurityTokenServiceClient
class. For information about providing credentials, see Using the AWS SDK for Java. - Start a session by calling the
getFederationToken()
method of the Security Token Service (STS) client. Provide session information, including the user name and an IAM policy, that you want to attach to the temporary credentials. You can provide an optional session duration. This method returns your temporary security credentials. - Package the temporary security credentials in an instance of the
BasicSessionCredentials
object. You use this object to provide the temporary security credentials to your Amazon S3 client. - Create an instance of the
AmazonS3Client
class using the temporary security credentials. You send requests to Amazon S3 using this client. If you send requests using expired credentials, Amazon S3 returns an error.
Example
The example lists keys in the specified S3 bucket. In the example, you obtain temporary security credentials for a two-hour session for your federated user and use the credentials to send authenticated requests to Amazon S3. To run the example, you need to create an IAM user with an attached policy that allows the user to request temporary security credentials and list your AWS resources. The following policy accomplishes this:
For more information about how to create an IAM user, see Creating Your First IAM User and Administrators Group in the IAM User Guide.
After creating an IAM user and attaching the preceding policy, you can run the following example. For instructions on creating and testing a working sample, see Testing the Amazon S3 Java Code Examples.