package com.aliyun.oss.demo; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.MethodType; import com.aliyuncs.http.ProtocolType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest; import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse; /** * StsServiceSample */ public class StsServiceSample { // 目前只有"cn-hangzhou"這個region可用, 不要使用填寫其餘region的值 public static final String REGION_CN_HANGZHOU = "cn-hangzhou"; // 當前 STS API 版本 public static final String STS_API_VERSION = "2015-04-01"; // STS服務必須爲 HTTPS public static final ProtocolType STS_PROTOCOL_TYPE = ProtocolType.HTTPS; static AssumeRoleResponse assumeRole(String accessKeyId,String accessKeySecret, String roleArn, String roleSessionName) throws ClientException { return assumeRole(accessKeyId, accessKeySecret, roleArn, roleSessionName, null, 3600, STS_PROTOCOL_TYPE); } static AssumeRoleResponse assumeRole(String accessKeyId,String accessKeySecret, String roleArn, String roleSessionName, String policy) throws ClientException { return assumeRole(accessKeyId, accessKeySecret, roleArn, roleSessionName, policy, 3600, STS_PROTOCOL_TYPE); } static AssumeRoleResponse assumeRole(String accessKeyId,String accessKeySecret, String roleArn, String roleSessionName, String policy, long expired, ProtocolType protocolType) throws ClientException { // 建立一個 Aliyun Acs Client, 用於發起 OpenAPI 請求 IClientProfile profile = DefaultProfile.getProfile(REGION_CN_HANGZHOU, accessKeyId, accessKeySecret); DefaultAcsClient client = new DefaultAcsClient(profile); // 建立一個 AssumeRoleRequest 並設置請求參數 final AssumeRoleRequest request = new AssumeRoleRequest(); request.setVersion(STS_API_VERSION); request.setMethod(MethodType.POST); request.setProtocol(protocolType); request.setRoleArn(roleArn); request.setRoleSessionName(roleSessionName); request.setPolicy(policy); request.setDurationSeconds(expired); // 發起請求,並獲得response return client.getAcsResponse(request); } public static void main(String[] args) { // 只有 子帳號才能調用 AssumeRole接口 // 阿里雲主帳號的AccessKeys不能用於發起AssumeRole請求 // 請首先在RAM控制檯建立子用戶,併爲這個用戶建立AccessKeys //String accessKeyId = "b6R5PxQSYFGhSQmR"; //String accessKeySecret = "QzubQVKg8Dzzc6f6LeCdDQTbDLPjF4"; String accessKeyId = "Qllj3v3UZ00jzNr5"; String accessKeySecret = "KSANDU3oU95oAL8K8lY3kjiOy1IM6C"; // AssumeRole API 請求參數: RoleArn, RoleSessionName, Policy, and // DurationSeconds // RoleArn能夠到控制檯上獲取,路徑是 訪問控制 > 角色管理 > 角色名稱 > 基本信息 > Arn String roleArn = "acs:ram::1085846427845250:role/accessossforinnerrole"; // RoleSessionName 是臨時Token的會話名稱,本身指定用於標識你的用戶,主要用於審計,或者用於區分Token頒發給誰 // 可是注意RoleSessionName的長度和規則,不要有空格,只能有'-' '_' 字母和數字等字符 // 具體規則請參考API文檔中的格式要求 String roleSessionName = "alice-001"; // 如何定製你的policy,若是policy爲null,則STS的權限與roleArn的policy的定義的權限 String policy = "{\n" + " \"Version\": \"1\", \n" + " \"Statement\": [\n" + " {\n" + " \"Action\": [\n" + " \"oss:GetBucket\", \n" + " \"oss:PutObject\", \n" + " \"oss:GetObject\", \n" + " \"oss:ListParts\" \n" + " ], \n" + " \"Resource\": [\n" + " \"acs:oss:*:*:*\"\n" + " ], \n" + " \"Effect\": \"Allow\"\n" + " }\n" + " ]\n" + "}"; // 過時時間設置默認是一小時,單位秒有效值是[900, 3600],即15分鐘到60分鐘。 long expired = 3600; try { AssumeRoleResponse response = assumeRole(accessKeyId,accessKeySecret, roleArn, roleSessionName, policy, expired, STS_PROTOCOL_TYPE); System.out.println("Expiration: " + response.getCredentials().getExpiration()); System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId()); System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret()); System.out.println("Security Token: " + response.getCredentials().getSecurityToken()); } catch (ClientException e) { System.out.println("Error code: " + e.getErrCode()); System.out.println("Error message: " + e.getErrMsg()); } } }