OSS的临时授权记录

编程入门 行业动态 更新时间:2024-10-13 14:21:41

<a href=https://www.elefans.com/category/jswz/34/1746150.html style=OSS的临时授权记录"/>

OSS的临时授权记录

主要是临时授权给移动端上传下载,临时授权提供读写功能

对OSS存储的了解先看这篇博客:

http://172.16.1.98:83/wordpress/2017/11/22/oss%E5%AD%98%E5%82%A8%E8%A7%84%E8%8C%83/

参考文档:.html

package com.lemo.face.util.oss;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;import lombok.extern.slf4j.Slf4j;/*** 临时访问凭证授权*/
@Slf4j
public class STSUtil {/*** 目前只有"cn-hangzhou"这个region可用, 不要使用填写其他region的值*/private static final String REGION_CN_HANGZHOU = "cn-hangzhou";/*** 当前 STS API 版本*/private static final String STS_API_VERSION = "2015-04-01";/*** 必须是https请求*/private static final ProtocolType PROTOCOL_TYPE = ProtocolType.HTTPS;private static final String ROLE_ARN = "创建ram账号的";/*** 可不用*/private static final String ROLE_SESSION_NAME = "随意";/*** @desc 创建上传临时账号* @author 杨小华* @create 2017/11/29 10:39**/public static Credentials createSTSForPutObject(String folder) {String policy = STSUtil.getPutObjectPolicy(folder);return createSTS(policy);}/*** 创建只读临时授权** @return com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials* @author 杨小华* @date 2018/1/31 15:24* @since 1.0.0*/public static Credentials createSTSForReadOnly() {String policy = STSUtil.getOSSReadOnlyAccessPolicy();return createSTS(policy);}/*** 授权策略** @param policy 授权策略* @return com.aliyuncs.sts.model.v20150401.AssumeRoleResponse.Credentials* @author 杨小华* @date 2018/1/31 15:22* @since 1.0.0*/private static Credentials createSTS(String policy) {try {final AssumeRoleResponse response =assumeRole(OSSClientUtil.accessKeyId, OSSClientUtil.accessKeySecret,ROLE_ARN, ROLE_SESSION_NAME,policy, PROTOCOL_TYPE);Credentials credentials = response.getCredentials();log.info("expiration:" + credentials.getExpiration() + "\t accessKeyIdSTS:" +credentials.getAccessKeyId() + "\t accessKeySecretSTS:" +credentials.getAccessKeySecret() + "\t " + "securityToken:" +credentials.getSecurityToken());return credentials;} catch (ClientException e) {log.error("Failed to get a token.");log.error("Error code: " + e.getErrCode());log.error("Error message: " + e.getErrMsg());}return null;}/*** @desc 自定义授权策略,对当前文件夹读写* @author 杨小华* @create 2017/11/29 10:39**/private static String getPutObjectPolicy(String folder) {return String.format("{\n" +"    \"Version\": \"1\", \n" +"    \"Statement\": [\n" +"        {\n" +"            \"Action\": [\n" +"                \"oss:PutObject\" \n" +"            ], \n" +"            \"Resource\": [\n" +"                \"acs:oss:*:*:%s/%s/*\"\n" +"            ], \n" +"            \"Effect\": \"Allow\"\n" +"        }\n" +"    ]\n" +"}", OSSClientUtil.BUCKET_NAME, folder);}/*** 只读访问该bucket对象存储服务(OSS)的权限,授权策略** @return java.lang.String* @author 杨小华* @date 2018/1/31 10:38* @since 1.0.0*/private static String getOSSReadOnlyAccessPolicy() {return String.format("{\n" +"  \"Statement\": [\n" +"    {\n" +"      \"Action\": [\n" +"        \"oss:Get*\",\n" +"        \"oss:List*\"\n" +"      ],\n" +"      \"Effect\": \"Allow\",\n" +"      \"Resource\": [\n" +"        \"acs:oss:*:*:%s/*\"\n" +"      ]\n" +"    }\n" +"  ],\n" +"  \"Version\": \"1\"\n" +"}", OSSClientUtil.BUCKET_NAME);}/*** @desc 权限验证, 扮演角色(AssumeRole)是实体用户获取角色身份的安全令牌的方法。* @author 杨小华* @create 2017/11/29 10:39**/private static AssumeRoleResponse assumeRole(String accessKeyId, String accessKeySecret,String roleArn,String roleSessionName, String policy,ProtocolType protocolType) throws ClientException {try {IClientProfile profile =DefaultProfile.getProfile(REGION_CN_HANGZHOU, accessKeyId, accessKeySecret);DefaultAcsClient client = new DefaultAcsClient(profile);final AssumeRoleRequest request = new AssumeRoleRequest();//持续秒数 3600秒,即1小时request.setDurationSeconds(3600L);request.setVersion(STS_API_VERSION);request.setMethod(MethodType.POST);request.setProtocol(protocolType);request.setRoleArn(roleArn);request.setRoleSessionName(roleSessionName);request.setPolicy(policy);return client.getAcsResponse(request);} catch (Exception e) {log.error(e.getMessage(), e);}return null;}}

 

更多推荐

OSS的临时授权记录

本文发布于:2024-03-05 23:48:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1713779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:OSS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!