Compare commits

...

2 Commits

Author SHA1 Message Date
wangziqi
17e9a5b42b refactor: 移除后端跨域配置,由前端代理处理
- 移除 FileUploadController 的 @CrossOrigin 注解
- 恢复 WebMvcConfig 拦截器配置
2026-02-10 16:07:37 +08:00
wangziqi
b1a153298f feat: 前端配置代理解决上传跨域问题
- 配置 Vite 开发服务器代理 /api 和 /uploads 到后端
- API baseURL 改为相对路径 '/api'
- 修改上传 action 为相对路径 '/api/upload/image'
- 移除后端 FileUploadController 的 @CrossOrigin 注解
- 恢复 WebMvcConfig 的拦截器配置
2026-02-10 16:06:53 +08:00
9 changed files with 19 additions and 10 deletions

View File

@@ -20,10 +20,8 @@ public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/uploads/**")
.excludePathPatterns("/api/upload/**");
registry.addInterceptor(authInterceptor).addPathPatterns("/**")
.excludePathPatterns("/uploads/**");
}
@Override

View File

@@ -14,7 +14,6 @@ import java.util.UUID;
@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS})
public class FileUploadController {
@Value("${app.upload-path:./uploads}")

View File

@@ -1,7 +1,7 @@
import axios from 'axios'
const http = axios.create({
baseURL: 'http://localhost:8080',
baseURL: '/api',
timeout: 10000
})

View File

@@ -90,7 +90,7 @@ const banners = ref([])
const bannerModalVisible = ref(false)
const bannerForm = reactive({ id: null, imageUrl: '', linkUrl: '', sortNo: 1, enabled: true })
const uploadAction = 'http://localhost:8080/api/upload/image'
const uploadAction = '/api/upload/image'
const uploadHeaders = computed(() => ({
'X-Token': localStorage.getItem('token') || ''
}))

View File

@@ -126,7 +126,7 @@ const merchantOptions = ref([])
const productModalVisible = ref(false)
const productForm = reactive({ id: null, name: '', category: '', description: '', price: 0, stock: 0, imageUrl: '', merchantId: null, approved: false })
const uploadAction = 'http://localhost:8080/api/upload/image'
const uploadAction = '/api/upload/image'
const uploadHeaders = computed(() => ({
'X-Token': localStorage.getItem('token') || ''
}))

View File

@@ -110,7 +110,7 @@ const products = ref([])
const productModalVisible = ref(false)
const productForm = reactive({ id: null, name: '', category: '', description: '', price: 0, stock: 0, imageUrl: '', approved: false })
const uploadAction = 'http://localhost:8080/api/upload/image'
const uploadAction = '/api/upload/image'
const uploadHeaders = computed(() => ({
'X-Token': localStorage.getItem('token') || ''
}))

View File

@@ -4,6 +4,18 @@ import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
server: {
port: 5173
port: 5173,
proxy: {
'/api': {
target: 'http://localhost:8080',
changeOrigin: true,
secure: false
},
'/uploads': {
target: 'http://localhost:8080',
changeOrigin: true,
secure: false
}
}
}
})