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> 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); } }