From 597b5384614ba570ae24bc1c2ba3a811352750d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A7=91=E6=96=87?= <2731938298@qq.com> Date: Fri, 25 Apr 2025 16:18:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E6=B5=8B=E8=AF=952?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../market/common/config/BaseWebConfig.java | 80 ++++++++++++------- 1 file changed, 52 insertions(+), 28 deletions(-) diff --git a/superMarket-backend/src/main/java/com/shanzhu/market/common/config/BaseWebConfig.java b/superMarket-backend/src/main/java/com/shanzhu/market/common/config/BaseWebConfig.java index 6cc7315..14581f2 100644 --- a/superMarket-backend/src/main/java/com/shanzhu/market/common/config/BaseWebConfig.java +++ b/superMarket-backend/src/main/java/com/shanzhu/market/common/config/BaseWebConfig.java @@ -1,62 +1,86 @@ +// 包声明:定义配置类所在的包路径 package com.shanzhu.market.common.config; -import com.shanzhu.market.common.sercurity.interceptor.EmpLoginInterceptor; -import com.shanzhu.market.common.util.PathUtils; -import org.springframework.context.annotation.Bean; +// 导入依赖的类库 +import com.shanzhu.market.common.sercurity.interceptor.EmpLoginInterceptor; +// 员工登录拦截器 +import com.shanzhu.market.common.util.PathUtils; +// 路径处理工具类 +import org.springframework.context.annotation.Bean; +// Spring Bean定义注解 import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.CorsRegistry; +// Spring配置类注解 +import org.springframework.web.servlet.config.annotation.CorsRegistry; +// 跨域配置注册器 import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +// 拦截器注册器 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +// 静态资源处理器 +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +// Spring MVC配置接口 /** - * 解决:1.跨域问题 + * Web基础配置类(解决跨域等全局配置) */ @Configuration -public class BaseWebConfig implements WebMvcConfigurer{ +//标识该类是一个 Spring 配置类,Spring 容器会将其作为配置类加载。 +// 标识为Spring配置类 +public class BaseWebConfig implements WebMvcConfigurer { + + // 创建登录拦截器Bean(当前被注释未启用) @Bean public EmpLoginInterceptor empLoginInterceptor(){ return new EmpLoginInterceptor(); } + + // 配置拦截器链(当前配置被注释) @Override public void addInterceptors(InterceptorRegistry registry) { -// registry.addInterceptor(empLoginInterceptor()) -// .addPathPatterns("/**") -// .excludePathPatterns("/","/login") -// .excludePathPatterns("/files/**") -// .excludePathPatterns("/static/**"); + // 示例配置:添加登录拦截器并设置路径规则 + /* + registry.addInterceptor(empLoginInterceptor()) + .addPathPatterns("/**") // 拦截所有路径 + .excludePathPatterns("/","/login") // 排除登录页和根路径 + .excludePathPatterns("/files/**") // 排除文件访问路径 + .excludePathPatterns("/static/**");// 排除静态资源路径 + */ } - //跨域访问配置 + // 跨域配置(核心配置) @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override - //重写父类提供的跨域请求处理的接口 public void addCorsMappings(CorsRegistry registry) { - //添加映射路径 - registry.addMapping("/**") - //放行哪些原始域 - .allowedOrigins("*") - //是否发送Cookie信息 - .allowCredentials(true) - //放行哪些原始域(请求方式) - .allowedMethods("GET", "POST", "PUT", "DELETE","OPTIONS") - //放行哪些原始域(头部信息) - .allowedHeaders("*") - //暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息) + registry.addMapping("/**") + // 匹配所有路径 + .allowedOrigins("*") + // 允许所有来源(生产环境建议指定具体域名) + .allowCredentials(true) + // 允许携带凭证(如cookies) + .allowedMethods("GET", "POST", "PUT", "DELETE","OPTIONS") + // 允许的HTTP方法 + .allowedHeaders("*") + // 允许所有请求头 .exposedHeaders("Header1", "Header2"); + // 暴露特定响应头给客户端 } }; } + // 静态资源映射配置 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { + // Windows系统下的本地文件存储路径(注意:Linux系统需调整路径格式) String winPath = PathUtils.getClassLoadRootPath() + "/src/main/resources/static/files/"; - //第一个方法设置访问路径前缀,第二个方法设置资源路径 - registry.addResourceHandler("/files/**"). - addResourceLocations("file:" + winPath); + // 映射虚拟路径/files/**到物理路径 + registry.addResourceHandler("/files/**") + // 客户端访问路径模式 + .addResourceLocations("file:" + winPath); + // 对应的物理文件路径 + WebMvcConfigurer.super.addResourceHandlers(registry); + // 保留父类默认配置 } } -- 2.34.1