2024-03-18 18:45:35 +08:00
|
|
|
|
package com.sky.common.config;
|
|
|
|
|
|
2024-03-19 09:45:43 +08:00
|
|
|
|
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
|
|
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
2024-03-18 21:00:53 +08:00
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2024-03-19 09:45:43 +08:00
|
|
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
|
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
2024-03-18 18:45:35 +08:00
|
|
|
|
import com.sky.common.interceptor.JwtTokenUserInterceptor;
|
|
|
|
|
import com.sky.common.interceptor.RedisTokenAdminInterceptor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
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.WebMvcConfigurationSupport;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
|
|
|
|
@Autowired
|
|
|
|
|
RedisTokenAdminInterceptor adminInterceptor;
|
|
|
|
|
@Autowired
|
|
|
|
|
JwtTokenUserInterceptor userInterceptor;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 注册自定义拦截器
|
|
|
|
|
*
|
|
|
|
|
* @param registry InterceptorRegistry
|
|
|
|
|
*/
|
|
|
|
|
protected void addInterceptors(InterceptorRegistry registry) {
|
|
|
|
|
log.info("开始注册自定义拦截器...");
|
|
|
|
|
registry.addInterceptor(adminInterceptor).addPathPatterns("/admin/**")
|
|
|
|
|
.excludePathPatterns("/admin/employee/login");
|
|
|
|
|
registry.addInterceptor(userInterceptor).addPathPatterns("/user/**")
|
|
|
|
|
.excludePathPatterns("/user/user/login")
|
|
|
|
|
.excludePathPatterns("/user/shop/status");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 扩展Spring MVC框架的消息转化器
|
|
|
|
|
*
|
|
|
|
|
* @param converters 转换器
|
|
|
|
|
*/
|
|
|
|
|
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
|
|
|
log.info("扩展消息转换器...");
|
|
|
|
|
// 创建一个消息转换器对象
|
|
|
|
|
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
2024-03-19 09:45:43 +08:00
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
|
|
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
|
|
|
|
objectMapper.registerModule(new JavaTimeModule());
|
2024-03-18 18:45:35 +08:00
|
|
|
|
// 需要为消息转换器设置一个对象转换器,对象转换器可以将Java对象序列化为json数据
|
2024-03-19 09:45:43 +08:00
|
|
|
|
converter.setObjectMapper(objectMapper);
|
2024-03-18 18:45:35 +08:00
|
|
|
|
// 将自己的消息转化器加入容器中
|
|
|
|
|
converters.add(0, converter);
|
|
|
|
|
}
|
|
|
|
|
}
|