WebMvcConfig.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package com.lqkj.link.config;
  2. import com.lqkj.link.GlobalAsyncExceptionHandler;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.http.CacheControl;
  6. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  7. import org.springframework.web.cors.CorsConfiguration;
  8. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  9. import org.springframework.web.filter.CorsFilter;
  10. import org.springframework.web.servlet.config.annotation.*;
  11. import org.springframework.web.servlet.mvc.WebContentInterceptor;
  12. import java.util.List;
  13. import java.util.concurrent.TimeUnit;
  14. @Configuration
  15. public class WebMvcConfig implements WebMvcConfigurer {
  16. /**
  17. * mvc异步线程池
  18. */
  19. @Override
  20. public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
  21. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  22. executor.setCorePoolSize(4);
  23. executor.setAllowCoreThreadTimeOut(false);
  24. executor.setMaxPoolSize(200);
  25. executor.setQueueCapacity(2000);
  26. executor.setThreadNamePrefix("link-mvc-");
  27. executor.initialize();
  28. //10秒超时时间
  29. configurer.setDefaultTimeout(1000 * 200);
  30. configurer.registerCallableInterceptors(new GlobalAsyncExceptionHandler());
  31. configurer.setTaskExecutor(executor);
  32. }
  33. /**
  34. * 静态资源配置
  35. */
  36. @Override
  37. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  38. registry.addResourceHandler("/upload/**")
  39. .addResourceLocations("file:./upload/")
  40. .setCacheControl(CacheControl.maxAge(0, TimeUnit.SECONDS));
  41. }
  42. /**
  43. * 跨域配置
  44. */
  45. @Bean
  46. public CorsFilter corsFilter() {
  47. CorsConfiguration corsConfiguration = new CorsConfiguration();
  48. corsConfiguration.setAllowCredentials(true);
  49. corsConfiguration.addAllowedOriginPattern("*");
  50. corsConfiguration.setAllowedHeaders(List.of("accept", "authorization", "content-type", "cache-control"));
  51. corsConfiguration.setAllowedMethods(List.of("POST", "GET", "PUT", "OPTIONS", "DELETE"));
  52. UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
  53. urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
  54. return new CorsFilter(urlBasedCorsConfigurationSource);
  55. }
  56. @Override
  57. public void addInterceptors(InterceptorRegistry registry) {
  58. WebContentInterceptor contentInterceptor = new WebContentInterceptor();
  59. contentInterceptor.setCacheSeconds(0);
  60. contentInterceptor.setUseExpiresHeader(true);
  61. contentInterceptor.setUseCacheControlNoStore(false);
  62. contentInterceptor.setUseCacheControlHeader(true);
  63. contentInterceptor.setUseCacheControlNoStore(false);
  64. registry.addInterceptor(contentInterceptor);
  65. }
  66. }