@@ -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
0 commit comments