阅读764 返回首页    go 阿里云


使用入门__Java SDK_STS SDK使用手册_访问控制-阿里云

创建和管理角色

STS的AssumeRole接口的使用,需要在RAM中创建和管理角色,请参考RAM角色管理

使用maven创建项目

  1. mvn archetype:generate -DgroupId=com.aliyun.sts.sample
  2. -DartifactId=sts-sdk-sample
  3. -Dpackage=com.aliyun.sts.sample
  4. -Dversion=1.0-SNAPSHOT

修改生成的pom.xml, 添加aliyun-java-sdk的相关依赖。以2.1.6版本为例,在 标签内加入如下内容:

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-sts</artifactId>
  4. <version>2.1.6</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.aliyun</groupId>
  8. <artifactId>aliyun-java-sdk-core</artifactId>
  9. <version>2.1.7</version>
  10. </dependency>

aliyun-java-sdk已经加入到 https://maven-repository.com/artifact/com.aliyun

无需设置maven的settings.xml。

aliyun-java-sdk-sts使用的示例代码

  • 注意: 请修改accessKeyId和accessKeySecret为有效值。
  • 在com/aliyun/sts/sample/目录下创建Java源代码StsServiceSample.java,内容如下:
  1. package com.aliyun.sts.sample;
  2. import com.aliyuncs.DefaultAcsClient;
  3. import com.aliyuncs.exceptions.ClientException;
  4. import com.aliyuncs.http.MethodType;
  5. import com.aliyuncs.http.ProtocolType;
  6. import com.aliyuncs.profile.DefaultProfile;
  7. import com.aliyuncs.profile.IClientProfile;
  8. import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
  9. import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
  10. public class StsServiceSample {
  11. // 目前只有"cn-hangzhou"这个region可用, 不要使用填写其他region的值
  12. public static final String REGION_CN_HANGZHOU = "cn-hangzhou";
  13. // 当前 STS API 版本
  14. public static final String STS_API_VERSION = "2015-04-01";
  15. static AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret,
  16. String roleArn, String roleSessionName, String policy,
  17. ProtocolType protocolType) throws ClientException {
  18. try {
  19. // 创建一个 Aliyun Acs Client, 用于发起 OpenAPI 请求
  20. IClientProfile profile = DefaultProfile.getProfile(REGION_CN_HANGZHOU, accessKeyId, accessKeySecret);
  21. DefaultAcsClient client = new DefaultAcsClient(profile);
  22. // 创建一个 AssumeRoleRequest 并设置请求参数
  23. final AssumeRoleRequest request = new AssumeRoleRequest();
  24. request.setVersion(STS_API_VERSION);
  25. request.setMethod(MethodType.POST);
  26. request.setProtocol(protocolType);
  27. request.setRoleArn(roleArn);
  28. request.setRoleSessionName(roleSessionName);
  29. request.setPolicy(policy);
  30. // 发起请求,并得到response
  31. final AssumeRoleResponse response = client.getAcsResponse(request);
  32. return response;
  33. } catch (ClientException e) {
  34. throw e;
  35. }
  36. }
  37. public static void main(String[] args) {
  38. // 只有 RAM用户(子账号)才能调用 AssumeRole 接口
  39. // 阿里云主账号的AccessKeys不能用于发起AssumeRole请求
  40. // 请首先在RAM控制台创建一个RAM用户,并为这个用户创建AccessKeys
  41. String accessKeyId = "o************F";
  42. String accessKeySecret = "y*******************U";
  43. // AssumeRole API 请求参数: RoleArn, RoleSessionName, Policy, and DurationSeconds
  44. // RoleArn 需要在 RAM 控制台上获取
  45. String roleArn = "acs:ram::145883****900618:role/ossadminrole";
  46. // RoleSessionName 是临时Token的会话名称,自己指定用于标识你的用户,主要用于审计,或者用于区分Token颁发给谁
  47. // 但是注意RoleSessionName的长度和规则,不要有空格,只能有'-' '_' 字母和数字等字符
  48. // 具体规则请参考API文档中的格式要求
  49. String roleSessionName = "alice-001";
  50. // 如何定制你的policy?
  51. String policy = "{n" +
  52. " "Version": "1", n" +
  53. " "Statement": [n" +
  54. " {n" +
  55. " "Action": [n" +
  56. " "oss:GetBucket", n" +
  57. " "oss:GetObject" n" +
  58. " ], n" +
  59. " "Resource": [n" +
  60. " "acs:oss:*:*:*"n" +
  61. " ], n" +
  62. " "Effect": "Allow"n" +
  63. " }n" +
  64. " ]n" +
  65. "}";
  66. // 此处必须为 HTTPS
  67. ProtocolType protocolType = ProtocolType.HTTPS;
  68. try {
  69. final AssumeRoleResponse response = assumeRole(accessKeyId, accessKeySecret,
  70. roleArn, roleSessionName, policy, protocolType);
  71. System.out.println("Expiration: " + response.getCredentials().getExpiration());
  72. System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId());
  73. System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret());
  74. System.out.println("Security Token: " + response.getCredentials().getSecurityToken());
  75. } catch (ClientException e) {
  76. System.out.println("Failed to get a token.");
  77. System.out.println("Error code: " + e.getErrCode());
  78. System.out.println("Error message: " + e.getErrMsg());
  79. }
  80. }
  81. }

编译运行示例代码

编译

  1. mvn install

运行

  1. mvn -q exec:java -Dexec.mainClass=com.aliyun.sts.sample.StsServiceSample

最后更新:2016-11-23 16:04:01

  上一篇:go 示例__.Net SDK_RAM SDK使用手册_访问控制-阿里云
  下一篇:go 怎样授权一个子用户管理两台指定的ECS实例___云服务器(ECS)授权问题_授权常见问题_访问控制-阿里云