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-阿里云