阿里云OSS对象存储STS鉴权配置以及JAVA-SDK代码

STS鉴权流程

1.创建RAM子用户
    在控制台完成,只能由子用户申请角色扮演,主用户、临时用户不可以。子用户必须授予AliyunSTSAssumeRoleAccess系统权限后,才能申请角色扮演进行临时访问。
 
需要保存AK 信息,AK 信息只会在创建时显示,请您妥善保管。为安全起见, RAM 不提供 AK 信息查询

2. 创建OSS角色

 



3.新建OSS角色授权策略

     在控制台页面的访问控制里面进行自定义授权策略

     系统授权策略,阿里云提供的一组通用授权策略,主要针对不同产品的只读权限、所有权限。对于阿里云提供的这组授权策略,用户只能用于授权,不能编辑和修改,用户自定义的授权策略可以进行编辑修改。




4. OSS角色进行授权

将新建的角色授权策略授权给oss角色



5. 新建OSS用户授权策略

oss角色的roleArn


将Resource改为oss角色的roleArn



6.将OSS用户授权策略授权给RAM子用户


这样就完成了配置!


在STS鉴权的时候需要用到新创建子用户中的参数

    AccessKey Id ,   AccessKey Secret ,  RoleArn

 创建访问密钥(AK)

   为需要通过 API 进行调用的用户创建访问密钥(AK),操作步骤如下:

  1. 登录 RAM 控制台

  2. 用户管理页面,找到需要创建 AccessKey 的用户(可使用用户名进行模糊查询),然后单击其用户名或其操作列下的管理按钮。

  3. 用户详情页面,单击 创建 AccessKey

  4. 在弹出的对话框中,查看新建的 AK 信息,并可选择 保存 AK 信息

注意

  • AK 信息只会在创建时显示,请您妥善保管。为安全起见, RAM 不提供 AK 信息查询。
  • 如果 AK 泄露或丢失,则需要创建新的 AK。
  • 并且AK最多只能创建两个


RoleArn的查看    角色管理>角色详情



5.java-sdk代码示例

     maven依赖添加

<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-sts</artifactId>
  <version>2.1.6</version>
</dependency>
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>2.1.7</version>
</dependency>
    代码示例
public static void main(String[] args) {
    String endpoint = "sts.aliyuncs.com";
    String accessKeyId = "子用户的accessKeyId";
    String accessKeySecret = "子用户的accessKeySecret";
    String roleArn = "角色的RoleArn";
    //roleSessionName时临时Token的会话名称,自己指定用于标识你的用户,或者用于区分Token颁发给谁
    //要注意roleSessionName的长度和规则,不要有空格,只能有'-'和'_'字母和数字等字符
    String roleSessionName = "session-name";
    String policy = null;
    ProtocolType protocolType = ProtocolType.HTTPS;
    try {
	    DefaultProfile.addEndpoint("", "", "Sts", endpoint);
	    IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret);
	    DefaultAcsClient client = new DefaultAcsClient(profile);
	    final AssumeRoleRequest request = new AssumeRoleRequest();
        request.setMethod(MethodType.POST);
        request.setRoleArn(roleArn);
        request.setRoleSessionName(roleSessionName);
        request.setPolicy(policy);
        request.setProtocol(protocolType);
        final AssumeRoleResponse response = client.getAcsResponse(request);
        System.out.println("=============" + response.getCredentials().getAccessKeyId());
        System.out.println("=============" + response.getCredentials().getAccessKeySecret());
        System.out.println("=============" + response.getCredentials().getSecurityToken());
    } catch (ClientException e) {
    	e.printStackTrace();
	}
}

角色扮演常见问题

序号 问题 原因
1 ErrorCode: NoPermission 
ErrorMessage: Roles may not be assumed by root accounts.
使用主用户的密钥调用AssumeRole,请使用子用户的密钥。
2 ErrorCode: MissingSecurityToken 
ErrorMessage: SecurityToken is mandatory for this action.
使用临时用户的密钥调用AssumeRole,请使用子用户的密钥。
3 Error code: InvalidAccessKeyId.NotFound 
Error message: Specified access key is not found
AccessKeyId无效,请检查是否写错,特别是前后不能后空格。
4 Error code: InvalidAccessKeyId.Inactive 
Error message: Specified access key is disabled.
使用的子用户的密钥,已经被禁止,请启用密钥或更换密钥。 密钥是否被禁止,请控制台的“访问控制->用户管理->管理->用户详情->用户AccessKey”确认,并开启。
5 ErrorCode: InvalidParameter.PolicyGrammar 
ErrorMessage: The parameter Policy has not passed grammar check.
角色扮演时指定的授权策略无效。AssumeRole时可以指定授权(Policy),也可以不指定。如果指定授权策略,则临时用户的权限是指定的授权策略和角色权限的交集;如果不指定授权策略,临时用户的权限是角色的权限。
报该错误时,请检查指定的授权策略Policy。不推荐临时用户扮演角色时指定授权策略。如果的确需要使用授权策略,强烈建使用 RAM Policy Editor 生成授权策略。
6 ErrorCode: InvalidParameter.RoleSessionName
ErrorMessage: The parameter RoleSessionName is wrongly formed.
角色扮演时指定RoleSessionName无效。此参数用来区分不同的Token,以标明谁在使用此Token,便于审计; 格式:^[a-zA-Z0-9.@-_]+$,2-32个字符,了解更多请参看 扮演角色操作接口。如命名a,1,abc\*abc,忍者神龟等都是非法的
7 ErrorCode: InvalidParameter.DurationSeconds 
Error message: The Min/Max value of DurationSeconds is 15min/1hr.
角色扮演时指定的过期时间无效,即AssumeRoleRequest.setDurationSeconds参数无效。角色扮演时可以指定过期时间,单位为秒,有效时间是900 ~ 3600,如assumeRoleRequest.setDurationSeconds(60L * 20); 20分钟内有效。
8 ErrorCode: NoPermission
ErrorMessage: No permission perform sts:AssumeRole on this Role. Maybe you are not authorized to perform sts:AssumeRole or the specified role does not trust you
原因1:AssumeRole的子用户没有权限,请给子用户授予AliyunSTSAssumeRoleAccess系统授权策略。请在“访问控制->用户管理->授权->可选授权策略名称”中给子用户授权AliyunSTSAssumeRoleAccess。
原因2:申请角色扮演的子用户的云账号ID与角色的“受信云账号ID”不符,请角色创建者确认并修改。子用户的云账号ID,即创建子用户的主用户的ID;角色的云账号ID,即创建角色的主用户的云账号ID。请在如下位置确认修改 访问控制 -> 角色管理 -> 管理 -> 角色详细 -> 编辑基本信息 中确认修改。
原因3:角色的类型错误,如果角色的类型分为“用户角色”和“服务角色”,服务角色不能使用AssumeRole扮演临时用户。

常见问题及排查

OSS 403问题

OSS返回的常见错误,请参看 OSS的错误响应。 
其中403错误及原因如下表:

| 错误 | 错误码错误信息 | 错误原因 | 解决办法 | 
| -------------- | --------------------------- | ---------------------------------------- | ------------------ | 
| UserDisable.UserDisable | ErrorCode: UserDisable
ErrorMessage: UserDisable | 用户欠费或者因为安全原因被禁止访问 | OSS常见错误及排查 |
| RequestTimeTooSkewed | ErrorCode: RequestTimeTooSkewed
ErrorMessage: The difference between the request time and the current time is too large. | 发起请求的时间和服务器时间超出15分钟,一般是客户端系统时间错误 | OSS常见错误及排查 |
| InvalidAccessKeyId | ErrorCode: InvalidAccessKeyId
ErrorMessage: The OSS Access Key Id you provided does not exist in our records. | AccessKeyId无效/被禁止或者过期。 | OSS常见错误及排查 |
| SignatureDoesNotMatch | ErrorCode: SignatureDoesNotMatch
ErrorMessage: The request signature we calculated does not match the signature you provided. Check your key and signing method. | 客户端和服务计算的签名不符 | OSS常见错误及排查 |
| PostObject | ErrorCode: AccessDenied
ErrorMessage: Invalid according to Policy: Policy expired.
ErrorCode: AccessDenied 
ErrorMessage: Invalid according to Policy: Policy Condition failed: … | PostObject中Policy无效 | PostObject |
| Cors | ErrorCode: AccessForbidden
ErrorMessage: CORSResponse: This CORS request is not allowed. This is usually because the evalution of Origin, request method / Access-Control-Request-Method or Access-Control-Requet-Headers are not whitelisted by the resource's CORS spec. | CORS没有配置或配置不对 | OSS设置跨域访问 | 
| Refers | ErrorCode: AccessDenied
ErrorMessage: You are denied by bucket referer policy. | 请检查Bucket的Refers配置 | OSS防盗链 |
| AccessDenied | 见下面表权限问题错误及原因 | 无权限 | 下面详细讲述 | 
| 其它小类错误 | | | |

权限问题

  权限问题错误及原因见下表:

序号 错误 原因
1 ErrorCode: AccessDenied
ErrorMessage: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.
Bucket和Endpoint不符
2 ErrorCode: AccessDenied
ErrorMessage: You are forbidden to list buckets.
没有listBuckets权限
3 ErrorCode: AccessDenied
ErrorMessage: You do not have write acl permission on this object
无setObjectAcl权限
4 ErrorCode: AccessDenied
ErrorMessage: You do not have read acl permission on this object.
无getObjectAcl权限
5 ErrorCode: AccessDenied
ErrorMessage: The bucket you visit is not belong to you.
子用户没有Bucket管理的权限(如getBucketAcl CreateBucket、deleteBucket setBucketReferer、 getBucketReferer等)
6 ErrorCode: AccessDenied
ErrorMessage: You have no right to access this object because of bucket acl.
子用户/临时用户没有访问Object的权限(如putObject getObject、appendObject deleteObject、postObject)等
7 ErrorCode: AccessDenied
ErrorMessage: Access denied by authorizer's policy.
临时用户访问无权限,该临时用户角色扮演指定授权策略,该授权策略无权限
8 ErrorCode: AccessDenied
ErrorMessage: You have no right to access this object.
子用户/临时用户无当前操作权限(如initiateMultipartUpload等)

猜你喜欢

转载自blog.csdn.net/lz199719/article/details/80243742
今日推荐