阿里云STS临时令牌操作OSS云存储(java) |
您所在的位置:网站首页 › 安全令牌获取失败 › 阿里云STS临时令牌操作OSS云存储(java) |
使用STS临时访问凭证访问OSS 您可以通过STS服务给其他用户颁发一个临时访问凭证。该用户可使用临时访问凭证在规定时间内访问您的OSS资源。临时访问凭证无需透露您的长期密钥,使您的OSS资源访问更加安全。 适用场景假设您是一个移动App开发者,希望使用阿里云OSS服务来保存App的终端用户数据,并且要保证每个App用户之间的数据隔离。此时,您可以使用STS授权用户直接访问OSS。 使用STS授权用户直接访问OSS的流程如下: 警告 以下示例仅供参考。您需要根据实际需求配置更细粒度的授权策略,防止出现权限过大的风险。关于更细粒度的授权策略配置详情,请参见通过RAM或STS服务向其他用户授权。 { "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "oss:PutObject" ], "Resource": [ "acs:oss:*:*:examplebucket/exampledir", "acs:oss:*:*:examplebucket/exampledir/*" ] } ] } 策略配置完成后,单击下一步。在基本信息区域,填写策略名称为RamTestPolicy,然后单击确定。为RAM角色RamOssTest授予自定义权限策略。 在左侧导航栏,选择身份管理 > 角色。在角色页面,找到目标RAM角色RamOssTest。单击RAM角色RamOssTest右侧的添加权限。在添加权限页面下的自定义策略页签,选择已创建的自定义权限策略RamTestPolicy。单击确定。 二、引入依赖 com.aliyun.oss aliyun-sdk-oss 3.14.0 com.aliyun aliyun-java-sdk-sts 3.1.0 三、编写工具类application.yaml配置文件统一配置,方便后期维护。 crm: #使用STS临时访问凭证访问OSS https://help.aliyun.com/document_detail/100624.html?spm=a2c4g.11186623.2.19.515649e8zOIom1#concept-xzh-nzk-2gb stsAccessKeyId: xxxxx stsAccessKeySecret: xxxxx stsRoleArn: acs:ram::xxxxx:role/xxxxxroleSessionName参数用于区分不同的sts令牌,根据需求填写。 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; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.io.IOException; import java.text.ParseException; @Slf4j @Service public class StsSampleUtil { @Value("${crm.stsAccessKeyId}") private String stsAccessKeyId; @Value("${crm.stsAccessKeySecret}") private String stsAccessKeySecret; @Value("${crm.stsRoleArn}") private String stsRoleArn; public AssumeRoleResponse.Credentials getStsMessage() throws IOException, ParseException { String endpoint = "sts.aliyuncs.com"; String accessKeyId = stsAccessKeyId; String accessKeySecret = stsAccessKeySecret; String roleArn = stsRoleArn; String roleSessionName = "xxx"; String policy = null; try { //构造default profile(参数留空,无需添加Region ID) IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret); //用profile构造client DefaultAcsClient client = new DefaultAcsClient(profile); final AssumeRoleRequest request = new AssumeRoleRequest(); request.setSysEndpoint(endpoint); request.setSysMethod(MethodType.POST); request.setRoleArn(roleArn); request.setRoleSessionName(roleSessionName); request.setDurationSeconds(60 * 60L); // 设置凭证有效时间 request.setPolicy(policy); final AssumeRoleResponse response = client.getAcsResponse(request); AssumeRoleResponse.Credentials credentials = response.getCredentials(); log.info("Access Key Id: " + credentials.getAccessKeyId()); log.info("Access Key Secret: " + credentials.getAccessKeySecret()); log.info("Security Token: " + credentials.getSecurityToken()); log.info("RequestId: " + response.getRequestId()); return credentials; } catch (ClientException e) { log.info("Failed:" + e.getStackTrace()); log.info("Error code: " + e.getErrCode()); log.info("Error message: " + e.getErrMsg()); log.info("RequestId: " + e.getRequestId()); return null; } } } 四、编写接口并测试 @Resource private StsSampleUtil stsSampleUtil; @PostMapping("/getStsMessage") @ApiOperation("获取临时访问凭证") public CommonResult getStsMessage() throws IOException, ParseException { AssumeRoleResponse.Credentials stsMessage = stsSampleUtil.getStsMessage(); return success(stsMessage); } |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |