Compare commits
2
Commits
a7e1c26853
...
042ef9a81e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
042ef9a81e | ||
|
|
fe2240e266 |
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>vip.jcfd</groupId>
|
<groupId>vip.jcfd</groupId>
|
||||||
<artifactId>zkh-framework</artifactId>
|
<artifactId>zkh-framework</artifactId>
|
||||||
<version>1.4</version>
|
<version>1.5</version>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<name>ZKH Framework</name>
|
<name>ZKH Framework</name>
|
||||||
<description>A Java framework for ZKH applications</description>
|
<description>A Java framework for ZKH applications</description>
|
||||||
@@ -38,6 +38,8 @@
|
|||||||
<module>zkh-common</module>
|
<module>zkh-common</module>
|
||||||
<module>zkh-web</module>
|
<module>zkh-web</module>
|
||||||
<module>zkh-data</module>
|
<module>zkh-data</module>
|
||||||
|
<module>zkh-log</module>
|
||||||
|
<module>zkh-file</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
@@ -71,6 +73,16 @@
|
|||||||
<artifactId>zkh-data</artifactId>
|
<artifactId>zkh-data</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>vip.jcfd</groupId>
|
||||||
|
<artifactId>zkh-log</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>vip.jcfd</groupId>
|
||||||
|
<artifactId>zkh-file</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springdoc</groupId>
|
<groupId>org.springdoc</groupId>
|
||||||
<artifactId>springdoc-openapi-common</artifactId>
|
<artifactId>springdoc-openapi-common</artifactId>
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>vip.jcfd</groupId>
|
<groupId>vip.jcfd</groupId>
|
||||||
<artifactId>zkh-framework</artifactId>
|
<artifactId>zkh-framework</artifactId>
|
||||||
<version>1.4</version>
|
<version>1.5</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>zkh-common</artifactId>
|
<artifactId>zkh-common</artifactId>
|
||||||
|
|||||||
+1
-7
@@ -6,19 +6,13 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>vip.jcfd</groupId>
|
<groupId>vip.jcfd</groupId>
|
||||||
<artifactId>zkh-framework</artifactId>
|
<artifactId>zkh-framework</artifactId>
|
||||||
<version>1.4</version>
|
<version>1.5</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>zkh-data</artifactId>
|
<artifactId>zkh-data</artifactId>
|
||||||
<name>ZKH Data</name>
|
<name>ZKH Data</name>
|
||||||
<description>Data layer components for ZKH framework</description>
|
<description>Data layer components for ZKH framework</description>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>21</maven.compiler.source>
|
|
||||||
<maven.compiler.target>21</maven.compiler.target>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>jakarta.persistence</groupId>
|
<groupId>jakarta.persistence</groupId>
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>vip.jcfd</groupId>
|
||||||
|
<artifactId>zkh-framework</artifactId>
|
||||||
|
<version>1.5</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>zkh-file</artifactId>
|
||||||
|
<name>ZKH file</name>
|
||||||
|
<description>
|
||||||
|
文件处理模块,提供文件上传、下载、处理等功能
|
||||||
|
</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>vip.jcfd</groupId>
|
||||||
|
<artifactId>zkh-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>2.20.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.minio</groupId>
|
||||||
|
<artifactId>minio</artifactId>
|
||||||
|
<version>8.5.17</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.hutool</groupId>
|
||||||
|
<artifactId>hutool-all</artifactId>
|
||||||
|
<version>5.8.41</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springdoc</groupId>
|
||||||
|
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||||
|
<version>2.8.14</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.14.1</version>
|
||||||
|
<configuration>
|
||||||
|
<annotationProcessorPaths>
|
||||||
|
<path>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
|
</path>
|
||||||
|
</annotationProcessorPaths>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
|
<version>3.2.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-sources</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jar-no-fork</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>3.4.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-javadocs</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jar</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package vip.jcfd.file.config;
|
||||||
|
|
||||||
|
import io.minio.MinioClient;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import vip.jcfd.file.config.props.MinioProps;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationPropertiesScan(basePackageClasses = {MinioProps.class})
|
||||||
|
public class MinioConfig {
|
||||||
|
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnProperty(name = "minio.endpoint")
|
||||||
|
public MinioClient minioClient(MinioProps minioProps) {
|
||||||
|
return MinioClient.builder()
|
||||||
|
.endpoint(minioProps.endpoint())
|
||||||
|
.credentials(minioProps.accessKey(), minioProps.secretKey())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package vip.jcfd.file.config.props;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
@ConfigurationProperties(prefix = "minio")
|
||||||
|
public record MinioProps(String endpoint, String accessKey, String secretKey, String bucket) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package vip.jcfd.file.controller;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.Parameter;
|
||||||
|
import io.swagger.v3.oas.annotations.media.Content;
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import vip.jcfd.common.core.R;
|
||||||
|
import vip.jcfd.file.dto.FileInfo;
|
||||||
|
import vip.jcfd.file.service.IFileDownloadService;
|
||||||
|
import vip.jcfd.file.service.IFileUploadService;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
@Tag(name = "文件管理", description = "文件上传和下载接口")
|
||||||
|
@RestController("_fileController")
|
||||||
|
@RequestMapping("/file")
|
||||||
|
public class FileController {
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(FileController.class);
|
||||||
|
private final IFileUploadService fileUploadService;
|
||||||
|
private final IFileDownloadService fileDownloadService;
|
||||||
|
|
||||||
|
public FileController(IFileUploadService fileUploadService, IFileDownloadService fileDownloadService) {
|
||||||
|
this.fileUploadService = fileUploadService;
|
||||||
|
this.fileDownloadService = fileDownloadService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "上传文件", description = "上传文件到服务器")
|
||||||
|
@ApiResponse(responseCode = "200", description = "上传成功",
|
||||||
|
content = @Content(mediaType = "application/json",
|
||||||
|
schema = @Schema(implementation = R.class)))
|
||||||
|
@PostMapping
|
||||||
|
public R<FileInfo> upload(
|
||||||
|
@Parameter(description = "要上传的文件", required = true)
|
||||||
|
MultipartFile file) {
|
||||||
|
try {
|
||||||
|
FileInfo upload = fileUploadService.upload(file);
|
||||||
|
return R.success(upload);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("上传失败", e);
|
||||||
|
return R.serverError("上传失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Operation(summary = "下载文件", description = "根据文件路径下载文件")
|
||||||
|
@ApiResponse(responseCode = "200", description = "下载成功")
|
||||||
|
@ApiResponse(responseCode = "404", description = "文件未找到")
|
||||||
|
@GetMapping("{path}")
|
||||||
|
public ResponseEntity<Resource> download(
|
||||||
|
@Parameter(description = "文件路径", required = true)
|
||||||
|
@PathVariable String path) {
|
||||||
|
Path normalize = Paths.get(path).normalize();
|
||||||
|
try {
|
||||||
|
Resource download = fileDownloadService.download(normalize.toString());
|
||||||
|
return ResponseEntity.ok()
|
||||||
|
.header("Content-Disposition", "attachment; filename=\"" + download.getFilename() + "\"")
|
||||||
|
.body(download);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("下载失败", e);
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package vip.jcfd.file.dto;
|
||||||
|
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
public record FileInfo(String filename, String contentType, Long size, String savePath) {
|
||||||
|
|
||||||
|
public static FileInfo fromMultiPartFile(MultipartFile file, String savePath) {
|
||||||
|
return new FileInfo(file.getOriginalFilename(), file.getContentType(), file.getSize(), savePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package vip.jcfd.file.service;
|
||||||
|
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface IFileDownloadService {
|
||||||
|
|
||||||
|
Resource download(String path) throws IOException;
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package vip.jcfd.file.service;
|
||||||
|
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import vip.jcfd.file.dto.FileInfo;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public interface IFileUploadService {
|
||||||
|
|
||||||
|
FileInfo upload(MultipartFile file) throws IOException;
|
||||||
|
}
|
||||||
@@ -0,0 +1,140 @@
|
|||||||
|
package vip.jcfd.file.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import io.minio.*;
|
||||||
|
import io.minio.errors.*;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
|
import org.springframework.core.io.AbstractResource;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import vip.jcfd.common.core.BizException;
|
||||||
|
import vip.jcfd.file.dto.FileInfo;
|
||||||
|
import vip.jcfd.file.service.IFileDownloadService;
|
||||||
|
import vip.jcfd.file.service.IFileUploadService;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.security.InvalidKeyException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@ConditionalOnBean(MinioClient.class)
|
||||||
|
public class MinioFileService implements IFileUploadService, IFileDownloadService {
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(MinioFileService.class);
|
||||||
|
private final MinioClient minioClient;
|
||||||
|
private final static String BUCKET_NAME = "upload"; // Changed from private to static
|
||||||
|
private final static String ORIGIN_FILENAME = "origin_filename";
|
||||||
|
|
||||||
|
public MinioFileService(MinioClient minioClient) {
|
||||||
|
this.minioClient = minioClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Resource download(String path) throws IOException {
|
||||||
|
Assert.isTrue(path.startsWith("/" + BUCKET_NAME), () -> new BizException("路径不合法"));
|
||||||
|
String[] split = path.split("/");
|
||||||
|
Assert.isTrue(split.length == 3, () -> new BizException("路径不合法"));
|
||||||
|
String objectName = split[2];
|
||||||
|
GetObjectArgs getObjectArgs = GetObjectArgs.builder().bucket(BUCKET_NAME).object(objectName).build();
|
||||||
|
try {
|
||||||
|
StatObjectResponse objectStat = minioClient.statObject(StatObjectArgs.builder().bucket(BUCKET_NAME).object(objectName).build());
|
||||||
|
Map<String, String> userMetadata = objectStat.userMetadata();
|
||||||
|
GetObjectResponse object = minioClient.getObject(getObjectArgs);
|
||||||
|
|
||||||
|
return new AbstractResource() {
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return userMetadata.getOrDefault(ORIGIN_FILENAME, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public InputStream getInputStream() throws IOException {
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getFilename() {
|
||||||
|
return userMetadata.getOrDefault(ORIGIN_FILENAME, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long contentLength() throws IOException {
|
||||||
|
return objectStat.size();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} catch (ErrorResponseException e) {
|
||||||
|
log.error("minio 服务异常", e);
|
||||||
|
} catch (InsufficientDataException e) {
|
||||||
|
log.error("数据不完整", e);
|
||||||
|
} catch (InternalException e) {
|
||||||
|
log.error("内部异常", e);
|
||||||
|
} catch (InvalidKeyException e) {
|
||||||
|
log.error("无效的密钥", e);
|
||||||
|
} catch (InvalidResponseException e) {
|
||||||
|
log.error("无效的响应", e);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
log.error("没有这样的算法", e);
|
||||||
|
} catch (ServerException e) {
|
||||||
|
log.error("服务器异常", e);
|
||||||
|
} catch (XmlParserException e) {
|
||||||
|
log.error("XML解析异常", e);
|
||||||
|
}
|
||||||
|
throw new BizException("下载失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileInfo upload(MultipartFile file) throws IOException {
|
||||||
|
try {
|
||||||
|
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(BUCKET_NAME).build())) {
|
||||||
|
minioClient.makeBucket(MakeBucketArgs.builder().bucket(BUCKET_NAME).build());
|
||||||
|
}
|
||||||
|
String string = UUID.randomUUID().toString();
|
||||||
|
String originalFilename = file.getOriginalFilename();
|
||||||
|
String filename = string;
|
||||||
|
if (StrUtil.isNotEmpty(originalFilename)) {
|
||||||
|
String suffix = FileUtil.getSuffix(originalFilename);
|
||||||
|
filename += "." + suffix;
|
||||||
|
}
|
||||||
|
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
|
||||||
|
.stream(file.getInputStream(), file.getSize(), -1)
|
||||||
|
.contentType(file.getContentType())
|
||||||
|
.object(filename)
|
||||||
|
.userMetadata(Map.of(
|
||||||
|
ORIGIN_FILENAME, Optional.ofNullable(originalFilename).orElse(filename)
|
||||||
|
))
|
||||||
|
.bucket(BUCKET_NAME)
|
||||||
|
.build();
|
||||||
|
ObjectWriteResponse response = minioClient.putObject(putObjectArgs);
|
||||||
|
String savePath = "/" + response.bucket() + "/" + response.object();
|
||||||
|
return FileInfo.fromMultiPartFile(file, savePath);
|
||||||
|
} catch (ErrorResponseException e) {
|
||||||
|
log.error("minio 服务异常", e);
|
||||||
|
} catch (InsufficientDataException e) {
|
||||||
|
log.error("数据不完整", e);
|
||||||
|
} catch (InternalException e) {
|
||||||
|
log.error("内部异常", e);
|
||||||
|
} catch (InvalidKeyException e) {
|
||||||
|
log.error("无效的密钥", e);
|
||||||
|
} catch (InvalidResponseException e) {
|
||||||
|
log.error("无效的响应", e);
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
log.error("没有这样的算法", e);
|
||||||
|
} catch (ServerException e) {
|
||||||
|
log.error("服务器异常", e);
|
||||||
|
} catch (XmlParserException e) {
|
||||||
|
log.error("XML解析异常", e);
|
||||||
|
}
|
||||||
|
throw new BizException("上传失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>vip.jcfd</groupId>
|
||||||
|
<artifactId>zkh-framework</artifactId>
|
||||||
|
<version>1.5</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>zkh-log</artifactId>
|
||||||
|
<name>ZKH log</name>
|
||||||
|
<description>Logging utilities for ZKH framework</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-security</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
|
<version>3.2.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-sources</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jar-no-fork</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>3.4.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-javadocs</id>
|
||||||
|
<goals>
|
||||||
|
<goal>jar</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package vip.jcfd.log;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
|
||||||
|
public class ConsoleLogService implements ILogService {
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(ConsoleLogService.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void log(String message, Authentication authentication) {
|
||||||
|
logger.debug("{} {}", authentication.getName(), message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package vip.jcfd.log;
|
||||||
|
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
|
||||||
|
public interface ILogService {
|
||||||
|
|
||||||
|
void log(String message, Authentication authentication);
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package vip.jcfd.log.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Documented
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
public @interface Log {
|
||||||
|
String value();
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package vip.jcfd.log.config;
|
||||||
|
|
||||||
|
import org.aspectj.lang.JoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.AfterReturning;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
import org.aspectj.lang.reflect.MethodSignature;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import vip.jcfd.log.ConsoleLogService;
|
||||||
|
import vip.jcfd.log.ILogService;
|
||||||
|
import vip.jcfd.log.annotation.Log;
|
||||||
|
|
||||||
|
@Configuration("_logConfiguration")
|
||||||
|
public class LogConfig {
|
||||||
|
|
||||||
|
@Bean("_defaultLogService")
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
@Order
|
||||||
|
public ILogService defaultLogService() {
|
||||||
|
return new ConsoleLogService();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@Component
|
||||||
|
public static class LogAspect {
|
||||||
|
private final ILogService logService;
|
||||||
|
|
||||||
|
public LogAspect(ILogService logService) {
|
||||||
|
this.logService = logService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Pointcut("@annotation(vip.jcfd.log.annotation.Log)")
|
||||||
|
public void logAspect() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterReturning(value = "logAspect()")
|
||||||
|
public void afterReturning(JoinPoint joinPoint) {
|
||||||
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||||
|
Log log = signature.getMethod().getAnnotation(Log.class);
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
logService.log(log.value(), authentication);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
-1
@@ -7,7 +7,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>vip.jcfd</groupId>
|
<groupId>vip.jcfd</groupId>
|
||||||
<artifactId>zkh-framework</artifactId>
|
<artifactId>zkh-framework</artifactId>
|
||||||
<version>1.4</version>
|
<version>1.5</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>zkh-web</artifactId>
|
<artifactId>zkh-web</artifactId>
|
||||||
@@ -19,6 +19,10 @@
|
|||||||
<groupId>vip.jcfd</groupId>
|
<groupId>vip.jcfd</groupId>
|
||||||
<artifactId>zkh-common</artifactId>
|
<artifactId>zkh-common</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>vip.jcfd</groupId>
|
||||||
|
<artifactId>zkh-log</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import vip.jcfd.common.core.R;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice("_globalExceptionHandler")
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|||||||
import vip.jcfd.web.config.props.SecurityProps;
|
import vip.jcfd.web.config.props.SecurityProps;
|
||||||
import vip.jcfd.web.redis.TokenRedisStorage;
|
import vip.jcfd.web.redis.TokenRedisStorage;
|
||||||
|
|
||||||
@Configuration
|
@Configuration("_redisConfiguration")
|
||||||
public class RedisConfig {
|
public class RedisConfig {
|
||||||
|
|
||||||
private final SecurityProps securityProps;
|
private final SecurityProps securityProps;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import org.springdoc.core.customizers.OpenApiCustomizer;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
@Configuration("_springDocConfig")
|
@Configuration("_springDocConfiguration")
|
||||||
public class SpringDocConfig {
|
public class SpringDocConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ import java.io.IOException;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@Configuration
|
@Configuration("_webSecurityConfiguration")
|
||||||
@EnableWebSecurity
|
@EnableWebSecurity
|
||||||
@ConfigurationPropertiesScan(basePackageClasses = {SecurityProps.class})
|
@ConfigurationPropertiesScan(basePackageClasses = {SecurityProps.class})
|
||||||
@EnableJpaAuditing
|
@EnableJpaAuditing
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
import vip.jcfd.common.core.R;
|
import vip.jcfd.common.core.R;
|
||||||
import vip.jcfd.common.dto.TokenRefreshRequest;
|
import vip.jcfd.common.dto.TokenRefreshRequest;
|
||||||
import vip.jcfd.common.dto.TokenRefreshResponse;
|
import vip.jcfd.common.dto.TokenRefreshResponse;
|
||||||
|
import vip.jcfd.log.annotation.Log;
|
||||||
import vip.jcfd.web.auth.RefreshTokenAuthenticationToken;
|
import vip.jcfd.web.auth.RefreshTokenAuthenticationToken;
|
||||||
import vip.jcfd.web.redis.TokenRedisStorage;
|
import vip.jcfd.web.redis.TokenRedisStorage;
|
||||||
|
|
||||||
@@ -37,6 +38,7 @@ public class AuthController {
|
|||||||
|
|
||||||
@PostMapping("/refresh-token")
|
@PostMapping("/refresh-token")
|
||||||
@Operation(summary = "刷新Token", description = "使用Refresh Token获取新的Access Token和Refresh Token")
|
@Operation(summary = "刷新Token", description = "使用Refresh Token获取新的Access Token和Refresh Token")
|
||||||
|
@Log("刷新了token")
|
||||||
public R<TokenRefreshResponse> refreshToken(
|
public R<TokenRefreshResponse> refreshToken(
|
||||||
@Valid @RequestBody TokenRefreshRequest request) {
|
@Valid @RequestBody TokenRefreshRequest request) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user