764
人物
使用入門__Java SDK_STS SDK使用手冊_訪問控製-阿裏雲
創建和管理角色
STS的AssumeRole接口的使用,需要在RAM中創建和管理角色,請參考RAM角色管理
使用maven創建項目
mvn archetype:generate -DgroupId=com.aliyun.sts.sample
-DartifactId=sts-sdk-sample
-Dpackage=com.aliyun.sts.sample
-Dversion=1.0-SNAPSHOT
修改生成的pom.xml, 添加aliyun-java-sdk的相關依賴。以2.1.6版本為例,在
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-sts</artifactId>
<version>2.1.6</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>2.1.7</version>
</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,內容如下:
package com.aliyun.sts.sample;
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;
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";
static AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret,
String roleArn, String roleSessionName, String policy,
ProtocolType protocolType) throws ClientException {
try {
// 創建一個 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);
// 發起請求,並得到response
final AssumeRoleResponse response = client.getAcsResponse(request);
return response;
} catch (ClientException e) {
throw e;
}
}
public static void main(String[] args) {
// 隻有 RAM用戶(子賬號)才能調用 AssumeRole 接口
// 阿裏雲主賬號的AccessKeys不能用於發起AssumeRole請求
// 請首先在RAM控製台創建一個RAM用戶,並為這個用戶創建AccessKeys
String accessKeyId = "o************F";
String accessKeySecret = "y*******************U";
// AssumeRole API 請求參數: RoleArn, RoleSessionName, Policy, and DurationSeconds
// RoleArn 需要在 RAM 控製台上獲取
String roleArn = "acs:ram::145883****900618:role/ossadminrole";
// RoleSessionName 是臨時Token的會話名稱,自己指定用於標識你的用戶,主要用於審計,或者用於區分Token頒發給誰
// 但是注意RoleSessionName的長度和規則,不要有空格,隻能有'-' '_' 字母和數字等字符
// 具體規則請參考API文檔中的格式要求
String roleSessionName = "alice-001";
// 如何定製你的policy?
String policy = "{n" +
" "Version": "1", n" +
" "Statement": [n" +
" {n" +
" "Action": [n" +
" "oss:GetBucket", n" +
" "oss:GetObject" n" +
" ], n" +
" "Resource": [n" +
" "acs:oss:*:*:*"n" +
" ], n" +
" "Effect": "Allow"n" +
" }n" +
" ]n" +
"}";
// 此處必須為 HTTPS
ProtocolType protocolType = ProtocolType.HTTPS;
try {
final AssumeRoleResponse response = assumeRole(accessKeyId, accessKeySecret,
roleArn, roleSessionName, policy, protocolType);
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("Failed to get a token.");
System.out.println("Error code: " + e.getErrCode());
System.out.println("Error message: " + e.getErrMsg());
}
}
}
編譯運行示例代碼
編譯
mvn install
運行
mvn -q exec:java -Dexec.mainClass=com.aliyun.sts.sample.StsServiceSample
最後更新:2016-11-23 16:04:01
上一篇:
示例__.Net SDK_RAM SDK使用手冊_訪問控製-阿裏雲
下一篇:
怎樣授權一個子用戶管理兩台指定的ECS實例___雲服務器(ECS)授權問題_授權常見問題_訪問控製-阿裏雲
下載 RDS 數據和日誌備份__備份與恢複_用戶指南_雲數據庫 RDS 版-阿裏雲
阿裏雲李津:三年內,阿裏雲成為世界雲計算兩極之一
ListAccessKeys__用戶管理接口_RAM API文檔_訪問控製-阿裏雲
高防CNAME接入流程__運維問題_產品常見問題_DDoS 高防IP-阿裏雲
批量添加域名解析記錄__批量管理接口_API文檔_雲解析-阿裏雲
彈性Web托管如何綁定域名__產品相關問題_使用問題_彈性 Web 托管-阿裏雲
過濾器(Filter)__Java-SDK (NEW)_SDK 參考_表格存儲-阿裏雲
SetLoadBalancerUDPListenerAttribute__Listener相關API_API 參考_負載均衡-阿裏雲
無敵!阿裏雲重磅新品:八大通信能力逆天
專業技術認證介紹___專業技術認證-阿裏雲
相關內容
常見錯誤說明__附錄_大數據計算服務-阿裏雲
發送短信接口__API使用手冊_短信服務-阿裏雲
接口文檔__Android_安全組件教程_移動安全-阿裏雲
運營商錯誤碼(聯通)__常見問題_短信服務-阿裏雲
設置短信模板__使用手冊_短信服務-阿裏雲
OSS 權限問題及排查__常見錯誤及排除_最佳實踐_對象存儲 OSS-阿裏雲
消息通知__操作指南_批量計算-阿裏雲
設備端快速接入(MQTT)__快速開始_阿裏雲物聯網套件-阿裏雲
查詢API調用流量數據__API管理相關接口_API_API 網關-阿裏雲
使用STS訪問__JavaScript-SDK_SDK 參考_對象存儲 OSS-阿裏雲