前提条件
注:扮演角色在RAM中对应的操作是AssumeRole。
<dependencies>
<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>3.2.6</version>
</dependency>
</dependencies>
import java.text.MessageFormat;
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;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials;
public class TokenProxy {
private static final String REGION_ID = "cn-shanghai";
private static final long DEFAULT_TOKEN_TIMEOUT = 3600; // max timeout is 1 hour
// 这里指定了Security Token所具有的权限,限制为只能访问指定PhotoStore指定照片库的数据
// 同时禁止其访问SetQuota接口
static final String POLICY_PATTERN = "'{'\n"
+ " \"Version\": \"1\",\n"
+ " \"Statement\": [\n"
+ " '{'\n"
+ " \"Effect\": \"Allow\",\n"
+ " \"Action\": \"*\",\n"
+ " \"Resource\": \"acs:cloudphoto:*:*:photostores/{0}/libraries/{1}\"\n"
+ " '}',\n"
+ " '{'\n"
+ " \"Effect\": \"Deny\",\n"
+ " \"Action\": \"cloudphoto:SetQuota\",\n"
+ " \"Resource\": \"acs:cloudphoto:*:*:photostores/{0}/libraries/{1}\"\n"
+ " '}'\n"
+ " ]\n"
+ "'}'";
private String roleArn;
private DefaultAcsClient acsClient;
public TokenProxy(String roleArn, String accessKeyId, String accessKeySecret) {
IClientProfile profile = DefaultProfile.getProfile(REGION_ID, accessKeyId, accessKeySecret);
this.acsClient = new DefaultAcsClient(profile);
this.roleArn = roleArn;
}
public Credentials createCredentialForClient(String storeName, String libraryId) throws ClientException {
AssumeRoleRequest request = new AssumeRoleRequest();
request.setMethod(MethodType.POST);
request.setProtocol(ProtocolType.HTTPS);
request.setRoleArn(roleArn);
// 智能云相册利用Security Token中的RoleSessionName来传递需要访问的照片库的ID,
// 因此,将RoleSessionName设置为LibraryId
request.setRoleSessionName(libraryId);
request.setDurationSeconds(DEFAULT_TOKEN_TIMEOUT);
String policy = MessageFormat.format(POLICY_PATTERN, storeName, libraryId);
System.out.println(policy);
request.setPolicy(policy);
AssumeRoleResponse response = acsClient.getAcsResponse(request);
return response.getCredentials();
}
public static void main(String[] args) throws ClientException {
// 设置相关参数
// 1. accessKeyId 和 accessKeySecret是上面创建子用户时为其生成的。
String accessKeyId = "";
String accessKeySecret = "";
// 2. ARN是上面创建角色时记录下的ARN
String roleArn = "";
// 3. 需要访问的photostore的名称
String storeName = "your_store_name";
// 4. 需要访问的照片库的Id
String libraryId = "";
TokenProxy proxy = new TokenProxy(roleArn, accessKeyId, accessKeySecret);
Credentials credentials = proxy.createCredentialForClient(storeName, libraryId);
// 以下三个参数即是为客户端生成临时访问智能云相册的STS凭证。
System.out.println("tmp accessKeyId = " + credentials.getAccessKeyId());
System.out.println("tmp accessKeySecret = " + credentials.getAccessKeySecret());
System.out.println("sts token = " + credentials.getSecurityToken());
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。