技术原理
通过STS为第三方访问颁发一个自定义时效和访问权限的访问凭证(STS临时Token)。第三方用户可以使用STS临时Token直接调用点播服务端API。
使用说明
说明
使用STS临时Token方式和使用上传地址和凭证方式各有优势。用户需要根据实际需求选择使用。两种方式详细的对比请参见
凭证方式与STS方式对比。更多关于上传地址和凭证的信息请参见
上传地址和凭证。
在上传场景下,使用STS临时Token和使用上传地址和凭证的操作区别是:
- 使用STS临时Token需要在构造上传请求时传入提前获取到的STS临时Token和临时AK对。
- 使用上传地址和凭证在构造上传请求时可直接传入阿里云账号或RAM用户的AK对。
不同的上传方式使用STS临时Token的描述如下:
上传方式 | 是否支持STS临时Token | 获取/使用方式 |
Java示例
Java获取STS临时Token示例代码
说明
此处以集成3.1.1版本的原版STS SDK为例进行获取STS临时Token示例说明,如需集成其余版本的STS SDK,详细信息,请参见STS SDK概览。
集成STS SDK。
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-sts</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.1</version>
</dependency>
调用AssumeRole获取STS临时Token。
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
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 TestStsService {
public static void main(String[] args) {
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
String roleArn = "acs:ram::174809843091****:role/vodrole";
String roleSessionName = "session-name";
String policy = "{\n" +
" \"Version\": \"1\",\n" +
" \"Statement\": [\n" +
" {\n" +
" \"Action\": \"vod:*\",\n" +
" \"Resource\": \"*\",\n" +
" \"Effect\": \"Allow\"\n" +
" }\n" +
" ]\n" +
"}";
try {
AssumeRoleResponse response = assumeRole(accessKeyId, accessKeySecret, roleArn, roleSessionName, policy);
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());
System.out.println("RequestId: " + response.getRequestId());
createUploadVideo(response.getCredentials().getAccessKeyId(), response.getCredentials().getAccessKeySecret(), 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());
}
}
static AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret, String roleArn, String roleSessionName, String policy) throws ClientException {
try {
IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
final AssumeRoleRequest request = new AssumeRoleRequest();
request.setSysEndpoint("sts.aliyuncs.com");
request.setSysMethod(MethodType.POST);
request.setRoleArn(roleArn);
request.setRoleSessionName(roleSessionName);
request.setPolicy(policy);
final AssumeRoleResponse response = client.getAcsResponse(request);
return response;
} catch (ClientException e) {
throw e;
}
}
参数 | 描述 |
RoleArn | 需要扮演的角色ID。为RAM用户创建好角色后,角色的ID可以从RAM控制台获取。获取路径:控制台的,单击角色名称后,在基本信息中可以复制ARN。 |
RoleSessionName | 角色会话名称。该参数为用户自定义参数。通常设置为调用该API的用户身份,例如:用户名。在操作审计日志中,即使是同一个RAM角色执行的操作,也可以根据不同的RoleSessionName来区分实际操作者,以实现用户级别的访问审计。长度为2~64个字符,可包含英文字母、数字、半角句号(.)、at(@)、短划线(-)和下划线(_)。 |
Policy | 在扮演角色的时候额外加上一个权限限制。 |
DurationSeconds | 临时凭证的有效期,单位为秒,最小为900,最大为3600。 |
accessKeyId和accessKeySecret | 需要扮演角色的RAM用户,及其AccessKey Secret。 |