Merge pull request '提交测试2' (#3) from 提交1 into main

王兆华
pcygfhfke 4 months ago
commit 14971b1014

@ -1,62 +1,86 @@
// 包声明:定义配置类所在的包路径
package com.shanzhu.market.common.config; package com.shanzhu.market.common.config;
// 导入依赖的类库
import com.shanzhu.market.common.sercurity.interceptor.EmpLoginInterceptor; import com.shanzhu.market.common.sercurity.interceptor.EmpLoginInterceptor;
// 员工登录拦截器
import com.shanzhu.market.common.util.PathUtils; import com.shanzhu.market.common.util.PathUtils;
// 路径处理工具类
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
// Spring Bean定义注解
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
// Spring配置类注解
import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.CorsRegistry;
// 跨域配置注册器
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
// 拦截器注册器
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 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 @Configuration
//标识该类是一个 Spring 配置类Spring 容器会将其作为配置类加载。
// 标识为Spring配置类
public class BaseWebConfig implements WebMvcConfigurer { public class BaseWebConfig implements WebMvcConfigurer {
// 创建登录拦截器Bean当前被注释未启用
@Bean @Bean
public EmpLoginInterceptor empLoginInterceptor(){ public EmpLoginInterceptor empLoginInterceptor(){
return new EmpLoginInterceptor(); return new EmpLoginInterceptor();
} }
// 配置拦截器链(当前配置被注释)
@Override @Override
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
// registry.addInterceptor(empLoginInterceptor()) // 示例配置:添加登录拦截器并设置路径规则
// .addPathPatterns("/**") /*
// .excludePathPatterns("/","/login") registry.addInterceptor(empLoginInterceptor())
// .excludePathPatterns("/files/**") .addPathPatterns("/**") // 拦截所有路径
// .excludePathPatterns("/static/**"); .excludePathPatterns("/","/login") // 排除登录页和根路径
.excludePathPatterns("/files/**") // 排除文件访问路径
.excludePathPatterns("/static/**");// 排除静态资源路径
*/
} }
//跨域访问配置 // 跨域配置(核心配置)
@Bean @Bean
public WebMvcConfigurer corsConfigurer() { public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() { return new WebMvcConfigurer() {
@Override @Override
//重写父类提供的跨域请求处理的接口
public void addCorsMappings(CorsRegistry registry) { public void addCorsMappings(CorsRegistry registry) {
//添加映射路径
registry.addMapping("/**") registry.addMapping("/**")
//放行哪些原始域 // 匹配所有路径
.allowedOrigins("*") .allowedOrigins("*")
//是否发送Cookie信息 // 允许所有来源(生产环境建议指定具体域名)
.allowCredentials(true) .allowCredentials(true)
//放行哪些原始域(请求方式) // 允许携带凭证如cookies
.allowedMethods("GET", "POST", "PUT", "DELETE","OPTIONS") .allowedMethods("GET", "POST", "PUT", "DELETE","OPTIONS")
//放行哪些原始域(头部信息) // 允许的HTTP方法
.allowedHeaders("*") .allowedHeaders("*")
//暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息) // 允许所有请求头
.exposedHeaders("Header1", "Header2"); .exposedHeaders("Header1", "Header2");
// 暴露特定响应头给客户端
} }
}; };
} }
// 静态资源映射配置
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
// Windows系统下的本地文件存储路径注意Linux系统需调整路径格式
String winPath = PathUtils.getClassLoadRootPath() + "/src/main/resources/static/files/"; String winPath = PathUtils.getClassLoadRootPath() + "/src/main/resources/static/files/";
//第一个方法设置访问路径前缀,第二个方法设置资源路径 // 映射虚拟路径/files/**到物理路径
registry.addResourceHandler("/files/**"). registry.addResourceHandler("/files/**")
addResourceLocations("file:" + winPath); // 客户端访问路径模式
.addResourceLocations("file:" + winPath);
// 对应的物理文件路径
WebMvcConfigurer.super.addResourceHandlers(registry); WebMvcConfigurer.super.addResourceHandlers(registry);
// 保留父类默认配置
} }
} }

Loading…
Cancel
Save