|
|
|
@ -19,51 +19,93 @@ import com.google.code.kaptcha.util.Config;
|
|
|
|
|
import com.tamguo.interceptor.MemberInterceptor;
|
|
|
|
|
import com.tamguo.interceptor.MenuInterceptor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 这个类使用了Spring的 @Configuration 注解,表示它是一个配置类,用于配置Spring Web相关的一些功能和组件,
|
|
|
|
|
// Spring在启动时会自动扫描并加载该类中的配置信息,将其中定义的Bean注册到Spring容器中。
|
|
|
|
|
@Configuration
|
|
|
|
|
public class WebConfig extends WebMvcConfigurerAdapter {
|
|
|
|
|
|
|
|
|
|
@Value("${file.storage.path}")
|
|
|
|
|
private String fileStoragePath;
|
|
|
|
|
@Autowired
|
|
|
|
|
private MenuInterceptor menuInterceptor;
|
|
|
|
|
@Autowired
|
|
|
|
|
private MemberInterceptor memberInterceptor;
|
|
|
|
|
// 通过 @Value 注解从配置文件(例如 application.properties 或 application.yml)中读取名为 "file.storage.path" 的属性值,
|
|
|
|
|
// 并注入到这个变量中,该路径可能用于指定文件存储的位置,后续在资源处理器配置中会用到这个路径来映射资源访问路径。
|
|
|
|
|
@Value("${file.storage.path}")
|
|
|
|
|
private String fileStoragePath;
|
|
|
|
|
|
|
|
|
|
// 通过Spring的依赖注入机制自动注入 MenuInterceptor 实例,MenuInterceptor 应该是自定义的拦截器,
|
|
|
|
|
// 用于在请求处理过程中对菜单相关的逻辑进行拦截和处理,比如权限校验、菜单数据准备等操作(具体功能取决于拦截器的实现代码)。
|
|
|
|
|
@Autowired
|
|
|
|
|
private MenuInterceptor menuInterceptor;
|
|
|
|
|
|
|
|
|
|
// 同样通过依赖注入机制注入 MemberInterceptor 实例,MemberInterceptor 也是自定义拦截器,
|
|
|
|
|
// 从命名可以推测它可能用于对会员相关的请求路径进行拦截,执行如会员权限验证、会员数据处理等相关操作。
|
|
|
|
|
@Autowired
|
|
|
|
|
private MemberInterceptor memberInterceptor;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 拦截器
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
|
|
|
registry.addInterceptor(menuInterceptor).addPathPatterns("/**");
|
|
|
|
|
registry.addInterceptor(memberInterceptor).addPathPatterns("/member/**");
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 拦截器
|
|
|
|
|
* 重写了父类 WebMvcConfigurerAdapter 的 addInterceptors 方法,用于注册自定义的拦截器到Spring的拦截器注册表中,
|
|
|
|
|
* 这样在处理Web请求时,相应的拦截器就会按照配置的路径模式对请求进行拦截并执行拦截器内部的逻辑。
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
|
|
|
// 将 menuInterceptor 注册到拦截器注册表中,并配置其拦截的路径模式为 "/**",表示对所有的请求路径都会进行拦截,
|
|
|
|
|
// 具体在 MenuInterceptor 内部会根据业务逻辑判断是否需要进行处理以及如何处理这些请求。
|
|
|
|
|
registry.addInterceptor(menuInterceptor).addPathPatterns("/**");
|
|
|
|
|
// 将 memberInterceptor 注册到拦截器注册表中,配置其拦截的路径模式为 "/member/**",意味着对以 "/member/" 开头的请求路径进行拦截,
|
|
|
|
|
// 这通常用于对会员模块相关的请求进行特定的前置处理,比如验证会员登录状态、权限等情况。
|
|
|
|
|
registry.addInterceptor(memberInterceptor).addPathPatterns("/member/**");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
|
|
registry.addResourceHandler("/files/**").addResourceLocations("file:"+fileStoragePath);
|
|
|
|
|
super.addResourceHandlers(registry);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 资源处理器配置
|
|
|
|
|
* 重写了父类的 addResourceHandlers 方法,用于配置Spring Web中资源文件的处理方式,
|
|
|
|
|
* 这里主要是定义了对 "/files/**" 这样的请求路径对应的资源文件的查找位置,将其映射到本地文件系统中指定的 fileStoragePath 路径下,
|
|
|
|
|
* 这样当客户端发起对 "/files/" 开头路径的资源请求时,Spring就能从对应的本地文件存储位置去查找并返回相应的文件资源。
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
|
|
registry.addResourceHandler("/files/**").addResourceLocations("file:" + fileStoragePath);
|
|
|
|
|
super.addResourceHandlers(registry);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean(name="producer")
|
|
|
|
|
public DefaultKaptcha getKaptchaBean(){
|
|
|
|
|
DefaultKaptcha defaultKaptcha=new DefaultKaptcha();
|
|
|
|
|
Properties properties=new Properties();
|
|
|
|
|
// properties.setProperty("kaptcha.border.color", "105,179,90");
|
|
|
|
|
/**
|
|
|
|
|
* Kaptcha验证码配置
|
|
|
|
|
* 定义了一个名为 "producer" 的Bean,用于创建和配置 DefaultKaptcha 实例,DefaultKaptcha 是用于生成验证码的组件,
|
|
|
|
|
* 在这里通过设置 Properties 对象中的各种属性来定制验证码的生成规则,例如验证码图片的宽度、高度、字体、字符长度等相关参数,
|
|
|
|
|
* 最终返回配置好的 DefaultKaptcha 实例,以便在项目的其他地方(比如登录页面等需要验证码的地方)可以使用它来生成验证码图片及对应的验证文本。
|
|
|
|
|
*/
|
|
|
|
|
@Bean(name = "producer")
|
|
|
|
|
public DefaultKaptcha getKaptchaBean() {
|
|
|
|
|
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
|
|
|
|
Properties properties = new Properties();
|
|
|
|
|
// properties.setProperty("kaptcha.border.color", "105,179,90");
|
|
|
|
|
// 设置验证码图片是否显示边框,这里配置为 "no",即不显示边框。
|
|
|
|
|
properties.setProperty("kaptcha.border", "no");
|
|
|
|
|
// 设置验证码图片的宽度为 125 像素。
|
|
|
|
|
properties.setProperty("kaptcha.image.width", "125");
|
|
|
|
|
// 设置验证码图片的高度为 45 像素。
|
|
|
|
|
properties.setProperty("kaptcha.image.height", "45");
|
|
|
|
|
// 设置在Session中存储验证码文本的键名,这里设置为 "code",方便后续在验证时从Session中获取对应的验证码文本进行比对验证。
|
|
|
|
|
properties.setProperty("kaptcha.session.key", "code");
|
|
|
|
|
// 设置验证码文本的字符长度为 4 个字符,即生成的验证码文本将包含 4 个字符。
|
|
|
|
|
properties.setProperty("kaptcha.textproducer.char.length", "4");
|
|
|
|
|
// 设置验证码文本生成时使用的字体,可以指定多个字体,用逗号隔开,这里指定了宋体、楷体、微软雅黑等常见字体,
|
|
|
|
|
// 在生成验证码时会随机从这些字体中选择一种来绘制验证码文本。
|
|
|
|
|
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
|
|
|
|
|
Config config=new Config(properties);
|
|
|
|
|
Config config = new Config(properties);
|
|
|
|
|
defaultKaptcha.setConfig(config);
|
|
|
|
|
return defaultKaptcha;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
/**
|
|
|
|
|
* 嵌入式Servlet容器定制
|
|
|
|
|
* 定义了一个名为 "containerCustomizer" 的Bean,返回一个实现了 EmbeddedServletContainerCustomizer 接口的匿名内部类实例,
|
|
|
|
|
* 用于对嵌入式Servlet容器(比如在Spring Boot项目中默认使用的Tomcat容器等)进行定制化配置,
|
|
|
|
|
* 这里主要配置了错误页面的映射,当发生 HttpStatus.NOT_FOUND(404 状态码,表示资源未找到)错误时,将重定向到 "/404.html" 页面,
|
|
|
|
|
* 当发生 HttpStatus.INTERNAL_SERVER_ERROR(500 状态码,表示服务器内部错误)错误时,将重定向到 "/500.html" 页面,
|
|
|
|
|
* 通过这样的配置可以为用户提供更友好的错误提示页面,提升用户体验。
|
|
|
|
|
*/
|
|
|
|
|
@Bean
|
|
|
|
|
public EmbeddedServletContainerCustomizer containerCustomizer() {
|
|
|
|
|
return new EmbeddedServletContainerCustomizer(){
|
|
|
|
|
return new EmbeddedServletContainerCustomizer() {
|
|
|
|
|
@Override
|
|
|
|
|
public void customize(ConfigurableEmbeddedServletContainer container) {
|
|
|
|
|
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
|
|
|
|
@ -71,5 +113,4 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|