mirror of
https://github.com/201206030/novel.git
synced 2025-04-27 07:30:50 +00:00
feat: 增加文件访问拦截器
This commit is contained in:
parent
6b366348c0
commit
61cc3b5f07
@ -1,7 +1,9 @@
|
|||||||
package io.github.xxyopen.novel.core.config;
|
package io.github.xxyopen.novel.core.config;
|
||||||
|
|
||||||
import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
|
import io.github.xxyopen.novel.core.constant.ApiRouterConsts;
|
||||||
|
import io.github.xxyopen.novel.core.constant.SystemConfigConsts;
|
||||||
import io.github.xxyopen.novel.core.interceptor.AuthInterceptor;
|
import io.github.xxyopen.novel.core.interceptor.AuthInterceptor;
|
||||||
|
import io.github.xxyopen.novel.core.interceptor.FileInterceptor;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
@ -19,11 +21,18 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class WebConfig implements WebMvcConfigurer {
|
public class WebConfig implements WebMvcConfigurer {
|
||||||
|
|
||||||
private final AuthInterceptor frontAuthInterceptor;
|
private final AuthInterceptor authInterceptor;
|
||||||
|
|
||||||
|
private final FileInterceptor fileInterceptor;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
public void addInterceptors(InterceptorRegistry registry) {
|
||||||
registry.addInterceptor(frontAuthInterceptor)
|
// 文件访问拦截
|
||||||
|
registry.addInterceptor(fileInterceptor)
|
||||||
|
.addPathPatterns(SystemConfigConsts.IMAGE_UPLOAD_DIRECTORY + "**");
|
||||||
|
|
||||||
|
// 权限认证拦截
|
||||||
|
registry.addInterceptor(authInterceptor)
|
||||||
// 拦截会员中心相关请求接口
|
// 拦截会员中心相关请求接口
|
||||||
.addPathPatterns(ApiRouterConsts.API_FRONT_USER_URL_PREFIX + "/**"
|
.addPathPatterns(ApiRouterConsts.API_FRONT_USER_URL_PREFIX + "/**"
|
||||||
// 拦截作家后台相关请求接口
|
// 拦截作家后台相关请求接口
|
||||||
@ -35,5 +44,6 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
, ApiRouterConsts.API_FRONT_USER_URL_PREFIX + "/login"
|
, ApiRouterConsts.API_FRONT_USER_URL_PREFIX + "/login"
|
||||||
, ApiRouterConsts.API_AUTHOR_URL_PREFIX + "/register"
|
, ApiRouterConsts.API_AUTHOR_URL_PREFIX + "/register"
|
||||||
,ApiRouterConsts.API_ADMIN_URL_PREFIX + "/login");
|
,ApiRouterConsts.API_ADMIN_URL_PREFIX + "/login");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,43 @@
|
|||||||
|
package io.github.xxyopen.novel.core.interceptor;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.servlet.HandlerInterceptor;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件 拦截器
|
||||||
|
*
|
||||||
|
* @author xiongxiaoyang
|
||||||
|
* @date 2022/5/22
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class FileInterceptor implements HandlerInterceptor {
|
||||||
|
|
||||||
|
@Value("${novel.file.upload.path}")
|
||||||
|
private String fileUploadPath;
|
||||||
|
|
||||||
|
@SuppressWarnings("NullableProblems")
|
||||||
|
@Override
|
||||||
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||||
|
// 获取请求的 URI
|
||||||
|
String requestUri = request.getRequestURI();
|
||||||
|
// 缓存10天
|
||||||
|
response.setDateHeader("expires", System.currentTimeMillis() + 60 * 60 * 24 * 10 * 1000);
|
||||||
|
try (OutputStream out = response.getOutputStream();InputStream input = new FileInputStream(fileUploadPath + requestUri)) {
|
||||||
|
byte[] b = new byte[4096];
|
||||||
|
for (int n; (n = input.read(b)) != -1; ) {
|
||||||
|
out.write(b, 0, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -65,7 +65,7 @@ novel:
|
|||||||
# 文件上传配置
|
# 文件上传配置
|
||||||
upload:
|
upload:
|
||||||
# 上传路径
|
# 上传路径
|
||||||
path: /var/upload
|
path: /Users/xiongxiaoyang/upload
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user