阿里云使用java-SDK请求STS临时安全凭证token

首先,安装一下STS阿里云的jar包,官方文档详情在https://help.aliyun.com/document_detail/28788.html?spm=5176.doc28789.6.704.dasE9X,jar包地址https://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/cn/ram/0.0.71/assets/sts-sdk/aliyun_java_sdk_sts_20150825.zip?spm=5176.doc28789.2.2.dasE9X&file=aliyun_java_sdk_sts_20150825.zip

这里已经默认你有了阿里云java-SDK的核心包了

接下来,先编写一个类,用来返回一个响应,这个响应包含了你想要的token信息

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";
  //静态方法,方便调用
    public 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;
        }
    }
}

 好,准备好这个工具类之后,我们开始调用他,需要传递6个参数

在这里,需要创建一个阿里云的子账号,会有一组accessKeyId,accessKeySecret,这时候还需要

        /**
	 * 请求阿里云安全证书token
	 */
	public void token() {
        // 只有 RAM用户(子账号)才能调用 AssumeRole 接口

        // 阿里云主账号的AccessKeys不能用于发起AssumeRole请求

        // 请首先在RAM控制台创建一个RAM用户,并为这个用户创建AccessKeys
		String accessKeyId = "子账号的accessKeyId";
		String accessKeySecret = 子账号的accessKeySecret;
          //需要在RAM控制台获取,此时要给子账号权限,并建立一个角色,把这个角色赋给子账户,这个角色会有一串值,就是rolearn要填的
          //记得角色的权限,子账户的权限要分配好,不然会报错 String roleArn = "";
          //临时Token的会话名称,自己指定用于标识你的用户,主要用于审计,或者用于区分Token颁发给谁 String roleSessionName = "alice-001";
          //这个可以为空,不好写,格式要对,无要求建议为空 String policy = null; ProtocolType protocolType = ProtocolType.HTTPS; try { AssumeRoleResponse response = StsServiceSample.assumeRole(accessKeyId, accessKeySecret, roleArn, roleSessionName, policy, protocolType);
              String accesskeyid = response.getCredentials().getAccessKeyId();
              String accesskeysecret = response.getCredentials().getAccessKeySecret();
              //这个就是我们想要的安全token
              String securitytoken = response.getCredentials().getSecurityToken(); } catch (ClientException e) { e.printStackTrace(); } }

 使用过程中注意子账户的权限和角色的权限要分配好,不然会报错

 

posted @ 2017-05-08 11:06  孤单的小孩  阅读(8248)  评论(0编辑  收藏  举报