edit-icon download-icon

Use STS token as user credentials

Last Updated: May 27, 2018

Using your Alibaba Cloud AccessKey directly to develop applications will have potential security risk. To enhance your account security, you can use the Security Token Service (STS) token issued for a subaccount to access Alibaba Cloud services.

Using the STS token as an access credential has the following advantages:

  • Using STS token will reduce the risks of a compromised AccessKey ID and AccessKey Secret, particularly reducing risks for your mobile devices.

  • STS token has flexible permission control. You can control the access permission in a finer granularity for products including SLB and ECS, according to the RAM role.

This document illustrates how to set up the STS token in the Java SDK to allow access to Alibaba Cloud services. For more information about STS token, see RAM and STS.

Note: Make sure that the product you are calling support STS.

Two methods are available to set up the STS token:

Use STS token directly

You can directly specify the STS token using the StsTokenCredential method. However, you must update the token periodically if you directly specify the token.

The StsTokenCredential method is as follows:

  1. import com.aliyuncs.DefaultAcsClient;
  2. import com.aliyuncs.auth.BasicSessionCredentials;
  3. import com.aliyuncs.ecs.model.v20140526.DescribeInstancesRequest;
  4. import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse;
  5. import com.aliyuncs.exceptions.ClientException;
  6. import com.aliyuncs.profile.DefaultProfile;
  7. public class SimpleSTSTokenSample {
  8. public static void main(String[] args) {
  9. BasicSessionCredentials credentials = new BasicSessionCredentials(
  10. "<your-access-key-id>",
  11. "<your-access-key-secret>",
  12. "<your-session-token>"
  13. );
  14. DefaultProfile profile = DefaultProfile.getProfile("<your-region-id>");
  15. DefaultAcsClient client = new DefaultAcsClient(profile, credentials);
  16. DescribeInstancesRequest request = new DescribeInstancesRequest();
  17. try {
  18. DescribeInstancesResponse response = client.getAcsResponse(request);
  19. } catch (ClientException e) {
  20. System.err.println(e.toString());
  21. }
  22. }
  23. }

where:

  • region-id is the ID of the region that you are using. See Regions and zones to obtain the region ID.

  • sts-access-key-id, sts-access-key-secret, and sts-session-token are credentials returned by the AssumeRole API.

Use SDK to manage STS tokens

You can create a new STSAssumeRoleSessionCredentialsProvider object to allow Alibaba Cloud Java SDK to create and maintain STS tokens.

The STSAssumeRoleSessionCredentialsProvider object is as follows:

  1. import com.aliyuncs.DefaultAcsClient;
  2. import com.aliyuncs.auth.BasicCredentials;
  3. import com.aliyuncs.auth.STSAssumeRoleSessionCredentialsProvider;
  4. import com.aliyuncs.ecs.model.v20140526.DescribeInstancesRequest;
  5. import com.aliyuncs.ecs.model.v20140526.DescribeInstancesResponse;
  6. import com.aliyuncs.exceptions.ClientException;
  7. import com.aliyuncs.profile.DefaultProfile;
  8. public class UseRoleArnSample {
  9. public static void main(String[] args) {
  10. DefaultProfile profile = DefaultProfile.getProfile("<your-region-id>");
  11. BasicCredentials basicCredentials = new BasicCredentials(
  12. "<your-access-key-id>",
  13. "<your-access-key-secret>"
  14. );
  15. STSAssumeRoleSessionCredentialsProvider provider = new STSAssumeRoleSessionCredentialsProvider(
  16. basicCredentials,
  17. "<your-role-arn>",
  18. profile
  19. );
  20. DefaultAcsClient client = new DefaultAcsClient(profile, provider);
  21. DescribeInstancesRequest request = new DescribeInstancesRequest();
  22. try {
  23. DescribeInstancesResponse response = client.getAcsResponse(request);
  24. } catch (ClientException e) {
  25. System.err.println(e.toString());
  26. }
  27. }
  28. }

where:

  • role-arn is the role resource descriptor. You can obtain it on the Role Details page from the RAM console.

  • role-session-name is a temporary role name. You can call the AssumeRole API to create a temporary identity. After the temporary identity is created, you can use the value set for the role-session-name parameter when calling the API.

Thank you! We've received your feedback.