Ver Fonte

build 新增v1签名接口

mamingxu há 1 mês atrás
pai
commit
1eb3f1b4b2

+ 6 - 0
yyc-common-oss/pom.xml

@@ -20,5 +20,11 @@
 			<artifactId>aws-java-sdk-s3</artifactId>
 			<version>${aws.version}</version>
 		</dependency>
+		<!-- 阿里云 OSS 原生 SDK,用于生成 V1 长效签名 URL -->
+		<dependency>
+			<groupId>com.aliyun.oss</groupId>
+			<artifactId>aliyun-sdk-oss</artifactId>
+			<version>3.17.4</version>
+		</dependency>
 	</dependencies>
 </project>

+ 11 - 0
yyc-common-oss/src/main/java/net/yyc/common/oss/OssAutoConfiguration.java

@@ -18,6 +18,8 @@
 package net.yyc.common.oss;
 
 import net.yyc.common.oss.http.OssEndpoint;
+import net.yyc.common.oss.service.AliyunOssSigner;
+import net.yyc.common.oss.service.OssSigner;
 import net.yyc.common.oss.service.OssTemplate;
 import io.minio.MinioClient;
 import lombok.AllArgsConstructor;
@@ -60,4 +62,13 @@ public class OssAutoConfiguration {
 				.credentials(properties.getAccessKey(), properties.getSecretKey())
 				.build();
 	}
+
+	/**
+	 * 阿里云 OSS V1 签名 Bean(生成长有效期 URL)
+	 */
+	@Bean
+	@ConditionalOnMissingBean(OssSigner.class)
+	public OssSigner aliyunOssSigner(OssProperties2 properties2) {
+		return new AliyunOssSigner(properties2);
+	}
 }

+ 80 - 0
yyc-common-oss/src/main/java/net/yyc/common/oss/service/AliyunOssSigner.java

@@ -0,0 +1,80 @@
+/*
+ *    Copyright (c) 2018-2025, hnqz All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * Neither the name of the pig4cloud.com developer nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * Author: hnqz
+ */
+
+package net.yyc.common.oss.service;
+
+import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
+import com.aliyun.oss.model.GeneratePresignedUrlRequest;
+import net.yyc.common.oss.OssProperties2;
+import lombok.extern.slf4j.Slf4j;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import java.util.Date;
+
+/**
+ * 阿里云 OSS V1 签名实现(基于 oss2 配置,生成长有效期 URL)
+ *
+ * <p>V1 签名不强制 7 天上限,expiresSeconds 可传入任意正数(如一年)。
+ *
+ * @author hnqz
+ */
+@Slf4j
+public class AliyunOssSigner implements OssSigner {
+
+	private final OssProperties2 properties;
+
+	private OSS ossClient;
+
+	public AliyunOssSigner(OssProperties2 properties) {
+		this.properties = properties;
+	}
+
+	@PostConstruct
+	public void init() {
+		log.info("初始化阿里云 OSS V1 签名客户端, endpoint={}, bucket={}, objectKeyPre={}",
+				properties.getEndpoint(), properties.getBucketName(), properties.getObjectKeyPre());
+		this.ossClient = new OSSClientBuilder().build(
+				properties.getEndpoint(),
+				properties.getAccessKey(),
+				properties.getSecretKey());
+	}
+
+	@Override
+	public String sign(String objectKey, long expiresSeconds) {
+		String prefix = properties.getObjectKeyPre() != null ? properties.getObjectKeyPre() : "";
+		return sign(properties.getBucketName(), prefix + objectKey, expiresSeconds);
+	}
+
+	@Override
+	public String sign(String bucket, String objectKey, long expiresSeconds) {
+		Date expiration = new Date(System.currentTimeMillis() + expiresSeconds * 1000L);
+		GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket, objectKey);
+		request.setExpiration(expiration);
+		return ossClient.generatePresignedUrl(request).toString();
+	}
+
+	@PreDestroy
+	public void destroy() {
+		if (ossClient != null) {
+			ossClient.shutdown();
+			log.info("阿里云 OSS 客户端已关闭");
+		}
+	}
+
+}

+ 48 - 0
yyc-common-oss/src/main/java/net/yyc/common/oss/service/OssSigner.java

@@ -0,0 +1,48 @@
+/*
+ *    Copyright (c) 2018-2025, hnqz All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * Neither the name of the pig4cloud.com developer nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * Author: hnqz
+ */
+
+package net.yyc.common.oss.service;
+
+/**
+ * OSS 签名 URL 生成器(生成长有效期访问链接)
+ *
+ * <p>V1 签名无 7 天有效期上限,可传入任意秒数(常见:一年 = 365*24*3600)
+ *
+ * @author hnqz
+ */
+public interface OssSigner {
+
+	/**
+	 * 使用 oss2 默认 bucket + objectKeyPre 生成签名 URL
+	 *
+	 * @param objectKey      对象 key(不含 prefix,会自动拼接 oss2.object-key-pre)
+	 * @param expiresSeconds 过期秒数(任意正数)
+	 * @return 完整签名 URL
+	 */
+	String sign(String objectKey, long expiresSeconds);
+
+	/**
+	 * 指定 bucket 与 objectKey 生成签名 URL
+	 *
+	 * @param bucket         桶名
+	 * @param objectKey      对象 key(完整路径)
+	 * @param expiresSeconds 过期秒数(任意正数)
+	 * @return 完整签名 URL
+	 */
+	String sign(String bucket, String objectKey, long expiresSeconds);
+
+}