Browse Source

添加拦截器,对上传文件真实类型进行验证

Diors.Po 6 years ago
parent
commit
21d557ab26

+ 1 - 1
site/src/main/java/com/mooctest/crowd/site/command/CrowdTestTaskCommand.java

@@ -26,7 +26,7 @@ public class CrowdTestTaskCommand {
     private Integer type;
 
     @NotNull(message = "请指定任务发布类型")
-    private Integer resource;
+    private Long resource;
 
     private Map<String, String> location;
 

+ 25 - 0
site/src/main/java/com/mooctest/crowd/site/configuration/WebMvcConfiguration.java

@@ -0,0 +1,25 @@
+package com.mooctest.crowd.site.configuration;
+
+import com.mooctest.crowd.site.controller.interceptor.FileCheckInterceptor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+/**
+ * @author: Diors.Po
+ * @Email: 171256175@qq.com
+ * @date 2019-08-06 00:30
+ */
+@Configuration
+public class WebMvcConfiguration implements WebMvcConfigurer {
+
+    @Autowired
+    private FileCheckInterceptor fileCheckInterceptor;
+
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+        registry.addInterceptor(fileCheckInterceptor)
+                .addPathPatterns("/api/files/**");
+    }
+}

+ 40 - 0
site/src/main/java/com/mooctest/crowd/site/controller/interceptor/FileCheckInterceptor.java

@@ -0,0 +1,40 @@
+package com.mooctest.crowd.site.controller.interceptor;
+
+import com.mooctest.crowd.domain.exception.BaseException;
+import com.mooctest.crowd.site.util.FileUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * @author: Diors.Po
+ * @Email: 171256175@qq.com
+ * @date 2019-08-06 00:20
+ */
+@Slf4j
+@Component
+public class FileCheckInterceptor extends HandlerInterceptorAdapter {
+    @Override
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+        if (request!=null && request instanceof MultipartHttpServletRequest) {
+            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
+            Map<String, MultipartFile> files = multipartRequest.getFileMap();
+            Iterator<String> iterator = files.keySet().iterator();
+            while (iterator.hasNext()){
+                String formKey = (String) iterator.next();
+                MultipartFile file = files.get(formKey);
+                if (!FileUtil.checkFile(file.getInputStream()))
+                    throw new BaseException("文件不是可接受的格式,或上传了非法修改后缀名的文件: "+file.getOriginalFilename());
+            }
+        }
+        log.info("\n\n\nThis is a Interceptor Test!!!\n\n\n");
+        return true;
+    }
+}

+ 1 - 1
site/src/main/java/com/mooctest/crowd/site/data/vo/CrowdTaskVO.java

@@ -27,7 +27,7 @@ public class CrowdTaskVO {
     private String description;
     private Double quotePrice;
     private Double fixedPrice;
-    private int resource;
+    private Long resource;
     private Map<String, String> location;
     private Long institution;
     private int serviceType;