Skip to content

Commit 485b726

Browse files
committed
fix v2 generate signed url
1 parent 009bd46 commit 485b726

File tree

10 files changed

+172
-65
lines changed

10 files changed

+172
-65
lines changed

src/main/java/com/aliyun/oss/ClientConfiguration.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
import com.aliyun.oss.common.auth.RequestSigner;
3232
import com.aliyun.oss.common.comm.IdleConnectionReaper;
3333
import com.aliyun.oss.common.comm.Protocol;
34+
import com.aliyun.oss.common.comm.SignVersion;
3435
import com.aliyun.oss.common.utils.ResourceManager;
3536
import com.aliyun.oss.common.utils.VersionInfoUtils;
3637
import com.aliyun.oss.internal.OSSConstants;
37-
import com.aliyun.oss.internal.SignParameters;
3838

3939
/**
4040
* Client configurations for accessing to OSS services.
@@ -60,7 +60,7 @@ public class ClientConfiguration {
6060

6161
public static final String DEFAULT_CNAME_EXCLUDE_LIST = "aliyuncs.com,aliyun-inc.com,aliyun.com";
6262

63-
public static final String DEFAULT_SIGNATURE_VERSION = SignParameters.AUTH_V1;
63+
public static final SignVersion DEFAULT_SIGNATURE_VERSION = SignVersion.V1;
6464

6565
protected String userAgent = DEFAULT_USER_AGENT;
6666
protected int maxErrorRetry = DEFAULT_MAX_RETRIES;
@@ -97,7 +97,7 @@ public class ClientConfiguration {
9797

9898
protected List<RequestSigner> signerHandlers = new LinkedList<RequestSigner>();
9999

100-
protected String signatureVersion = DEFAULT_SIGNATURE_VERSION;
100+
protected SignVersion signatureVersion = DEFAULT_SIGNATURE_VERSION;
101101

102102
/**
103103
* Gets the user agent string.
@@ -662,7 +662,7 @@ public void setSignerHandlers(List<RequestSigner> signerHandlers) {
662662
*
663663
* @return signature version
664664
*/
665-
public String getSignatureVersion() {
665+
public SignVersion getSignatureVersion() {
666666
return signatureVersion;
667667
}
668668

@@ -671,7 +671,7 @@ public String getSignatureVersion() {
671671
*
672672
* @param signatureVersion
673673
*/
674-
public void setSignatureVersion(String signatureVersion) {
674+
public void setSignatureVersion(SignVersion signatureVersion) {
675675
this.signatureVersion = signatureVersion;
676676
}
677677
}

src/main/java/com/aliyun/oss/OSS.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import com.aliyun.oss.common.auth.Credentials;
3030
import com.aliyun.oss.common.comm.ResponseMessage;
31+
import com.aliyun.oss.common.comm.SignVersion;
3132
import com.aliyun.oss.model.*;
3233
import com.aliyun.oss.model.SetBucketCORSRequest.CORSRule;
3334

@@ -58,7 +59,7 @@ public interface OSS {
5859
* @param signatureVersion
5960
* the signature version to switch to。
6061
*/
61-
public void switchSignatureVersion(String signatureVersion);
62+
public void switchSignatureVersion(SignVersion signatureVersion);
6263

6364
/**
6465
* Shuts down the OSS instance (release all resources) The OSS instance is

src/main/java/com/aliyun/oss/OSSClient.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@
4747
import com.aliyun.oss.common.auth.CredentialsProvider;
4848
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
4949
import com.aliyun.oss.common.auth.ServiceSignature;
50-
import com.aliyun.oss.common.comm.DefaultServiceClient;
51-
import com.aliyun.oss.common.comm.ResponseMessage;
52-
import com.aliyun.oss.common.comm.ServiceClient;
53-
import com.aliyun.oss.common.comm.TimeoutServiceClient;
50+
import com.aliyun.oss.common.comm.*;
5451
import com.aliyun.oss.common.utils.BinaryUtil;
5552
import com.aliyun.oss.common.utils.DateUtil;
5653
import com.aliyun.oss.internal.*;
@@ -301,9 +298,9 @@ public void switchCredentials(Credentials creds) {
301298
}
302299

303300
@Override
304-
public void switchSignatureVersion(String signatureVersion) {
305-
if (!SignParameters.AUTH_V1.equals(signatureVersion) && !SignParameters.AUTH_V2.equals(signatureVersion)) {
306-
throw new IllegalArgumentException("unsupported signature version" + signatureVersion);
301+
public void switchSignatureVersion(SignVersion signatureVersion) {
302+
if (signatureVersion == null) {
303+
throw new IllegalArgumentException("signatureVersion should not be null.");
307304
}
308305

309306
this.getClientConfiguration().setSignatureVersion(signatureVersion);
@@ -700,7 +697,7 @@ public URL generatePresignedUrl(GeneratePresignedUrlRequest request) throws Clie
700697
}
701698
String url;
702699

703-
if (serviceClient.getClientConfiguration().getSignatureVersion() != null && serviceClient.getClientConfiguration().getSignatureVersion().equals(SignParameters.AUTH_V2)) {
700+
if (serviceClient.getClientConfiguration().getSignatureVersion() != null && serviceClient.getClientConfiguration().getSignatureVersion() == SignVersion.V2) {
704701
url = SignV2Utils.buildSignedURL(request, credsProvider.getCredentials(), serviceClient.getClientConfiguration(), endpoint);
705702
} else {
706703
url = SignUtils.buildSignedURL(request, credsProvider.getCredentials(), serviceClient.getClientConfiguration(), endpoint);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.aliyun.oss.common.comm;
2+
3+
public enum SignVersion {
4+
V1,
5+
V2
6+
}

src/main/java/com/aliyun/oss/internal/OSSOperation.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,7 @@
3333
import com.aliyun.oss.common.auth.Credentials;
3434
import com.aliyun.oss.common.auth.CredentialsProvider;
3535
import com.aliyun.oss.common.auth.RequestSigner;
36-
import com.aliyun.oss.common.comm.ExecutionContext;
37-
import com.aliyun.oss.common.comm.NoRetryStrategy;
38-
import com.aliyun.oss.common.comm.RequestChecksumHanlder;
39-
import com.aliyun.oss.common.comm.RequestHandler;
40-
import com.aliyun.oss.common.comm.RequestMessage;
41-
import com.aliyun.oss.common.comm.RequestProgressHanlder;
42-
import com.aliyun.oss.common.comm.ResponseChecksumHandler;
43-
import com.aliyun.oss.common.comm.ResponseHandler;
44-
import com.aliyun.oss.common.comm.ResponseMessage;
45-
import com.aliyun.oss.common.comm.ResponseProgressHandler;
46-
import com.aliyun.oss.common.comm.RetryStrategy;
47-
import com.aliyun.oss.common.comm.ServiceClient;
36+
import com.aliyun.oss.common.comm.*;
4837
import com.aliyun.oss.common.parser.ResponseParseException;
4938
import com.aliyun.oss.common.parser.ResponseParser;
5039
import com.aliyun.oss.common.utils.ExceptionFactory;
@@ -165,7 +154,7 @@ protected <T> T doOperation(RequestMessage request, ResponseParser<T> parser, St
165154
}
166155
}
167156

168-
private static RequestSigner createSigner(HttpMethod method, String bucketName, String key, Credentials creds, String signatureVersion) {
157+
private static RequestSigner createSigner(HttpMethod method, String bucketName, String key, Credentials creds, SignVersion signatureVersion) {
169158
String resourcePath = "/" + ((bucketName != null) ? bucketName + "/" : "") + ((key != null ? key : ""));
170159

171160
return new OSSRequestSigner(method.toString(), resourcePath, creds, signatureVersion);

src/main/java/com/aliyun/oss/internal/OSSRequestSigner.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.aliyun.oss.common.auth.Credentials;
2424
import com.aliyun.oss.common.auth.RequestSigner;
2525
import com.aliyun.oss.common.comm.RequestMessage;
26+
import com.aliyun.oss.common.comm.SignVersion;
2627

2728
public class OSSRequestSigner implements RequestSigner {
2829

@@ -33,9 +34,9 @@ public class OSSRequestSigner implements RequestSigner {
3334

3435
private Credentials creds;
3536

36-
private String signatureVersion;
37+
private SignVersion signatureVersion;
3738

38-
public OSSRequestSigner(String httpMethod, String resourcePath, Credentials creds, String signatureVersion) {
39+
public OSSRequestSigner(String httpMethod, String resourcePath, Credentials creds, SignVersion signatureVersion) {
3940
this.httpMethod = httpMethod;
4041
this.resourcePath = resourcePath;
4142
this.creds = creds;
@@ -50,7 +51,7 @@ public void sign(RequestMessage request) throws ClientException {
5051
if (accessKeyId.length() > 0 && secretAccessKey.length() > 0) {
5152
String signature;
5253

53-
if (SignParameters.AUTH_V2.equals(signatureVersion)) {
54+
if (signatureVersion == SignVersion.V2) {
5455
signature = SignV2Utils.buildSignature(secretAccessKey, httpMethod, resourcePath, request);
5556
request.addHeader(OSSHeaders.AUTHORIZATION, SignV2Utils.composeRequestAuthorization(accessKeyId,signature, request));
5657
} else {

src/main/java/com/aliyun/oss/internal/SignParameters.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414

1515
public class SignParameters {
1616

17-
public static final String AUTH_V1 = "auth-v1";
18-
19-
public static final String AUTH_V2 = "auth-v2";
20-
2117
public static final String AUTHORIZATION_PREFIX = "OSS ";
2218

2319
public static final String AUTHORIZATION_PREFIX_V2 = "OSS2 ";

src/main/java/com/aliyun/oss/internal/SignV2Utils.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,34 @@ public class SignV2Utils {
2727

2828
public static String composeRequestAuthorization(String accessKeyId, String signature, RequestMessage request) {
2929
StringBuilder sb = new StringBuilder();
30-
Set<String> ts = buildSortedAdditionalHeaderNames(request.getOriginalRequest().getHeaders().keySet(),
31-
request.getOriginalRequest().getAdditionalHeaderNames());
32-
3330
sb.append(AUTHORIZATION_PREFIX_V2 + AUTHORIZATION_ACCESS_KEY_ID).append(":").append(accessKeyId).append(", ");
3431

35-
if (ts != null && !ts.isEmpty()) {
36-
sb.append(AUTHORIZATION_ADDITIONAL_HEADERS).append(":");
37-
38-
String separator = "";
32+
String additionHeaderNameStr = buildSortedAdditionalHeaderNameStr(request.getOriginalRequest().getHeaders().keySet(),
33+
request.getOriginalRequest().getAdditionalHeaderNames());
3934

40-
for (String header : ts) {
41-
sb.append(separator);
42-
sb.append(header.toLowerCase());
43-
separator = ";";
44-
}
45-
sb.append(", ");
35+
if (!additionHeaderNameStr.isEmpty()) {
36+
sb.append(AUTHORIZATION_ADDITIONAL_HEADERS).append(":").append(additionHeaderNameStr).append(", ");
4637
}
4738
sb.append(AUTHORIZATION_SIGNATURE).append(":").append(signature);
4839

4940
return sb.toString();
5041
}
5142

52-
private static TreeSet<String> buildSortedAdditionalHeaderNames(Set<String> headerNames, Set<String> additionalHeaderNames) {
53-
TreeSet<String> ts = new TreeSet<String>();
43+
private static String buildSortedAdditionalHeaderNameStr(Set<String> headerNames, Set<String> additionalHeaderNames) {
44+
Set<String> ts = buildSortedAdditionalHeaderNames(headerNames, additionalHeaderNames);
45+
StringBuilder sb = new StringBuilder();
46+
String separator = "";
47+
48+
for (String header : ts) {
49+
sb.append(separator);
50+
sb.append(header);
51+
separator = ";";
52+
}
53+
return sb.toString();
54+
}
55+
56+
private static Set<String> buildSortedAdditionalHeaderNames(Set<String> headerNames, Set<String> additionalHeaderNames) {
57+
Set<String> ts = new TreeSet<String>();
5458

5559
if (headerNames != null && additionalHeaderNames != null) {
5660
for (String additionalHeaderName : additionalHeaderNames) {
@@ -197,11 +201,21 @@ public static String buildSignedURL(GeneratePresignedUrlRequest request, Credent
197201
requestMessage.addParameter(OSS_SIGNATURE_VERSION, SignParameters.AUTHORIZATION_V2);
198202
requestMessage.addParameter(OSS_EXPIRES, expires);
199203
requestMessage.addParameter(OSS_ACCESS_KEY_ID_PARAM, accessId);
204+
String additionalHeaderNameStr = buildSortedAdditionalHeaderNameStr(requestMessage.getHeaders().keySet(),
205+
request.getAdditionalHeaderNames());
206+
207+
if (!additionalHeaderNameStr.isEmpty()) {
208+
requestMessage.addParameter(OSS_ADDITIONAL_HEADERS, additionalHeaderNameStr);
209+
}
200210
Set<String> rawAdditionalHeaderNames = buildRawAdditionalHeaderNames(request.getHeaders().keySet(), request.getAdditionalHeaderNames());
201211
String canonicalString = buildCanonicalString(method.toString(), canonicalResource, requestMessage, rawAdditionalHeaderNames);
202212
String signature = new HmacSHA256Signature().computeSignature(accessKey, canonicalString);
203213

204214
Map<String, String> params = new LinkedHashMap<String, String>();
215+
216+
if (!additionalHeaderNameStr.isEmpty()) {
217+
params.put(OSS_ADDITIONAL_HEADERS, additionalHeaderNameStr);
218+
}
205219
params.put(OSS_SIGNATURE, signature);
206220
params.putAll(requestMessage.getParameters());
207221

src/test/java/com/aliyun/oss/integrationtests/GetObjectTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ public void testGetObjectWithSpecialChars() {
529529
@Test
530530
public void testGetObjectByUrlsignature() {
531531
final String key = "put-object-by-urlsignature";
532-
final String expirationString = "Sun, 12 Apr 2018 12:00:00 GMT";
532+
final String expirationString = "Sun, 12 Apr 2020 12:00:00 GMT";
533533
final long inputStreamLength = 128 * 1024; //128KB
534534
final long firstByte= inputStreamLength / 2;
535535
final long lastByte = inputStreamLength - 1;

0 commit comments

Comments
 (0)