阿里雲使用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(); } }

 使用過程中注意子賬戶的權限和角色的權限要分配好,不然會報錯

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM