package com.kwan.springbootkwan.config; import org.springframework.boot.validation.MessageInterpolatorFactory; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.format.FormatterRegistry; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.validation.MessageCodesResolver; import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.HandlerMethodReturnValueHandler; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer; import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.nio.charset.StandardCharsets; import java.util.List; /** * 全局webconfig-跨域配置 * * @author : qinyingjie * @version : 2.2.0 * @date : 2022/12/19 16:11 */ @Configuration public class MyWebMvcConfig extends WebMvcConfigurationSupport implements WebMvcConfigurer { /** * 配置静态资源 * * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/"); registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } @Override public void extendMessageConverters(List> converters) { // 解决controller返回字符串中文乱码问题 for (HttpMessageConverter converter : converters) { if (converter instanceof StringHttpMessageConverter) { ((StringHttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8); } else if (converter instanceof MappingJackson2HttpMessageConverter) { ((MappingJackson2HttpMessageConverter) converter).setDefaultCharset(StandardCharsets.UTF_8); } } } @Override public void configureHandlerExceptionResolvers(List resolvers) { WebMvcConfigurer.super.configureHandlerExceptionResolvers(resolvers); } @Override public void extendHandlerExceptionResolvers(List resolvers) { WebMvcConfigurer.super.extendHandlerExceptionResolvers(resolvers); } @Override public Validator getValidator() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setDefaultEncoding("utf-8");// 读取配置文件的编码格式 messageSource.setCacheMillis(-1);// 缓存时间,-1表示不过期 messageSource.setBasename("ValidationMessages");// 配置文件前缀名,设置为Messages,那你的配置文件必须以Messages.properties/Message_en.properties... LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean(); MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory(); factoryBean.setMessageInterpolator(interpolatorFactory.getObject()); factoryBean.setValidationMessageSource(messageSource); return factoryBean; } @Override public MessageCodesResolver getMessageCodesResolver() { return WebMvcConfigurer.super.getMessageCodesResolver(); } @Override public void configurePathMatch(PathMatchConfigurer configurer) { WebMvcConfigurer.super.configurePathMatch(configurer); } @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { WebMvcConfigurer.super.configureContentNegotiation(configurer); } @Override public void configureAsyncSupport(AsyncSupportConfigurer configurer) { WebMvcConfigurer.super.configureAsyncSupport(configurer); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { WebMvcConfigurer.super.configureDefaultServletHandling(configurer); } @Override public void addFormatters(FormatterRegistry registry) { WebMvcConfigurer.super.addFormatters(registry); } /** * 配置拦截器 * * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/hello"); } /** * 配置全局跨域 * * @param registry */ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/book/**") .allowedHeaders("*") .allowedMethods("*") .maxAge(1800) .allowedOrigins("http://localhost:8081"); } @Override public void addViewControllers(ViewControllerRegistry registry) { WebMvcConfigurer.super.addViewControllers(registry); } @Override public void configureViewResolvers(ViewResolverRegistry registry) { WebMvcConfigurer.super.configureViewResolvers(registry); } @Override public void addArgumentResolvers(List resolvers) { WebMvcConfigurer.super.addArgumentResolvers(resolvers); } @Override public void addReturnValueHandlers(List handlers) { WebMvcConfigurer.super.addReturnValueHandlers(handlers); } @Override public void configureMessageConverters(List> converters) { WebMvcConfigurer.super.configureMessageConverters(converters); } }