WebConfig.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. /**
  62. * 上传路径映射
  63. */
  64. @Value("${ms.diy.html-dir}")
  65. private String htmlDir;
  66. @Bean
  67. public ActionInterceptor actionInterceptor() {
  68. return new ActionInterceptor();
  69. }
  70. @Bean
  71. public ConfigurationCustomizer configurationCustomizer() {
  72. return configuration -> configuration.setUseDeprecatedExecutor(false);
  73. }
  74. /**
  75. * 增加对rest api鉴权的spring mvc拦截器
  76. */
  77. @Override
  78. public void addInterceptors(InterceptorRegistry registry) {
  79. // 排除配置
  80. registry.addInterceptor(actionInterceptor()).excludePathPatterns("/static/**", "/app/**", "/webjars/**",
  81. "/*.html", "/*.htm");
  82. }
  83. @Override
  84. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  85. registry.addResourceHandler(uploadMapping).addResourceLocations(File.separator+uploadFloderPath+File.separator,"file:"+uploadFloderPath+File.separator,"classpath:/template/");
  86. registry.addResourceHandler("/template/**").addResourceLocations(File.separator+template+File.separator,"file:"+template+File.separator,"classpath:/html/");
  87. registry.addResourceHandler(File.separator+htmlDir+File.separator+"**").addResourceLocations(File.separator+htmlDir+File.separator,"file:"+htmlDir+File.separator);
  88. //三种映射方式 webapp下、当前目录下、jar内
  89. registry.addResourceHandler("/app/**").addResourceLocations("/app/","file:app/", "classpath:/app/");
  90. registry.addResourceHandler("/static/**").addResourceLocations("/static/","file:static/","classpath:/static/","classpath:/META-INF/resources/");
  91. registry.addResourceHandler("/api/**").addResourceLocations("/api/","file:api/", "classpath:/api/");
  92. if(new File(uploadFloderPath).isAbsolute()){
  93. //如果指定了绝对路径,上传的文件都映射到uploadMapping下
  94. registry.addResourceHandler(uploadMapping).addResourceLocations("file:"+uploadFloderPath+ File.separator
  95. //映射其他路径文件
  96. //,file:F://images
  97. );
  98. }
  99. }
  100. /**
  101. * druid数据库连接池监控
  102. */
  103. @Bean
  104. public BeanTypeAutoProxyCreator beanTypeAutoProxyCreator() {
  105. BeanTypeAutoProxyCreator beanTypeAutoProxyCreator = new BeanTypeAutoProxyCreator();
  106. beanTypeAutoProxyCreator.setTargetBeanType(DruidDataSource.class);
  107. beanTypeAutoProxyCreator.setInterceptorNames("druidStatInterceptor");
  108. return beanTypeAutoProxyCreator;
  109. }
  110. //XSS过滤器
  111. @Bean
  112. public FilterRegistrationBean xssFilterRegistration() {
  113. XSSEscapeFilter xssFilter = new XSSEscapeFilter();
  114. FilterRegistrationBean registration = new FilterRegistrationBean(xssFilter);
  115. xssFilter.includes.add(".*/search.do");
  116. registration.setName("XSSFilter");
  117. registration.addUrlPatterns("/*");
  118. registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
  119. return registration;
  120. }
  121. /**
  122. * RequestContextListener注册
  123. */
  124. @Bean
  125. public ServletListenerRegistrationBean<RequestContextListener> requestContextListenerRegistration() {
  126. return new ServletListenerRegistrationBean<>(new RequestContextListener());
  127. }
  128. /**
  129. * 设置默认首页
  130. */
  131. @Override
  132. public void addViewControllers(ViewControllerRegistry registry) {
  133. registry.addViewController("/").setViewName("forward:/index.do");
  134. registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
  135. WebMvcConfigurer.super.addViewControllers(registry);
  136. }
  137. /**
  138. * 解决com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException 问题,提交实体不存在的字段异常
  139. */
  140. @Override
  141. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  142. // TODO Auto-generated method stub
  143. converters.add(mappingJackson2HttpMessageConverter());
  144. WebMvcConfigurer.super.configureMessageConverters(converters);
  145. }
  146. @Bean
  147. public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
  148. MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  149. ObjectMapper objectMapper = new ObjectMapper();
  150. //添加此配置
  151. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  152. converter.setObjectMapper(objectMapper);
  153. return converter;
  154. }
  155. @Bean
  156. public ExecutorService crawlExecutorPool() {
  157. // 创建线程池
  158. ExecutorService pool =
  159. new ThreadPoolExecutor(20, 20,
  160. 0L, TimeUnit.MILLISECONDS,
  161. new LinkedBlockingQueue<Runnable>());
  162. return pool;
  163. }
  164. /**
  165. * 国际化配置拦截
  166. * @return
  167. */
  168. @Bean
  169. public MSLocaleResolver localeResolver(){
  170. return new MSLocaleResolver();
  171. }
  172. }