AWS SDK for Java 1.x 403问题解决方法和原因

问题表现

使用AWS SDK for Java 1.x访问S3,已经确认文件存在,且具有权限,仍然出现403 Forbidden应答。

解决方法

升级到AWS SDK for Java 2.x。

问题原因

AWS签名机制严格依赖请求的精确路径格式,任何URI的差异(如 ///%2F )都会导致签名校验失败。AWS SDK for Java 1.x版本中,当资源路径 resourcePath 以斜杠开头时(如 /foo/... ),与 endpoint 拼接后会产生双斜杠 // 。SDK内部会将其转义为 /%2F ,导致实际请求路径与签名计算的路径不一致,触发 SignatureDoesNotMatch 错误。

关键代码分析

在AWS SDK for Java 1.x版本中,当调用 httpRequestFactory.create(request, options) 方法时,URL的生成过程涉及路径拼接逻辑与双斜杠转义机制,具体流程如下:

复制代码
@Override
   public HttpRequestBase create(final Request<?> request,
                                 final HttpClientSettings settings)
           throws
           FakeIOException {
       URI endpoint = request.getEndpoint();

       String uri;
       // skipAppendUriPath is set for APIs making requests with presigned urls. Otherwise
       // a slash will be appended at the end and the request will fail
       if (request.getOriginalRequest().getRequestClientOptions().isSkipAppendUriPath()) {
           uri = endpoint.toString();
       } else {
           /*
            * HttpClient cannot handle url in pattern of "http://host//path", so we
            * have to escape the double-slash between endpoint and resource-path
            * into "/%2F"
            */
           uri = SdkHttpUtils.appendUri(endpoint.toString(), request.getResourcePath(), true);
       }

       String encodedParams = SdkHttpUtils.encodeParameters(request);

       /*
        * For all non-POST requests, and any POST requests that already have a
        * payload, we put the encoded params directly in the URI, otherwise,
        * we'll put them in the POST request's payload.
        */
       boolean requestHasNoPayload = request.getContent() != null;
       boolean requestIsPost = request.getHttpMethod() == HttpMethodName.POST;
       boolean putParamsInUri = !requestIsPost || requestHasNoPayload;
       if (encodedParams != null && putParamsInUri) {
           uri += "?" + encodedParams;
       }

       final HttpRequestBase base = createApacheRequest(request, uri, encodedParams);
       addHeadersToRequest(base, request);
       addRequestConfig(base, request, settings);

       return base;
   }

假设原始请求参数为

复制代码
Endpoint: http://127.0.0.1/mybucket
ResourcePath: /foo/bar/... (以斜杠开头)

SdkHttpUtils.appendUri()endpointresourcePath 拼接为:

复制代码
http://127.0.0.1/mybucket//foo/bar/...

注意中间的 // 双斜杠。由于第三个参数 escapeDoubleSlash=true ,SDK会将双斜杠转义为 /%2F

复制代码
http://172.24.152.73:80/mybucket/%2Ffoo/bar/...

生成的URI变为转义后的路径,而计算签名时使用的路径是未经转义的原始路径 /mybucket//foo/bar/... ,导致签名不匹配。

相关推荐
翼龙云_cloud3 小时前
阿里云国际版代理商:OSS自定义域名绑定与HTTPS配置排障教程
数据库·阿里云·https·云计算
sbjdhjd4 小时前
CTF 技术复盘:从参数类型绕过到正则回溯 | Merry Christmas PHP CTF(gift.php & gift_plus.php)
安全·网络安全·云计算·php·ctf·红队·网络攻防
皮皮虾❀4 小时前
阿里云代理商采购+超额返点一体化实战:从“单次采购无优惠“到“年度合作解锁最高35%专属返点“的成本优化方案
阿里云·云计算
SaaS_Product1 天前
企业云盘收费标准主要取决于什么?企业云盘价格到底怎么算?
大数据·云计算·saas·onedrive
2401_861678621 天前
阿里云远程连接
阿里云·云计算
皮皮虾❀2 天前
阿里云上云迁移服务商选型+官方资质核验+ACE认证团队一体化实战:从“买到假代理”到“官网可查认证服务商承接迁移项目
阿里云·云计算
怪奇云呼军2 天前
知识库也会注入指令?闪电智能VoiceAgent 如何防住 Prompt Injection
人工智能·python·算法·云计算·音视频
jikemaoshiyanshi2 天前
企业搭建大模型网关与智能路由体系,如何实现精细化推理分发?——AWS 双层网关架构适配规模化业务落地
架构·云计算·aws
径硕科技JINGdigital2 天前
AI 训练时 GPU 利用率低,哪些云上高性能存储方案更适合优化训练成本?AWS 按 I/O 瓶颈分层选型
人工智能·云计算·aws
仓储管理员20252 天前
不同业务场景下,如何匹配适配的WMS系统?
大数据·运维·云计算·精选