1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-01 22:53:29 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

创建预授权URL后端日志记录

This commit is contained in:
2022-04-08 18:44:32 +08:00
parent 2df583ab61
commit b4d7f858a3
11 changed files with 220 additions and 120 deletions

View File

@@ -8,7 +8,9 @@ import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.region.Region;
import plus.bookshelf.Common.Error.BusinessException;
import plus.bookshelf.Config.QCloudCosConfig;
import plus.bookshelf.Service.Service.CosPresignedUrlGenerateLogService;
import javax.annotation.PreDestroy;
import java.net.URL;
@@ -21,13 +23,17 @@ public class QCloudCosUtils {
// 配置信息
QCloudCosConfig qCloudCosConfig;
// 记录日志 Service
CosPresignedUrlGenerateLogService cosPresignedUrlGenerateLogService;
/**
* 构造函数
*
* @param qCloudCosConfig
*/
public QCloudCosUtils(QCloudCosConfig qCloudCosConfig) {
public QCloudCosUtils(QCloudCosConfig qCloudCosConfig, CosPresignedUrlGenerateLogService cosPresignedUrlGenerateLogService) {
this.qCloudCosConfig = qCloudCosConfig;
this.cosPresignedUrlGenerateLogService = cosPresignedUrlGenerateLogService;
}
private static COSClient _cosClient = null;
@@ -93,7 +99,7 @@ public class QCloudCosUtils {
* @param expireMinute 过期时间
* @return
*/
public String generatePresignedUrl(String token, HttpMethodName httpMethodName, String objectKey, Integer expireMinute) {
public String generatePresignedUrl(Integer userId, HttpMethodName httpMethodName, String objectKey, Integer expireMinute) throws BusinessException {
// 调用 COS 接口之前必须保证本进程存在一个 COSClient 实例,如果没有则创建
// 详细代码参见本页:简单操作 -> 创建 COSClient
// COSClient cosClient = createCOSClient();
@@ -110,10 +116,10 @@ public class QCloudCosUtils {
// 填写本次请求的参数,需与实际请求相同,能够防止用户篡改此签名的 HTTP 请求的参数
Map<String, String> params = new HashMap<>();
params.put("poweredBy", "bookshelf.plus");
params.put("userToken", token);
String downloadGUID = NanoIdUtils.randomNanoId();
params.put("downloadGUID", downloadGUID); // 当次生成下载链接的全局唯一Id
params.put("by", "书栖网 bookshelf.plus");
params.put("userId", String.valueOf(userId));
String urlGUID = NanoIdUtils.randomNanoId();
params.put("guid", urlGUID); // 当次生成下载链接的全局唯一Id
// 填写本次请求的头部,需与实际请求相同,能够防止用户篡改此签名的 HTTP 请求的头部
Map<String, String> headers = new HashMap<>();
@@ -125,6 +131,9 @@ public class QCloudCosUtils {
URL url = cosClient.generatePresignedUrl(bucketName, key, expirationDate, method, headers, params);
// System.out.println(url.toString());
// 记录用户操作日志
cosPresignedUrlGenerateLogService.log(userId, expireMinute, httpMethodName.name(), key, urlGUID);
return url.toString();
}

View File

@@ -13,6 +13,7 @@ import plus.bookshelf.Common.Response.CommonReturnType;
import plus.bookshelf.Config.QCloudCosConfig;
import plus.bookshelf.Service.Impl.UserServiceImpl;
import plus.bookshelf.Service.Model.UserModel;
import plus.bookshelf.Service.Service.CosPresignedUrlGenerateLogService;
@Api(tags = "文件管理")
@Controller("file")
@@ -25,6 +26,9 @@ public class FileController extends BaseController {
@Autowired
UserServiceImpl userService;
@Autowired
CosPresignedUrlGenerateLogService cosPresignedUrlGenerateLogService;
/**
* 创建文件操作预授权URL
*
@@ -61,7 +65,7 @@ public class FileController extends BaseController {
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "httpMethod 参数不合法");
}
QCloudCosUtils qCloudCosUtils = new QCloudCosUtils(qCloudCosConfig);
QCloudCosUtils qCloudCosUtils = new QCloudCosUtils(qCloudCosConfig, cosPresignedUrlGenerateLogService);
// 判断对象是否存在
Boolean isExist = qCloudCosUtils.doesObjectExist(fileName);
@@ -77,7 +81,7 @@ public class FileController extends BaseController {
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "httpMethod 参数暂不支持");
}
String url = qCloudCosUtils.generatePresignedUrl(token, httpMethodName, fileName, 30);
String url = qCloudCosUtils.generatePresignedUrl(userModel.getId(), httpMethodName, fileName, 30);
return CommonReturnType.create(url);
}

View File

@@ -2,11 +2,11 @@ package plus.bookshelf.Dao.DO;
import java.util.Date;
public class FileOperationDO {
public class CosPresignedUrlGenerateLogDO {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column file_operation_log.id
* This field corresponds to the database column cos_presigned_url_generate_log.id
*
* @mbg.generated
*/
@@ -15,7 +15,7 @@ public class FileOperationDO {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column file_operation_log.user_id
* This field corresponds to the database column cos_presigned_url_generate_log.user_id
*
* @mbg.generated
*/
@@ -24,7 +24,7 @@ public class FileOperationDO {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column file_operation_log.time
* This field corresponds to the database column cos_presigned_url_generate_log.time
*
* @mbg.generated
*/
@@ -33,16 +33,16 @@ public class FileOperationDO {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column file_operation_log.expire_minute
* This field corresponds to the database column cos_presigned_url_generate_log.expire_minute
*
* @mbg.generated
*/
private String expireMinute;
private Integer expireMinute;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column file_operation_log.method
* This field corresponds to the database column cos_presigned_url_generate_log.method
*
* @mbg.generated
*/
@@ -51,7 +51,7 @@ public class FileOperationDO {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column file_operation_log.file_path
* This field corresponds to the database column cos_presigned_url_generate_log.file_path
*
* @mbg.generated
*/
@@ -60,7 +60,7 @@ public class FileOperationDO {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column file_operation_log.url_guid
* This field corresponds to the database column cos_presigned_url_generate_log.url_guid
*
* @mbg.generated
*/
@@ -68,9 +68,9 @@ public class FileOperationDO {
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_operation_log.id
* This method returns the value of the database column cos_presigned_url_generate_log.id
*
* @return the value of file_operation_log.id
* @return the value of cos_presigned_url_generate_log.id
*
* @mbg.generated
*/
@@ -80,9 +80,9 @@ public class FileOperationDO {
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column file_operation_log.id
* This method sets the value of the database column cos_presigned_url_generate_log.id
*
* @param id the value for file_operation_log.id
* @param id the value for cos_presigned_url_generate_log.id
*
* @mbg.generated
*/
@@ -92,9 +92,9 @@ public class FileOperationDO {
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_operation_log.user_id
* This method returns the value of the database column cos_presigned_url_generate_log.user_id
*
* @return the value of file_operation_log.user_id
* @return the value of cos_presigned_url_generate_log.user_id
*
* @mbg.generated
*/
@@ -104,9 +104,9 @@ public class FileOperationDO {
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column file_operation_log.user_id
* This method sets the value of the database column cos_presigned_url_generate_log.user_id
*
* @param userId the value for file_operation_log.user_id
* @param userId the value for cos_presigned_url_generate_log.user_id
*
* @mbg.generated
*/
@@ -116,9 +116,9 @@ public class FileOperationDO {
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_operation_log.time
* This method returns the value of the database column cos_presigned_url_generate_log.time
*
* @return the value of file_operation_log.time
* @return the value of cos_presigned_url_generate_log.time
*
* @mbg.generated
*/
@@ -128,9 +128,9 @@ public class FileOperationDO {
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column file_operation_log.time
* This method sets the value of the database column cos_presigned_url_generate_log.time
*
* @param time the value for file_operation_log.time
* @param time the value for cos_presigned_url_generate_log.time
*
* @mbg.generated
*/
@@ -140,33 +140,33 @@ public class FileOperationDO {
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_operation_log.expire_minute
* This method returns the value of the database column cos_presigned_url_generate_log.expire_minute
*
* @return the value of file_operation_log.expire_minute
* @return the value of cos_presigned_url_generate_log.expire_minute
*
* @mbg.generated
*/
public String getExpireMinute() {
public Integer getExpireMinute() {
return expireMinute;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column file_operation_log.expire_minute
* This method sets the value of the database column cos_presigned_url_generate_log.expire_minute
*
* @param expireMinute the value for file_operation_log.expire_minute
* @param expireMinute the value for cos_presigned_url_generate_log.expire_minute
*
* @mbg.generated
*/
public void setExpireMinute(String expireMinute) {
this.expireMinute = expireMinute == null ? null : expireMinute.trim();
public void setExpireMinute(Integer expireMinute) {
this.expireMinute = expireMinute;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_operation_log.method
* This method returns the value of the database column cos_presigned_url_generate_log.method
*
* @return the value of file_operation_log.method
* @return the value of cos_presigned_url_generate_log.method
*
* @mbg.generated
*/
@@ -176,9 +176,9 @@ public class FileOperationDO {
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column file_operation_log.method
* This method sets the value of the database column cos_presigned_url_generate_log.method
*
* @param method the value for file_operation_log.method
* @param method the value for cos_presigned_url_generate_log.method
*
* @mbg.generated
*/
@@ -188,9 +188,9 @@ public class FileOperationDO {
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_operation_log.file_path
* This method returns the value of the database column cos_presigned_url_generate_log.file_path
*
* @return the value of file_operation_log.file_path
* @return the value of cos_presigned_url_generate_log.file_path
*
* @mbg.generated
*/
@@ -200,9 +200,9 @@ public class FileOperationDO {
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column file_operation_log.file_path
* This method sets the value of the database column cos_presigned_url_generate_log.file_path
*
* @param filePath the value for file_operation_log.file_path
* @param filePath the value for cos_presigned_url_generate_log.file_path
*
* @mbg.generated
*/
@@ -212,9 +212,9 @@ public class FileOperationDO {
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column file_operation_log.url_guid
* This method returns the value of the database column cos_presigned_url_generate_log.url_guid
*
* @return the value of file_operation_log.url_guid
* @return the value of cos_presigned_url_generate_log.url_guid
*
* @mbg.generated
*/
@@ -224,9 +224,9 @@ public class FileOperationDO {
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column file_operation_log.url_guid
* This method sets the value of the database column cos_presigned_url_generate_log.url_guid
*
* @param urlGuid the value for file_operation_log.url_guid
* @param urlGuid the value for cos_presigned_url_generate_log.url_guid
*
* @mbg.generated
*/

View File

@@ -0,0 +1,55 @@
package plus.bookshelf.Dao.Mapper;
import org.springframework.stereotype.Repository;
import plus.bookshelf.Dao.DO.CosPresignedUrlGenerateLogDO;
@Repository // 添加这个注解Autowired的时候idea就不会报错了
public interface CosPresignedUrlGenerateLogDOMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table cos_presigned_url_generate_log
*
* @mbg.generated
*/
int deleteByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table cos_presigned_url_generate_log
*
* @mbg.generated
*/
int insert(CosPresignedUrlGenerateLogDO record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table cos_presigned_url_generate_log
*
* @mbg.generated
*/
int insertSelective(CosPresignedUrlGenerateLogDO record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table cos_presigned_url_generate_log
*
* @mbg.generated
*/
CosPresignedUrlGenerateLogDO selectByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table cos_presigned_url_generate_log
*
* @mbg.generated
*/
int updateByPrimaryKeySelective(CosPresignedUrlGenerateLogDO record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table cos_presigned_url_generate_log
*
* @mbg.generated
*/
int updateByPrimaryKey(CosPresignedUrlGenerateLogDO record);
}

View File

@@ -1,53 +0,0 @@
package plus.bookshelf.Dao.Mapper;
import plus.bookshelf.Dao.DO.FileOperationDO;
public interface FileOperationDOMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table file_operation_log
*
* @mbg.generated
*/
int deleteByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table file_operation_log
*
* @mbg.generated
*/
int insert(FileOperationDO record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table file_operation_log
*
* @mbg.generated
*/
int insertSelective(FileOperationDO record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table file_operation_log
*
* @mbg.generated
*/
FileOperationDO selectByPrimaryKey(Integer id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table file_operation_log
*
* @mbg.generated
*/
int updateByPrimaryKeySelective(FileOperationDO record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table file_operation_log
*
* @mbg.generated
*/
int updateByPrimaryKey(FileOperationDO record);
}

View File

@@ -0,0 +1,44 @@
package plus.bookshelf.Service.Impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import plus.bookshelf.Common.Error.BusinessErrorCode;
import plus.bookshelf.Common.Error.BusinessException;
import plus.bookshelf.Dao.DO.CosPresignedUrlGenerateLogDO;
import plus.bookshelf.Dao.Mapper.CosPresignedUrlGenerateLogDOMapper;
import plus.bookshelf.Service.Service.CosPresignedUrlGenerateLogService;
import java.util.Date;
@Service
public class CosPresignedUrlGenerateLogServiceImpl implements CosPresignedUrlGenerateLogService {
@Autowired
CosPresignedUrlGenerateLogDOMapper cosPresignedUrlGenerateLogDOMapper;
/**
* 记录cos获取的预授权url日志
*
* @param userId 用户id
* @param expireTime 过期时间(分钟)
* @param method 请求方法
* @param filePath 文件路径
* @param urlGuid url唯一标识
* @return
*/
@Override
public void log(Integer userId, Integer expireTime, String method, String filePath, String urlGuid) throws BusinessException {
CosPresignedUrlGenerateLogDO cosPresignedUrlGenerateLogDO = new CosPresignedUrlGenerateLogDO();
cosPresignedUrlGenerateLogDO.setUserId(userId);
cosPresignedUrlGenerateLogDO.setTime(new Date());
cosPresignedUrlGenerateLogDO.setExpireMinute(expireTime);
cosPresignedUrlGenerateLogDO.setMethod(method);
cosPresignedUrlGenerateLogDO.setFilePath(filePath);
cosPresignedUrlGenerateLogDO.setUrlGuid(urlGuid);
Integer affectRows = cosPresignedUrlGenerateLogDOMapper.insertSelective(cosPresignedUrlGenerateLogDO);
if (affectRows == 0) {
throw new BusinessException(BusinessErrorCode.UNKNOWN_ERROR, "日志记录失败");
}
}
}

View File

@@ -0,0 +1,17 @@
package plus.bookshelf.Service.Service;
import plus.bookshelf.Common.Error.BusinessException;
public interface CosPresignedUrlGenerateLogService {
/**
* 记录cos获取的预授权url日志
*
* @param userId 用户id
* @param expireTime 过期时间(分钟)
* @param method 请求方法
* @param filePath 文件路径
* @param urlGuid url唯一标识
* @return
*/
void log(Integer userId, Integer expireTime, String method, String filePath, String urlGuid) throws BusinessException;
}