94 lines
4.2 KiB
Java
94 lines
4.2 KiB
Java
package com.imyeyu.api.config;
|
|
|
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
import com.fasterxml.jackson.databind.MapperFeature;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
|
import com.imyeyu.api.annotation.EnableSettingInterceptor;
|
|
import com.imyeyu.api.annotation.RequestRateLimitInterceptor;
|
|
import com.imyeyu.api.annotation.RequireCorePermissionInterceptor;
|
|
import com.imyeyu.api.annotation.RequireCoreRoleInterceptor;
|
|
import com.imyeyu.api.modules.gao.annotation.RequireGaoPermissionInterceptor;
|
|
import com.imyeyu.api.modules.gao.annotation.RequireGaoRoleInterceptor;
|
|
import com.imyeyu.api.modules.journal.util.JournalAPIInterceptor;
|
|
import com.imyeyu.api.modules.system.util.SystemAPIInterceptor;
|
|
import com.imyeyu.spring.annotation.RequestBodyValueArgumentResolver;
|
|
import com.imyeyu.utils.Time;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 系统配置
|
|
*
|
|
* @author 夜雨
|
|
* @since 2021-07-20 16:44
|
|
*/
|
|
@EnableWebMvc
|
|
@Configuration
|
|
@RequiredArgsConstructor
|
|
public class WebConfig implements WebMvcConfigurer {
|
|
|
|
private final ObjectMapper jackson;
|
|
private final SystemAPIInterceptor systemAPIInterceptor;
|
|
private final JournalAPIInterceptor journalAPIInterceptor;
|
|
private final EnableSettingInterceptor enableSettingInterceptor;
|
|
private final RequireGaoRoleInterceptor requireGaoRoleInterceptor;
|
|
private final RequireCoreRoleInterceptor requireCoreRoleInterceptor;
|
|
private final RequestRateLimitInterceptor requestRateLimitInterceptor;
|
|
private final RequireGaoPermissionInterceptor requireGaoPermissionInterceptor;
|
|
private final RequireCorePermissionInterceptor requireCorePermissionInterceptor;
|
|
|
|
/**
|
|
* 过滤器
|
|
*
|
|
* @param registry 注册表
|
|
*/
|
|
@Override
|
|
public void addInterceptors(InterceptorRegistry registry) {
|
|
registry.addInterceptor(systemAPIInterceptor).addPathPatterns(SystemAPIInterceptor.PATH);
|
|
registry.addInterceptor(journalAPIInterceptor).addPathPatterns(JournalAPIInterceptor.PATH);
|
|
registry.addInterceptor(enableSettingInterceptor).addPathPatterns("/**");
|
|
registry.addInterceptor(requireCoreRoleInterceptor).addPathPatterns("/**");
|
|
registry.addInterceptor(requireCorePermissionInterceptor).addPathPatterns("/**");
|
|
registry.addInterceptor(requireGaoRoleInterceptor).addPathPatterns("/**");
|
|
registry.addInterceptor(requireGaoPermissionInterceptor).addPathPatterns("/**");
|
|
registry.addInterceptor(requestRateLimitInterceptor).addPathPatterns("/**");
|
|
}
|
|
|
|
@Override
|
|
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
|
|
argumentResolvers.add(new RequestBodyValueArgumentResolver(jackson));
|
|
}
|
|
|
|
/**
|
|
* 通信消息转换
|
|
*
|
|
* @param converters 转换器
|
|
*/
|
|
@Override
|
|
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
JsonMapper jsonMapper = JsonMapper.builder()
|
|
// 设置默认属性包含规则:忽略 null 值
|
|
.serializationInclusion(JsonInclude.Include.NON_NULL)
|
|
// 日期格式化
|
|
.defaultDateFormat(Time.dateTime)
|
|
// 忽略不存在字段
|
|
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
|
|
// 启用默认视图包含
|
|
.enable(MapperFeature.DEFAULT_VIEW_INCLUSION).build();
|
|
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(jsonMapper);
|
|
converter.setDefaultCharset(StandardCharsets.UTF_8);
|
|
converters.add(converter);
|
|
}
|
|
}
|