|
@@ -1,20 +1,23 @@
|
|
|
package com.onemap.file.config;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
+import org.springframework.web.servlet.resource.PathResourceResolver;
|
|
|
|
|
|
/**
|
|
|
* 通用映射配置
|
|
|
- *
|
|
|
+ *
|
|
|
* @author onemap
|
|
|
*/
|
|
|
@Configuration
|
|
|
-public class ResourcesConfig implements WebMvcConfigurer
|
|
|
-{
|
|
|
+public class ResourcesConfig implements WebMvcConfigurer {
|
|
|
/**
|
|
|
* 上传文件存储在本地的根路径
|
|
|
*/
|
|
@@ -28,20 +31,27 @@ public class ResourcesConfig implements WebMvcConfigurer
|
|
|
public String localFilePrefix;
|
|
|
|
|
|
@Override
|
|
|
- public void addResourceHandlers(ResourceHandlerRegistry registry)
|
|
|
- {
|
|
|
- /** 本地文件上传路径 */
|
|
|
- registry.addResourceHandler(localFilePrefix + "/**")
|
|
|
- .addResourceLocations("file:" + localFilePath + File.separator);
|
|
|
+ public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
+ /** 本地文件上传路径,暂时只允许png通过 */
|
|
|
+ registry.addResourceHandler(localFilePrefix + "/**").addResourceLocations("file:" + localFilePath + File.separator).resourceChain(true).addResolver(new PathResourceResolver() {
|
|
|
+ @Override
|
|
|
+ protected Resource getResource(String resourcePath, Resource location) throws IOException {
|
|
|
+ // 只允许 .png 文件被访问
|
|
|
+ if (resourcePath.endsWith(".png") || resourcePath.endsWith(".tif")) {
|
|
|
+ return super.getResource(resourcePath, location);
|
|
|
+ }
|
|
|
+ return null; // 其他文件类型返回 null,表示不可访问
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 开启跨域
|
|
|
*/
|
|
|
@Override
|
|
|
public void addCorsMappings(CorsRegistry registry) {
|
|
|
// 设置允许跨域的路由
|
|
|
- registry.addMapping(localFilePrefix + "/**")
|
|
|
+ registry.addMapping(localFilePrefix + "/**")
|
|
|
// 设置允许跨域请求的域名
|
|
|
.allowedOrigins("*")
|
|
|
// 设置允许的方法
|