WebConfig.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2012-2022 铭软科技(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 cn.hutool.core.util.StrUtil;
  21. import com.alibaba.druid.pool.DruidDataSource;
  22. import com.alibaba.druid.support.spring.stat.BeanTypeAutoProxyCreator;
  23. import com.alibaba.fastjson.JSON;
  24. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  25. import net.mingsoft.basic.filter.XSSEscapeFilter;
  26. import net.mingsoft.basic.interceptor.ActionInterceptor;
  27. import net.mingsoft.mdiy.biz.IConfigBiz;
  28. import net.mingsoft.mdiy.entity.ConfigEntity;
  29. import org.apache.commons.lang3.StringUtils;
  30. import org.springframework.beans.factory.annotation.Value;
  31. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  32. import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
  33. import org.springframework.context.annotation.Bean;
  34. import org.springframework.context.annotation.Configuration;
  35. import org.springframework.core.Ordered;
  36. import org.springframework.http.converter.HttpMessageConverter;
  37. import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  38. import org.springframework.web.context.request.RequestContextListener;
  39. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  40. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  41. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  42. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  43. import javax.annotation.Resource;
  44. import java.io.File;
  45. import java.util.Arrays;
  46. import java.util.HashMap;
  47. import java.util.List;
  48. import java.util.Map;
  49. import java.util.concurrent.ExecutorService;
  50. import java.util.concurrent.LinkedBlockingQueue;
  51. import java.util.concurrent.ThreadPoolExecutor;
  52. import java.util.concurrent.TimeUnit;
  53. @Configuration
  54. public class WebConfig implements WebMvcConfigurer {
  55. @Resource
  56. private IConfigBiz configBiz;
  57. @Resource
  58. private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;
  59. @Bean
  60. public ActionInterceptor actionInterceptor() {
  61. return new ActionInterceptor();
  62. }
  63. // @Bean
  64. // public ConfigurationCustomizer configurationCustomizer() {
  65. // return configuration -> configuration.setUseDeprecatedExecutor(false);
  66. // }
  67. // 最新版
  68. // @Bean
  69. // public MybatisPlusInterceptor mybatisPlusInterceptor() {
  70. // MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  71. // interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.));
  72. // return interceptor;
  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. String uploadMapping = MSProperties.upload.mapping;
  86. String uploadFolderPath = MSProperties.upload.path;
  87. String template = MSProperties.upload.template;
  88. String htmlDir = MSProperties.diy.htmlDir;
  89. // 上传路径映射 这里的映射不能使用File.separator Windows会存在映射问题
  90. registry.addResourceHandler(uploadMapping).addResourceLocations("/" + uploadFolderPath + "/", "file:" + uploadFolderPath + "/");
  91. registry.addResourceHandler("/" + template + "/**").addResourceLocations("/" + template + "/", "file:" + template + "/");
  92. registry.addResourceHandler("/"+htmlDir+"/**").addResourceLocations("/"+htmlDir+"/", "file:"+htmlDir+"/");
  93. //三种映射方式 webapp下、当前目录下、jar内
  94. registry.addResourceHandler("/app/**").addResourceLocations("/app/", "file:app/", "classpath:/app/");
  95. registry.addResourceHandler("/static/**").addResourceLocations("/static/", "file:static/", "classpath:/static/", "classpath:/META-INF/resources/");
  96. registry.addResourceHandler("/api/**").addResourceLocations("/api/", "file:api/", "classpath:/api/");
  97. if (new File(uploadFolderPath).isAbsolute()) {
  98. //如果指定了绝对路径,上传的文件都映射到uploadMapping下
  99. registry.addResourceHandler(uploadMapping).addResourceLocations("file:" + uploadFolderPath + "/"
  100. //映射其他路径文件
  101. //,file:F://images
  102. );
  103. }
  104. }
  105. /**
  106. * druid数据库连接池监控
  107. */
  108. @Bean
  109. public BeanTypeAutoProxyCreator beanTypeAutoProxyCreator() {
  110. BeanTypeAutoProxyCreator beanTypeAutoProxyCreator = new BeanTypeAutoProxyCreator();
  111. beanTypeAutoProxyCreator.setTargetBeanType(DruidDataSource.class);
  112. beanTypeAutoProxyCreator.setInterceptorNames("druidStatInterceptor");
  113. return beanTypeAutoProxyCreator;
  114. }
  115. //XSS过滤器
  116. @Bean
  117. public FilterRegistrationBean xssFilterRegistration(@Value("${ms.xss.enable:true}") boolean xssEnable,
  118. @Value("${ms.xss.filter-url}") String filterUrl,
  119. @Value("${ms.xss.exclude-url}") String excludeUrl) {
  120. XSSEscapeFilter xssFilter = new XSSEscapeFilter();
  121. Map<String, String> initParameters = new HashMap();
  122. FilterRegistrationBean registration = new FilterRegistrationBean();
  123. registration.setName("XSSFilter");
  124. registration.addUrlPatterns(new String[]{"/*"});
  125. registration.setOrder(-2147483648);
  126. if (filterUrl != null && StrUtil.isNotBlank(filterUrl)) {
  127. xssFilter.includes.addAll(Arrays.asList(filterUrl.split(",")));
  128. }else {
  129. xssFilter.includes.add("/**");
  130. }
  131. if (excludeUrl != null && StrUtil.isNotBlank(excludeUrl)) {
  132. xssFilter.excludes.addAll(Arrays.asList(excludeUrl.split(",")));
  133. }else {
  134. xssFilter.excludes.add(MSProperties.manager.path + "/**");
  135. }
  136. initParameters.put("isIncludeRichText", "false");
  137. registration.setInitParameters(initParameters);
  138. registration.setFilter(xssFilter);
  139. registration.setEnabled(xssEnable);
  140. return registration;
  141. }
  142. /**
  143. * RequestContextListener注册
  144. */
  145. @Bean
  146. public ServletListenerRegistrationBean<RequestContextListener> requestContextListenerRegistration() {
  147. return new ServletListenerRegistrationBean<>(new RequestContextListener());
  148. }
  149. /**
  150. * 设置默认首页
  151. */
  152. @Override
  153. public void addViewControllers(ViewControllerRegistry registry) {
  154. registry.addViewController("/").setViewName("forward:/index");
  155. registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
  156. WebMvcConfigurer.super.addViewControllers(registry);
  157. }
  158. /**
  159. * 解决com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException 问题,提交实体不存在的字段异常
  160. */
  161. @Override
  162. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  163. // TODO Auto-generated method stub
  164. converters.add(mappingJackson2HttpMessageConverter);
  165. WebMvcConfigurer.super.configureMessageConverters(converters);
  166. }
  167. @Bean
  168. public ExecutorService crawlExecutorPool() {
  169. // 创建线程池
  170. ExecutorService pool =
  171. new ThreadPoolExecutor(20, 20,
  172. 0L, TimeUnit.MILLISECONDS,
  173. new LinkedBlockingQueue<Runnable>());
  174. return pool;
  175. }
  176. public Map getMap(String configName) {
  177. if (!StringUtils.isEmpty(configName) && !StringUtils.isEmpty(configName)) {
  178. ConfigEntity configEntity = new ConfigEntity();
  179. configEntity.setConfigName(configName);
  180. configEntity = (ConfigEntity)this.configBiz.getOne(new QueryWrapper(configEntity));
  181. return configEntity != null && !StringUtils.isEmpty(configEntity.getConfigData()) ? (Map) JSON.parseObject(configEntity.getConfigData(), HashMap.class) : null;
  182. } else {
  183. return null;
  184. }
  185. }
  186. }