生成V4版本的签名URL,集成OSS-SDK
package com.example.oss; import com.aliyun.oss.HttpMethod; import com.aliyun.oss.common.auth.Credentials; import com.aliyun.oss.common.auth.DefaultCredentials; import com.aliyun.oss.common.auth.RequestPresigner; import com.aliyun.oss.common.comm.RequestMessage; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.internal.signer.OSSSignerParams; import com.aliyun.oss.internal.signer.OSSV4Signer; import java.net.URISyntaxException; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.HashSet; import java.util.Set; public class OSSSignURLV4D2 { public static void main(String[] args) throws URISyntaxException { //设置时间 LocalDateTime currentTime = LocalDateTime.now(); // 设置未来时间为当前时间加上2小时 LocalDateTime futureTime = currentTime.plus(2, ChronoUnit.HOURS); // 截至到秒单位 LocalDateTime futureTimeTruncated = futureTime.truncatedTo(ChronoUnit.SECONDS); // 转换为Date类型 Date futureDate = Date.from(futureTimeTruncated.atZone(ZoneId.systemDefault()).toInstant()); String endpoint = "https://oss-cn-beijing.aliyuncs.com"; String accessKeyId = ""; String accessKeySecret = ""; String bucketName = "bucketname"; String objectName = "1122.png"; String resourcePath = "/bucketname/1122.png"; String url = "https://"+bucketName+".oss-cn-beijing.aliyuncs.com/"; Credentials credentials = new DefaultCredentials(accessKeyId,accessKeySecret); Set<String> additionalHeaderNames = new HashSet<>(); additionalHeaderNames.add("host"); OSSSignerParams ossSignerParams = new OSSSignerParams(resourcePath,credentials); ossSignerParams.setProduct("oss"); ossSignerParams.setRegion("cn-beijing"); ossSignerParams.setExpiration(futureDate); ossSignerParams.setAdditionalHeaderNames(additionalHeaderNames); RequestPresigner requestPresigner = OSSV4Signer.createRequestPresigner(SignVersion.V4,ossSignerParams); RequestMessage requestMessage = new RequestMessage(bucketName, objectName); requestMessage.setMethod(HttpMethod.GET); //URI enp =new URI(endpoint); //requestMessage.setEndpoint(enp); //requestMessage.setResourcePath(resourcePath); requestPresigner.presign(requestMessage); for (String value : requestMessage.getParameters().values()) { System.out.println(value); } String version = requestMessage.getParameters().get("x-oss-signature-version"); String credential = requestMessage.getParameters().get("x-oss-credential"); String date = requestMessage.getParameters().get("x-oss-date"); String expires = requestMessage.getParameters().get("x-oss-expires"); String additional = requestMessage.getParameters().get("x-oss-additional-headers"); String signature = requestMessage.getParameters().get("x-oss-signature"); StringBuilder ossurl = new StringBuilder(); if (additional== null) { ossurl.append(url+objectName).append("?"); ossurl.append("x-oss-signature-version=").append(version).append("&"); ossurl.append("x-oss-credential=").append(credential).append("&"); ossurl.append("x-oss-date=").append(date).append("&"); ossurl.append("x-oss-expires=").append(expires).append("&"); ossurl.append("x-oss-signature=").append(signature); }else { ossurl.append(url + objectName).append("?"); ossurl.append("x-oss-signature-version=").append(version).append("&"); ossurl.append("x-oss-credential=").append(credential).append("&"); ossurl.append("x-oss-date=").append(date).append("&"); ossurl.append("x-oss-expires=").append(expires).append("&"); ossurl.append("x-oss-additional-headers=").append(additional).append("&"); ossurl.append("x-oss-signature=").append(signature); } System.out.println("URL:"+ossurl.toString()); //https://examplebucket.oss-cn-hangzhou.aliyuncs.com/exampleobject? // x-oss-signature-version=OSS4-HMAC-SHA256& // x-oss-credential=<AccessKeyId>/20231203/cn-hangzhou/oss/aliyun_v4_request& // x-oss-date=20231203T121212Z& // x-oss-expires=86400& // x-oss-additional-headers=host& // x-oss-signature=<signature-to-be-calculated> } }
生成V4版本的Header签名,集成OSS-SDK
package com.example.oss; import com.aliyun.oss.HttpMethod; import com.aliyun.oss.common.auth.Credentials; import com.aliyun.oss.common.auth.DefaultCredentials; import com.aliyun.oss.common.auth.RequestSigner; import com.aliyun.oss.common.comm.RequestMessage; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.internal.OSSHeaders; import com.aliyun.oss.internal.signer.OSSSignerParams; import com.aliyun.oss.internal.signer.OSSV4Signer; import java.net.URI; import java.net.URISyntaxException; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.HashSet; import java.util.Set; public class OSSSignHeaderV4D3 { public static void main(String[] args) throws URISyntaxException { //设置时间 LocalDateTime currentTime = LocalDateTime.now(); // 设置未来时间为当前时间加上2小时 LocalDateTime futureTime = currentTime.plus(2, ChronoUnit.HOURS); // 截至到秒单位 LocalDateTime futureTimeTruncated = futureTime.truncatedTo(ChronoUnit.SECONDS); // 转换为Date类型 Date futureDate = Date.from(futureTimeTruncated.atZone(ZoneId.systemDefault()).toInstant()); String endpoint = "https://oss-cn-beijing.aliyuncs.com"; String accessKeyId = ""; String accessKeySecret = ""; String bucketName = "bucketname"; String objectName = "1122.png"; String resourcePath = "/bucketname/1122.png"; String url = "https://"+bucketName+".oss-cn-beijing.aliyuncs.com/"; Credentials credentials = new DefaultCredentials(accessKeyId,accessKeySecret); Set<String> additionalHeaderNames = new HashSet<>(); additionalHeaderNames.add("host"); OSSSignerParams ossSignerParams = new OSSSignerParams(resourcePath,credentials); ossSignerParams.setProduct("oss"); ossSignerParams.setRegion("cn-beijing"); ossSignerParams.setExpiration(futureDate); ossSignerParams.setAdditionalHeaderNames(additionalHeaderNames); RequestSigner requestSigner = OSSV4Signer.createRequestSigner(SignVersion.V4, ossSignerParams); RequestMessage requestMessage = new RequestMessage(bucketName, objectName); requestMessage.setMethod(HttpMethod.GET); URI enp =new URI(endpoint); //requestMessage.setEndpoint(enp); //requestMessage.setResourcePath(resourcePath); requestSigner.sign(requestMessage); System.out.println("Authorization:"+requestMessage.getHeaders().get(OSSHeaders.AUTHORIZATION)); System.out.println("x-oss-date:"+requestMessage.getHeaders().get(OSSHeaders.OSS_DATE)); System.out.println("URL:"+url+objectName); } }
阿里云官网的提供的V4签名示例
在Header中包含V4签名
https://help.aliyun.com/zh/oss/developer-reference/recommend-to-use-signature-version-4
在URL中包含V4签名
https://help.aliyun.com/zh/oss/developer-reference/add-signatures-to-urls
POST V4签名
https://help.aliyun.com/zh/oss/developer-reference/signature-version-4-recommend