12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- package com.lqkj.link.config;
- import com.lqkj.link.GlobalAsyncExceptionHandler;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.CacheControl;
- import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
- import org.springframework.web.cors.CorsConfiguration;
- import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
- import org.springframework.web.filter.CorsFilter;
- import org.springframework.web.servlet.config.annotation.*;
- import org.springframework.web.servlet.mvc.WebContentInterceptor;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
- @Configuration
- public class WebMvcConfig implements WebMvcConfigurer {
- /**
- * mvc异步线程池
- */
- @Override
- public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
- ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
- executor.setCorePoolSize(4);
- executor.setAllowCoreThreadTimeOut(false);
- executor.setMaxPoolSize(200);
- executor.setQueueCapacity(2000);
- executor.setThreadNamePrefix("link-mvc-");
- executor.initialize();
- //10秒超时时间
- configurer.setDefaultTimeout(1000 * 200);
- configurer.registerCallableInterceptors(new GlobalAsyncExceptionHandler());
- configurer.setTaskExecutor(executor);
- }
- /**
- * 静态资源配置
- */
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/upload/**")
- .addResourceLocations("file:./upload/")
- .setCacheControl(CacheControl.maxAge(0, TimeUnit.SECONDS));
- }
- /**
- * 跨域配置
- */
- @Bean
- public CorsFilter corsFilter() {
- CorsConfiguration corsConfiguration = new CorsConfiguration();
- corsConfiguration.setAllowCredentials(true);
- corsConfiguration.addAllowedOriginPattern("*");
- corsConfiguration.setAllowedHeaders(List.of("accept", "authorization", "content-type", "cache-control"));
- corsConfiguration.setAllowedMethods(List.of("POST", "GET", "PUT", "OPTIONS", "DELETE"));
- UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
- urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
- return new CorsFilter(urlBasedCorsConfigurationSource);
- }
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- WebContentInterceptor contentInterceptor = new WebContentInterceptor();
- contentInterceptor.setCacheSeconds(0);
- contentInterceptor.setUseExpiresHeader(true);
- contentInterceptor.setUseCacheControlNoStore(false);
- contentInterceptor.setUseCacheControlHeader(true);
- contentInterceptor.setUseCacheControlNoStore(false);
- registry.addInterceptor(contentInterceptor);
- }
- }
|