Files
gitea-test/src/main/java/cn/rensijin/cchs/config/web/WebConfig.java
2026-01-02 10:08:57 +08:00

59 lines
2.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cn.rensijin.cchs.config.web;
import cn.rensijin.cchs.config.date.JacksonObjectMapper;
import cn.rensijin.cchs.interceptor.LoginInterceptor;
import cn.rensijin.cchs.interceptor.RateLimitInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import javax.annotation.Resource;
import java.util.List;
/**
* web配置
*/
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Resource
private LoginInterceptor loginInterceptor;
@Resource
private RateLimitInterceptor rateLimitInterceptor;
//扩展SpringMVC框架的消息转化器例如实现返回日期时间的格式化
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//将java对象序列化为json数据,为消息转换器设置序列转换器
converter.setObjectMapper(new JacksonObjectMapper());
//将自己的消息转换器加入容器中
converters.add(0, converter);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(rateLimitInterceptor);
registry.addInterceptor(loginInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/", "/static/**", "/file/download/**")
.excludePathPatterns("/login/**", "/user/info", "/test/**","/user/register");
}
/**
* springboot 2.0配置WebMvcConfigurationSupport之后会导致默认配置被覆盖要访问静态资源需要重写addResourceHandlers方法
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/resources/")
.addResourceLocations("classpath:/static/");
super.addResourceHandlers(registry);
}
}