目前还在看
什么是STS
AssumeRole
RAM角色和STS Token常见问题
RAM Policy常见示例
使用TST临时访问凭证访问OSS
大佬的博文1
大佬的博文2
做一下记录,稍后来补.。。。。。
最终解决,生成token这里
最终解决,读取这里
明天再来补自己的笔记;
1:前端或者其他用户需要临时操作oss
2:私有资源指向在指定时间内允许访问
准备: 1,一个设置为私有的oss-bucket
2.一个专门用于STSToken相关操作的RAM账号
3.获取这个用户的AKID和AS
AS只在第一次打开的时候会显示,记得保存
4.创建权限策略
5.创建/编辑角色
创建:
创建角色-》信任实体:阿里云账号-》下一步-》确定
编辑:
角色是为了一会STS创建token 的时候指定角色,方便给token指定授权,而不是给当前创建的RAM用户使用,记得复制这里的RAN稍后靠这个指定角色
所以我们还需要给当前的RAM提供STS权限
6.为用户添加权限,这里主要是为了给用户添加STS权限,稍后当前ram才能签发token
至此,准备工作就完成了;
我们来代码部分;这里我只示范资源访问,资源请求可以看我上面提供的大佬的博文。他们做了文件上传;
首先需要依赖:
com.aliyun aliyun-java-sdk-core 3.2.2 com.aliyun aliyun-java-sdk-sts 3.0.0 com.aliyun.oss aliyun-sdk-oss 3.11.3
有奇怪的现象是依赖有时候在dependencyManageMent里不下载,拖到普通的dependencys里下了再拉回管理来吧
直接上代码,一行一注释,大家慢慢看
package com.doria.pzh.core;
import cn.hutool.core.bean.BeanUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
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.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.net.URL;
import java.util.Date;
import java.util.Random;
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestSrv {
@Autowired
RedisTemplate redisTemplate;
private final String stsAk="LTAaxxxxxxxxsrkDYw5";
private final String stsAs="QFeeQCI1xxxxxxxxxxxxxHxG";
@Test
public void test(){
STSResponse sts = sts();
OssDownVO ossDownVO = BeanUtil.copyProperties(sts, OssDownVO.class);
ossDownVO.setBucketName("oxxxx"); // ossbucket名称
ossDownVO.setOssEndpoint("oss-cn-beijing.aliyuncs.com");
// 注意根目录不要带[/],根目录下的就直接写文件名否则报错The specified key does not exist
ossDownVO.setResourceOssPath("新建文本文档.txt"); // 资源路径
URL down = down(ossDownVO);
// 打印获取到的url
System.out.println(down);
}
public STSResponse sts() {
AssumeRoleResponse.Credentials credentials = null;
String endpoint = "sts.cn-beijing.aliyuncs.com"; // STS服务的节点
String accessKeyId = stsAk; // 阿里的AK(不是那个ak)
String accessKeySecret = stsAs; // AS
// 角色名称,点开角色能看到,这里决定了我们授权的临时token具有什么角色,而角色又有具体的权限
String roleArn = "acs:ram::1133899369190070:role/oxls-crud";
String roleSessionName = "AliyunDMSRol--ePolicy"+new Random().nextInt(10000); // 看样子是会话名称的意思
// 这段可以根据情况自己决定设不设置,这个的作用是在设置的时候是否在原来角色的基础之上额外添加权限策略
try {
// 创建一个 Aliyun Acs Client, 用于发起 OpenAPI 请求
// 添加endpoint(直接使用STS endpoint,前两个参数留空,无需添加region ID)
DefaultProfile.addEndpoint("", "", "Sts", endpoint);
// 构造default profile(参数留空,无需添加region ID),客户端描述文件
IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret);
// 用profile构造client,至此就拥有了客户端
DefaultAcsClient client = new DefaultAcsClient(profile);
// 创建一个 AssumeRoleRequest 并设置请求参数,一个扮演某个角色的请求
final AssumeRoleRequest request = new AssumeRoleRequest();
request.setMethod(MethodType.POST); // 请求类型
request.setRoleArn(roleArn); // 设置角色ARN
request.setDurationSeconds(900L);// 设置过期时间,最小值15min也就是900秒,不设置默认3600
request.setRoleSessionName(roleSessionName); //设置会话名称,这里可以自定义,仅仅为了区别token分发给了谁
//request.setPolicy(policy); // 可选,如果设置了就需要添加
request.setProtocol(ProtocolType.HTTPS); // 必须使用HTTPS协议访问STS服务);
final AssumeRoleResponse response = client.getAcsResponse(request); // 使用客户端执行请求
credentials = response.getCredentials(); // 获取凭证
// 这里可以再封装返回,或者自行用这些参数请求服务获取正确的url以后返回,也可以获取url后直接重定向,总之就看你自己拉
System.out.println("Expiration: " + credentials.getExpiration()); // 获取过期时间
System.out.println("Access Key Id: " + credentials.getAccessKeyId());// 获取AK
System.out.println("Access Key Secret: " + credentials.getAccessKeySecret());// 获取AS
System.out.println("Security Token: " + credentials.getSecurityToken());// 获取安全token
System.out.println("RequestId: " + response.getRequestId());// 获取请求id
} catch (ClientException e) {
System.out.println("Failed:");
System.out.println("Error code: " + e.getErrCode());
System.out.println("Error message: " + e.getErrMsg());
System.out.println("RequestId: " + e.getRequestId());
}
if (ObjectUtils.isNotEmpty(credentials))
return STSResponse.builder().accessKeyId(credentials.getAccessKeyId())
.accessKeySecret(credentials.getAccessKeySecret())
.endpoint(endpoint)
.expiration(credentials.getExpiration())
.securityToken(credentials.getSecurityToken())
.build();
return new STSResponse();
}
public static URL down(OssDownVO ossDownVO) {
// String endpoint = stsResponse.getEndpoint(); // 指定节点名称
String accessKeyId = ossDownVO.getAccessKeyId(); // 指定AK(STS获取到的AK)
String accessKeySecret = ossDownVO.getAccessKeySecret();// 指定AS(STS获取到的AS)
String securityToken = ossDownVO.getSecurityToken();// 指定安全Token(STS获取到的安全token)
String bucketName = ossDownVO.getBucketName(); // bucket名称
String ossEndpoint = ossDownVO.getOssEndpoint();// OSS节点名称
String ossPath = ossDownVO.getResourceOssPath();// 资源路径省略节点和bucket部分
// 指定oss节点名称,AK,AS,ST
OSS ossClient = new OSSClientBuilder().build(ossEndpoint, accessKeyId, accessKeySecret, securityToken);
// 设置过期时间,上面设置的是我们获取到的token过期时间,这里设置的是资源过期时间
Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
// 计算获取资源路径
URL url = ossClient.generatePresignedUrl(bucketName, ossPath, expiration);
// 关流
ossClient.shutdown();
return url;
}
}
实体
// 下载文件请求VO
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OssDownVO {
private String expiration;
private String accessKeyId;
private String accessKeySecret;
private String securityToken;
private String requestId;
private String bucketName;
private String ossEndpoint;
private String resourceOssPath;
}
// STS 获取到的响应结果
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class STSResponse {
private String endpoint;
private String expiration;
private String accessKeyId;
private String accessKeySecret;
private String securityToken;
private String requestId;
}



