2024-03-18 18:45:35 +08:00
|
|
|
|
package com.sky.common.config;
|
|
|
|
|
|
|
|
|
|
import com.sky.common.interceptor.JwtTokenUserInterceptor;
|
|
|
|
|
import com.sky.common.interceptor.RedisTokenAdminInterceptor;
|
2024-03-19 09:49:03 +08:00
|
|
|
|
import com.sky.common.json.JacksonObjectMapper;
|
2024-03-18 18:45:35 +08:00
|
|
|
|
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();
|
|
|
|
|
// 需要为消息转换器设置一个对象转换器,对象转换器可以将Java对象序列化为json数据
|
2024-03-19 09:49:03 +08:00
|
|
|
|
converter.setObjectMapper(new JacksonObjectMapper());
|
2024-03-18 18:45:35 +08:00
|
|
|
|
// 将自己的消息转化器加入容器中
|
|
|
|
|
converters.add(0, converter);
|
|
|
|
|
}
|
|
|
|
|
}
|