Skip to content

Commit b122a4b

Browse files
committed
add token for preview static sources
1 parent 3df3a01 commit b122a4b

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

src/main/java/com/jiaruiblog/config/CommonExceptionHandler.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import org.springframework.validation.BindException;
99
import org.springframework.web.HttpRequestMethodNotSupportedException;
1010
import org.springframework.web.bind.MethodArgumentNotValidException;
11+
import org.springframework.web.bind.MissingServletRequestParameterException;
1112
import org.springframework.web.bind.annotation.ExceptionHandler;
1213
import org.springframework.web.bind.annotation.ResponseBody;
1314
import org.springframework.web.bind.annotation.RestControllerAdvice;
15+
import org.springframework.web.method.HandlerMethod;
1416
import org.springframework.web.multipart.MaxUploadSizeExceededException;
1517

1618
import javax.servlet.http.HttpServletResponse;
@@ -108,6 +110,19 @@ public void dealAuthenticationException(HttpServletResponse response) {
108110
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
109111
}
110112

113+
/**
114+
* @Author luojiarui
115+
* @Description 管理员设置的禁止操作的错误
116+
* @Date 21:18 2022/12/9
117+
* @Param [e]
118+
**/
119+
@ExceptionHandler(MissingServletRequestParameterException.class)
120+
public BaseApiResult dealAuthenticationException(MissingServletRequestParameterException e, HandlerMethod handlerMethod) {
121+
String errorMessage = String.format("MissingServletRequestParameterException(遗漏Servlet请求参数异常):%s",
122+
e.getMessage());
123+
return BaseApiResult.error(MessageConstant.PARAMS_ERROR_CODE, errorMessage);
124+
}
125+
111126
}
112127

113128

src/main/java/com/jiaruiblog/controller/FileController.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cn.hutool.core.io.IoUtil;
44
import cn.hutool.core.util.StrUtil;
55
import cn.hutool.crypto.SecureUtil;
6+
import com.auth0.jwt.interfaces.Claim;
67
import com.google.common.collect.Lists;
78
import com.jiaruiblog.common.MessageConstant;
89
import com.jiaruiblog.entity.FileDocument;
@@ -16,6 +17,7 @@
1617
import com.jiaruiblog.service.TaskExecuteService;
1718
import com.jiaruiblog.util.BaseApiResult;
1819
import com.jiaruiblog.util.FileContentTypeUtils;
20+
import com.jiaruiblog.util.JwtUtil;
1921
import io.swagger.annotations.Api;
2022
import io.swagger.annotations.ApiOperation;
2123
import lombok.extern.slf4j.Slf4j;
@@ -79,14 +81,24 @@ public List<FileDocument> list(@ModelAttribute BasePageDTO basePageDTO) {
7981
*/
8082
@ApiOperation(value = "查询文档预览结果")
8183
@GetMapping("/view/{id}")
82-
public ResponseEntity<Object> serveFileOnline(@PathVariable String id) throws UnsupportedEncodingException {
84+
public ResponseEntity<Object> serveFileOnline(@PathVariable String id,
85+
@RequestParam("token") String token,
86+
HttpServletResponse response)
87+
throws UnsupportedEncodingException {
88+
Map<String, Claim> userData = JwtUtil.verifyToken(token);
89+
if (CollectionUtils.isEmpty(userData)) {
90+
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
91+
return null;
92+
}
8393
Optional<FileDocument> file = fileService.getById(id);
8494
if (file.isPresent()) {
8595
return ResponseEntity.ok()
8696
// 这里需要进行中文编码
87-
.header(HttpHeaders.CONTENT_DISPOSITION, "fileName=" + URLEncoder.encode(file.get().getName(), "utf-8"))
97+
.header(HttpHeaders.CONTENT_DISPOSITION,
98+
"fileName=" + URLEncoder.encode(file.get().getName(), "utf-8"))
8899
.header(HttpHeaders.CONTENT_TYPE, file.get().getContentType())
89-
.header(HttpHeaders.CONTENT_LENGTH, file.get().getSize() + "").header("Connection", "close")
100+
.header(HttpHeaders.CONTENT_LENGTH, file.get().getSize() + "")
101+
.header("Connection", "close")
90102
.header(HttpHeaders.CONTENT_LENGTH, file.get().getSize() + "")
91103
.body(file.get().getContent());
92104
} else {
@@ -98,7 +110,7 @@ public ResponseEntity<Object> serveFileOnline(@PathVariable String id) throws Un
98110
* 在线显示文件
99111
*
100112
* @param id 文件id
101-
* @return
113+
* @return ResponseEntity<Object> 返回实体
102114
*/
103115
@GetMapping("/view2/{id}")
104116
public ResponseEntity<Object> previewFileOnline(@PathVariable String id) throws UnsupportedEncodingException {
@@ -119,8 +131,8 @@ public ResponseEntity<Object> previewFileOnline(@PathVariable String id) throws
119131
/**
120132
* 下载附件
121133
*
122-
* @param id
123-
* @return
134+
* @param id 请求文件id
135+
* @return ResponseEntity<Object>
124136
* @throws UnsupportedEncodingException
125137
*/
126138
@GetMapping("/{id}")
@@ -288,7 +300,7 @@ public BaseApiResult uploadBatch(FileUploadDTO fileUploadDTO, HttpServletRequest
288300
tags = tags.subList(0, 10);
289301
}
290302
// 当只上传一个文档的时候,跳过错误肯定是False
291-
if (files.length <2) {
303+
if (files.length < 2) {
292304
skipError = Boolean.FALSE;
293305
}
294306
return fileService.uploadBatch(category, tags, description, skipError, files, userId, username);
@@ -325,11 +337,11 @@ public BaseApiResult uploadByUrl(@RequestBody UrlUploadDTO urlUploadDTO, HttpSer
325337
}
326338

327339
/**
340+
* @return java.lang.Boolean
328341
* @Author luojiarui
329342
* @Description 文件上传时的参数检查:长度要求;格式要求;敏感词要求
330343
* @Date 16:14 2023/4/22
331344
* @Param [tags, category, description, name]
332-
* @return java.lang.Boolean
333345
**/
334346
private static Boolean checkParam(List<String> tags, String category, String description, String name) {
335347

@@ -427,7 +439,14 @@ public ResponseModel deleteFileByGetMethod(@PathVariable String id) {
427439
**/
428440
@GetMapping(value = "/image/{thumbId}", produces = MediaType.IMAGE_PNG_VALUE)
429441
@ResponseBody
430-
public byte[] previewThumb(@PathVariable String thumbId) throws Exception {
442+
public byte[] previewThumb(@PathVariable String thumbId,
443+
@RequestParam("token") String token,
444+
HttpServletResponse response) throws Exception {
445+
Map<String, Claim> userData = JwtUtil.verifyToken(token);
446+
if (CollectionUtils.isEmpty(userData)) {
447+
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
448+
return new byte[]{};
449+
}
431450
InputStream inputStream = fileService.getFileThumb(thumbId);
432451
FileInputStream fileInputStream = (FileInputStream) (inputStream);
433452
if (inputStream == null) {
@@ -471,7 +490,14 @@ public ResponseEntity<Object> previewThumb1(@PathVariable String id) {
471490

472491
@GetMapping(value = "/image2/{thumbId}", produces = MediaType.IMAGE_PNG_VALUE)
473492
@ResponseBody
474-
public byte[] previewThumb2(@PathVariable String thumbId, HttpServletResponse response) {
493+
public byte[] previewThumb2(@PathVariable String thumbId,
494+
@RequestParam("token") String token,
495+
HttpServletResponse response) {
496+
Map<String, Claim> userData = JwtUtil.verifyToken(token);
497+
if (CollectionUtils.isEmpty(userData)) {
498+
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
499+
return new byte[]{};
500+
}
475501
// 设置响应头,缓存 1 小时
476502
response.setHeader("Cache-Control", "max-age=3600, public");
477503
return fileService.getFileBytes(thumbId);
@@ -583,7 +609,6 @@ public BaseApiResult temporaryFileDownloadLink() {
583609
// }
584610

585611

586-
587612
return BaseApiResult.success();
588613
}
589614
}

0 commit comments

Comments
 (0)