WebConfig.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2020 铭软科技(mingsoft.net)
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. * The above copyright notice and this permission notice shall be included in all
  11. * copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. package net.mingsoft.config;
  20. import com.alibaba.druid.pool.DruidDataSource;
  21. import com.alibaba.druid.support.spring.stat.BeanTypeAutoProxyCreator;
  22. import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
  23. import com.fasterxml.jackson.databind.DeserializationFeature;
  24. import com.fasterxml.jackson.databind.ObjectMapper;
  25. import net.mingsoft.basic.filter.XSSEscapeFilter;
  26. import net.mingsoft.basic.interceptor.ActionInterceptor;
  27. import net.mingsoft.basic.resolver.MSLocaleResolver;
  28. import org.springframework.beans.factory.annotation.Value;
  29. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  30. import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
  31. import org.springframework.context.annotation.Bean;
  32. import org.springframework.context.annotation.Configuration;
  33. import org.springframework.core.Ordered;
  34. import org.springframework.http.converter.HttpMessageConverter;
  35. import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  36. import org.springframework.web.context.request.RequestContextListener;
  37. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  38. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  39. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  40. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  41. import java.io.File;
  42. import java.util.List;
  43. import java.util.concurrent.ExecutorService;
  44. import java.util.concurrent.LinkedBlockingQueue;
  45. import java.util.concurrent.ThreadPoolExecutor;
  46. import java.util.concurrent.TimeUnit;
  47. @Configuration
  48. public class WebConfig implements WebMvcConfigurer {
  49. /**
  50. * 上传路径
  51. */
  52. @Value("${ms.upload.path}")
  53. private String uploadFloderPath;
  54. @Value("${ms.upload.template}")
  55. private String template;
  56. /**
  57. * 上传路径映射
  58. */
  59. @Value("${ms.upload.mapping}")
  60. private String uploadMapping;
  61. @Bean
  62. public ActionInterceptor actionInterceptor() {
  63. return new ActionInterceptor();
  64. }
  65. @Bean
  66. public ConfigurationCustomizer configurationCustomizer() {
  67. return configuration -> configuration.setUseDeprecatedExecutor(false);
  68. }
  69. /**
  70. * 增加对rest api鉴权的spring mvc拦截器
  71. */
  72. @Override
  73. public void addInterceptors(InterceptorRegistry registry) {
  74. // 排除配置
  75. registry.addInterceptor(actionInterceptor()).excludePathPatterns("/static/**", "/app/**", "/webjars/**",
  76. "/*.html", "/*.htm");
  77. }
  78. @Override
  79. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  80. registry.addResourceHandler(uploadMapping).addResourceLocations(File.separator+uploadFloderPath+File.separator,"file:"+uploadFloderPath+File.separator);
  81. registry.addResourceHandler("/template/**").addResourceLocations(File.separator+template+File.separator,"file:"+template+File.separator);
  82. registry.addResourceHandler("/html/**").addResourceLocations("/html/","file:html/");
  83. //三种映射方式 webapp下、当前目录下、jar内
  84. registry.addResourceHandler("/app/**").addResourceLocations("/app/","file:app/", "classpath:/app/");
  85. registry.addResourceHandler("/static/**").addResourceLocations("/static/","file:static/","classpath:/static/","classpath:/META-INF/resources/");
  86. registry.addResourceHandler("/api/**").addResourceLocations("/api/","file:api/", "classpath:/api/");
  87. if(new File(uploadFloderPath).isAbsolute()){
  88. //如果指定了绝对路径,上传的文件都映射到uploadMapping下
  89. registry.addResourceHandler(uploadMapping).addResourceLocations("file:"+uploadFloderPath+ File.separator
  90. //映射其他路径文件
  91. //,file:F://images
  92. );
  93. }
  94. }
  95. /**
  96. * druid数据库连接池监控
  97. */
  98. @Bean
  99. public BeanTypeAutoProxyCreator beanTypeAutoProxyCreator() {
  100. BeanTypeAutoProxyCreator beanTypeAutoProxyCreator = new BeanTypeAutoProxyCreator();
  101. beanTypeAutoProxyCreator.setTargetBeanType(DruidDataSource.class);
  102. beanTypeAutoProxyCreator.setInterceptorNames("druidStatInterceptor");
  103. return beanTypeAutoProxyCreator;
  104. }
  105. //XSS过滤器
  106. @Bean
  107. public FilterRegistrationBean xssFilterRegistration() {
  108. XSSEscapeFilter xssFilter = new XSSEscapeFilter();
  109. FilterRegistrationBean registration = new FilterRegistrationBean(xssFilter);
  110. xssFilter.includes.add(".*/search.do");
  111. registration.setName("XSSFilter");
  112. registration.addUrlPatterns("/*");
  113. registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
  114. return registration;
  115. }
  116. /**
  117. * RequestContextListener注册
  118. */
  119. @Bean
  120. public ServletListenerRegistrationBean<RequestContextListener> requestContextListenerRegistration() {
  121. return new ServletListenerRegistrationBean<>(new RequestContextListener());
  122. }
  123. /**
  124. * 设置默认首页
  125. */
  126. @Override
  127. public void addViewControllers(ViewControllerRegistry registry) {
  128. registry.addViewController("/").setViewName("forward:/index");
  129. registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
  130. WebMvcConfigurer.super.addViewControllers(registry);
  131. }
  132. /**
  133. * 解决com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException 问题,提交实体不存在的字段异常
  134. */
  135. @Override
  136. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  137. // TODO Auto-generated method stub
  138. converters.add(mappingJackson2HttpMessageConverter());
  139. WebMvcConfigurer.super.configureMessageConverters(converters);
  140. }
  141. @Bean
  142. public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
  143. MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  144. ObjectMapper objectMapper = new ObjectMapper();
  145. //添加此配置
  146. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  147. converter.setObjectMapper(objectMapper);
  148. return converter;
  149. }
  150. @Bean
  151. public ExecutorService crawlExecutorPool() {
  152. // 创建线程池
  153. ExecutorService pool =
  154. new ThreadPoolExecutor(20, 20,
  155. 0L, TimeUnit.MILLISECONDS,
  156. new LinkedBlockingQueue<Runnable>());
  157. return pool;
  158. }
  159. /**
  160. * 国际化配置拦截
  161. * @return
  162. */
  163. @Bean
  164. public MSLocaleResolver localeResolver(){
  165. return new MSLocaleResolver();
  166. }
  167. }